当前位置:   article > 正文

Python实战—植物大战僵尸_机枪豌豆源代码

机枪豌豆源代码

开发思路
1)显示窗口(左上角坐标(0,0));
2)显示豌豆;
3)通过键盘控制豌豆上下移动;
只要不松手,没有执行向上的事件,就一直移动
优化:
1)移动速度快;
2)界面越界;
3)背景乱;(往下移动覆盖没有背景的地方,由于是实时刷新,本次实例不会存在此情况因为背景图片占满整个屏幕)
面向对象:

安装pygame
在这里插入图片描述
查看安装路径:
print(pygame.file)
包路径:
D:\Python 3.5\lib\site-packages\pygame_init_.py在这里插入图片描述

#coding:utf-8

import pygame
import random
#所有的 Pygame 常量导入
from pygame.locals import *


#窗体的高度和宽度
WIDTH=1200
HEIGHT=600

#豌豆对象
class Peas:
    def __init__(self):
        #获取图片
        self.image=pygame.image.load(r"./imgs/sunflower.png")
        #h获取位置大小
        self.image_rect=self.image.get_rect()
        #初始位置
        self.image_rect.top=280
        #设置豌豆默认不上下移动,默认不发射炮弹
        self.is_move_down=False
        self.is_move_up = False
        self.is_shout = False

    #豌豆显示在页面上
    def display(self):
        # 显示豌豆
        screen.blit(self.image, self.image_rect)

    #向上移动
    def move_up(self):
        if self.image_rect.top>10:
            peas.image_rect.move_ip(0, -5)
        #豌豆碰到僵尸
        for z in Zombie.zombie_list:
            if self.image_rect.colliderect(z.image_rect):
                pygame.quit()
    #向下移动
    def move_down(self):
        if self.image_rect.bottom<600:
            peas.image_rect.move_ip(0, 5)
        # 豌豆碰到僵尸
        for z in Zombie.zombie_list:
            if self.image_rect.colliderect(z.image_rect):
                pygame.quit()
    #发射炮弹的方法
    def shout_bullet(self):
        #创建一个炮弹对象
        bullet=Bullet(peas)
        #保存创建好的炮弹对象
        Bullet.bullet_list.append(bullet)


#炮弹对象
class Bullet:
    #显示所有炮弹信息
    bullet_list=[]
    #设置炮弹间隔时间(由于炮弹发射太快影响体验)
    interval=0

    def __init__(self,pea):
        #获取图片
        self.image=pygame.image.load(r"./imgs/sun.png")
        # 改变图片大小
        self.image = pygame.transform.scale(self.image, (50, 50))
        #h获取位置大小
        self.image_rect=self.image.get_rect()
        #初始位置
        self.image_rect.top=pea.image_rect.top
        self.image_rect.left=pea.image_rect.right

    # 炮弹显示在页面上
    def display(self):
        # 显示豌豆
        screen.blit(self.image, self.image_rect)

    #炮弹移动
    def move(self):
        #炮弹移动,x增加10
        self.image_rect.move_ip(10,0)
        #如果炮弹越界,删除炮弹
        if self.image_rect.right>WIDTH-40:
            Bullet.bullet_list.remove(self)
        #遍历所有僵尸
        for z in Zombie.zombie_list:
            #如果炮弹碰撞僵尸,移除僵尸,炮弹
            if self.image_rect.colliderect(z.image_rect):
                Bullet.bullet_list.remove(self)
                Zombie.zombie_list.remove(z)
                break

