赞
踩
开发思路
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()
参考资料:https://www.bilibili.com/video/BV1Ut411D78w?from=search&seid=15797485005666258913
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。