#僵尸对象
class Zombie:
    #创建频率
    interval = 0
    #类变量,保存多个僵尸
    zombie_list=[]

    def __init__(self):
        # 获取图片
        self.image = pygame.image.load(r"./imgs/bubble.png")
        #改变图片大小
        self.image=pygame.transform.scale(self.image, (70,70))
        # h获取位置大小
        self.image_rect = self.image.get_rect()
        # 初始位置
        self.image_rect.top = random.randint(10,HEIGHT-70)
        self.image_rect.left = WIDTH

    # 炮弹显示在页面上
    def display(self):
        # 显示僵尸
        screen.blit(self.image, self.image_rect)
    #僵尸移动
    def move(self):
        self.image_rect.move_ip(-2,0)
        #僵尸移动的x小于等于
        if self.image_rect.right<=0:
            #僵尸消失
            Zombie.zombie_list.remove(self)
        for b in Bullet.bullet_list:
            #僵尸碰撞到炮弹
            if self.image_rect.colliderect(b.image_rect):
                Zombie.zombie_list.remove(self)
                Bullet.bullet_list.remove(b)
                break
        #僵尸碰到豌豆
        if self.image_rect.colliderect(peas.image_rect):
            pygame.quit()
            exit()



#控制事件
def key_control():
    # 对事件处理
    for event in pygame.event.get():
        # 事件类型判断
        if event.type == QUIT:
            # 退出游戏
            pygame.quit()
            exit()
        # 按下事件类型的监听
        elif event.type == KEYDOWN:
            # 判断具体的键
            if event.key == K_UP:
                print('向上移动')
                # 移动方法
                peas.is_move_up = True
            elif event.key == K_DOWN:
                print('向下移动')
                peas.is_move_down = True
            elif event.key==K_SPACE:
                print('发射炮弹')
                peas.is_shout=True
        # 松开事件类型的监听
        elif event.type == KEYUP:
            # 判断具体的键
            if event.key == K_UP:
                print("向上移动松开")
                peas.is_move_up = False
            elif event.key == K_DOWN:
                print("向下移动松开")
                peas.is_move_down = False
            elif event.key == K_SPACE:
                print('空格键松开')
                peas.is_shout = False


if __name__ == '__main__':
    #1 显示窗体
    screen=pygame.display.set_mode((WIDTH,HEIGHT))
    #背景图片
    backeground_image=pygame.image.load(r"./imgs/bg.png")
    #改变图片大小
    scale_background_image=pygame.transform.scale(backeground_image,(WIDTH,HEIGHT))
    #获取到图片位置和大小
    scale_background_image_rect=scale_background_image.get_rect()
    #设置窗体的背景图片
    screen.blit(scale_background_image,scale_background_image_rect)
    #创建一个时钟,优化运行速度效果
    clock=pygame.time.Clock()
    #创建一个豌豆对象
    peas=Peas()

    while True:
        #设置窗体的背景图片
        screen.blit(scale_background_image,scale_background_image_rect)
        #调用展示豌豆
        peas.display()
        #调用事件处理方法
        key_control()
        #按键并图片位置顶部大于20或豌豆位置底部小于600,就可以执行移动方法
        if peas.is_move_up:
            peas.move_up()
        if peas.is_move_down:
            peas.move_down()
        #每20次发射炮弹
        Bullet.interval+=1
        if peas.is_shout and Bullet.interval>20:
            Bullet.interval=0
            peas.shout_bullet()
            print('发射炮弹')
        #创建僵尸
        Zombie.interval+=1
        if Zombie.interval>=20:
            Zombie.interval=0
            # 创建僵尸,并且保存
            zombie = Zombie()
            Zombie.zombie_list.append(zombie)


        #显示所有炮弹
        for bullet in Bullet.bullet_list:
            #炮弹显示
            bullet.display()
            #炮弹移动
            bullet.move()
        #显示僵尸
        for zombie in Zombie.zombie_list:
            zombie.display()
            zombie.move()

        #变化帧率:就是让我们的动画基于时间运作,我们需要知道上一个画面到现在经过了多少时间,然后我们才能决定是否开始绘制下一幅
        clock.tick(60)
        #重新显示图片、窗口
        pygame.display.update()


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231

参考资料:https://www.bilibili.com/video/BV1Ut411D78w?from=search&seid=15797485005666258913

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号