实现步骤
1.创建窗口
2.创建一个玩家飞机,按方向键可以左右移动
3.给玩家飞机添加按空格键发射子弹功能
4.创建一个敌机
5.敌机自动左右移动
6.敌机自动发射子弹
1.创建窗口
import pygame import time def main(): #1.创建窗口 screen = pygame.display.set_mode((480,852)) #2 创建一个背景图片 background = pygame.image.load(‘./feiji/background.png‘) while True: #3 显示到屏幕 screen.blit(background,(0,0)) pygame.display.update() time.sleep(0.05) if __name__ == ‘__main__‘: main()
2.显示玩家飞机,检测键盘,控制飞机左右移动
import pygame import time from pygame.locals import * class HeroPlane(object): def __init__(self,screen_temp): self.x = 210 self.y = 700 self.screen = screen_temp #游戏窗口 self.image = pygame.image.load(‘feiji/hero1.png‘) def display(self): self.screen.blit(self.image,(self.x,self.y)) #加载玩家飞机到窗口 def move_left(self): self.x -= 5 def move_right(self): self.x += 5 def key_control(hero_temp): # 获取事件,比如按键等 for event in pygame.event.get(): # 判断是否是点击了退出按钮 if event.type == QUIT: print("exit") exit() # 判断是否是按下了键 elif event.type == KEYDOWN: # 检测按键是否是a或者left if event.key == K_a or event.key == K_LEFT: print(‘left‘) hero_temp.move_left() # 检测按键是否是d或者right elif event.key == K_d or event.key == K_RIGHT: print(‘right‘) hero_temp.move_right() # 检测按键是否是空格键 elif event.key == K_SPACE: print(‘space‘) def main(): #1.创建窗口 screen = pygame.display.set_mode((480,852),0,0) #2 创建一个背景图片 background = pygame.image.load(‘./feiji/background.png‘) # 创建一个飞机对象 hero = HeroPlane(screen) while True: #把背景图片放到窗口显示 screen.blit(background,(0,0)) #显示玩家飞机到定义的(x,y)坐标位置 hero.display() pygame.display.update() #检测键盘,控制玩家飞机移动 key_control(hero) time.sleep(0.05) if __name__ == ‘__main__‘: main()
3.玩家发射子弹
import pygame import time from pygame.locals import * class HeroPlane(object): def __init__(self,screen_temp): self.x = 210 self.y = 700 self.screen = screen_temp #游戏窗口 self.image = pygame.image.load(‘feiji/hero1.png‘) self.bullet_list = [] #存储发射出去的子弹 def display(self): # 加载玩家飞机到窗口 self.screen.blit(self.image,(self.x,self.y)) for bullet in self.bullet_list: bullet.display() bullet.move() def move_left(self): self.x -= 5 def move_right(self): self.x += 5 def fire(self): self.bullet_list.append(Bullet(self.screen,self.x,self.y)) class Bullet(object): def __init__(self,screen_temp,x,y): self.x = x + 40 self.y = y - 20 self.screen = screen_temp self.image = pygame.image.load(‘feiji/bullet.png‘) def display(self): self.screen.blit(self.image,(self.x,self.y)) def move(self): self.y -= 20 def key_control(hero_temp): # 获取事件,比如按键等 for event in pygame.event.get(): # 判断是否是点击了退出按钮 if event.type == QUIT: print("exit") exit() # 判断是否是按下了键 elif event.type == KEYDOWN: # 检测按键是否是a或者left if event.key == K_a or event.key == K_LEFT: print(‘left‘) hero_temp.move_left() # 检测按键是否是d或者right elif event.key == K_d or event.key == K_RIGHT: print(‘right‘) hero_temp.move_right() # 检测按键是否是空格键 elif event.key == K_SPACE: print(‘space‘) hero_temp.fire() def main(): #1.创建窗口 screen = pygame.display.set_mode((480,852),0,0) #2 创建一个背景图片 background = pygame.image.load(‘./feiji/background.png‘) # 创建一个飞机对象 hero = HeroPlane(screen) while True: #把背景图片放到窗口显示 screen.blit(background,(0,0)) #显示玩家飞机到定义的(x,y)坐标位置 hero.display() pygame.display.update() #检测键盘,控制玩家飞机移动 key_control(hero) time.sleep(0.05) if __name__ == ‘__main__‘: main()
效果如下:
4.创建一个敌机
--snip-- class EnemyPlane(object): def __init__(self,screen_temp): self.x = 0 self.y = 0 self.screen = screen_temp self.image = pygame.image.load(‘feiji/enemy0.png‘) def display(self): self.screen.blit(self.image,(self.x,self.y)) --snip-- def main(): --snip-- #4 创建一个敌机 enemy = EnemyPlane(screen) while True: --snip-- enemy.display() --snip
5.控制敌机左右移动
--snip-- class EnemyPlane(object): def __init__(self,screen_temp): --snip-- self.direction = ‘right‘ #定义敌机默认往右移动 --snip-- def move(self): if self.direction == ‘right‘: self.x += 8 elif self.direction == ‘left‘: self.x -= 8 if self.x > 430: self.direction = ‘left‘ elif self.x < 0: self.direction = ‘right‘ --snip-- def main(): --snip-- #4 创建一个敌机 enemy = EnemyPlane(screen) enemy.move() --snip--
import pygame import time from pygame.locals import * class HeroPlane(object): def __init__(self,screen_temp): self.x = 210 self.y = 700 self.screen = screen_temp #游戏窗口 self.image = pygame.image.load(‘feiji/hero1.png‘) self.bullet_list = [] #存储发射出去的子弹 def display(self): # 加载玩家飞机到窗口 self.screen.blit(self.image,(self.x,self.y)) for bullet in self.bullet_list: bullet.display() bullet.move() def move_left(self): self.x -= 5 def move_right(self): self.x += 5 def fire(self): self.bullet_list.append(Bullet(self.screen,self.x,self.y)) class EnemyPlane(object): def __init__(self,screen_temp): self.x = 0 self.y = 0 self.screen = screen_temp self.image = pygame.image.load(‘feiji/enemy0.png‘) self.direction = ‘right‘ #定义敌机默认往右移动 def display(self): self.screen.blit(self.image,(self.x,self.y)) def move(self): if self.direction == ‘right‘: self.x += 8 elif self.direction == ‘left‘: self.x -= 8 if self.x > 430: self.direction = ‘left‘ elif self.x < 0: self.direction = ‘right‘ class Bullet(object): def __init__(self,screen_temp,x,y): self.x = x + 40 self.y = y - 20 self.screen = screen_temp self.image = pygame.image.load(‘feiji/bullet.png‘) def display(self): self.screen.blit(self.image,(self.x,self.y)) def move(self): self.y -= 20 def key_control(hero_temp): # 获取事件,比如按键等 for event in pygame.event.get(): # 判断是否是点击了退出按钮 if event.type == QUIT: print("exit") exit() # 判断是否是按下了键 elif event.type == KEYDOWN: # 检测按键是否是a或者left if event.key == K_a or event.key == K_LEFT: print(‘left‘) hero_temp.move_left() # 检测按键是否是d或者right elif event.key == K_d or event.key == K_RIGHT: print(‘right‘) hero_temp.move_right() # 检测按键是否是空格键 elif event.key == K_SPACE: print(‘space‘) hero_temp.fire() def main(): #1.创建窗口 screen = pygame.display.set_mode((480,852),0,0) #2 创建一个背景图片 background = pygame.image.load(‘./feiji/background.png‘) #3 创建一个飞机对象 hero = HeroPlane(screen) #4 创建一个敌机 enemy = EnemyPlane(screen) while True: #把背景图片放到窗口显示 screen.blit(background,(0,0)) #显示玩家飞机到定义的(x,y)坐标位置 hero.display() enemy.display() enemy.move() pygame.display.update() #检测键盘,控制玩家飞机移动 key_control(hero) time.sleep(0.05) if __name__ == ‘__main__‘: main()
6.删除子弹,只要发射出去的子弹超过边界就删掉
--snip-- class HeroPlane(object): --snip-- --snip-- bullet.move() if bullet.judge(): #判断子弹是否越界 self.bullet_list.remove(bullet) --snip-- class Bullet(object): --sinp-- def judge(self): if self.y < 0: return True else: return False --snip
import pygame import time from pygame.locals import * class HeroPlane(object): def __init__(self,screen_temp): self.x = 210 self.y = 700 self.screen = screen_temp #游戏窗口 self.image = pygame.image.load(‘feiji/hero1.png‘) self.bullet_list = [] #存储发射出去的子弹 def display(self): # 加载玩家飞机到窗口 self.screen.blit(self.image,(self.x,self.y)) for bullet in self.bullet_list: bullet.display() bullet.move() if bullet.judge(): #判断子弹是否越界 self.bullet_list.remove(bullet) def move_left(self): self.x -= 5 def move_right(self): self.x += 5 def fire(self): self.bullet_list.append(Bullet(self.screen,self.x,self.y)) class EnemyPlane(object): def __init__(self,screen_temp): self.x = 0 self.y = 0 self.screen = screen_temp self.image = pygame.image.load(‘feiji/enemy0.png‘) self.direction = ‘right‘ #定义敌机默认往右移动 def display(self): self.screen.blit(self.image,(self.x,self.y)) def move(self): if self.direction == ‘right‘: self.x += 8 elif self.direction == ‘left‘: self.x -= 8 if self.x > 430: self.direction = ‘left‘ elif self.x < 0: self.direction = ‘right‘ class Bullet(object): def __init__(self,screen_temp,x,y): self.x = x + 40 self.y = y - 20 self.screen = screen_temp self.image = pygame.image.load(‘feiji/bullet.png‘) def display(self): self.screen.blit(self.image,(self.x,self.y)) def move(self): self.y -= 20 def judge(self): if self.y < 200: return True else: return False def key_control(hero_temp): # 获取事件,比如按键等 for event in pygame.event.get(): # 判断是否是点击了退出按钮 if event.type == QUIT: print("exit") exit() # 判断是否是按下了键 elif event.type == KEYDOWN: # 检测按键是否是a或者left if event.key == K_a or event.key == K_LEFT: print(‘left‘) hero_temp.move_left() # 检测按键是否是d或者right elif event.key == K_d or event.key == K_RIGHT: print(‘right‘) hero_temp.move_right() # 检测按键是否是空格键 elif event.key == K_SPACE: print(‘space‘) hero_temp.fire() def main(): #1.创建窗口 screen = pygame.display.set_mode((480,852),0,0) #2 创建一个背景图片 background = pygame.image.load(‘./feiji/background.png‘) #3 创建一个飞机对象 hero = HeroPlane(screen) #4 创建一个敌机 enemy = EnemyPlane(screen) while True: #把背景图片放到窗口显示 screen.blit(background,(0,0)) #显示玩家飞机到定义的(x,y)坐标位置 hero.display() enemy.display() enemy.move() pygame.display.update() #检测键盘,控制玩家飞机移动 key_control(hero) time.sleep(0.05) if __name__ == ‘__main__‘: main()
7.添加敌机放射子弹功能
使用random来控制敌机发射子弹的频率
--snip-- import random class EnemyPlane(object): --snip - - self.bullet_list = [] #存储发射出去的子弹 self.direction = ‘right‘ #定义敌机默认往右移动 def display(self): --snip - - bullet.move() if bullet.judge(): #判断子弹是否越界 self.bullet_list.remove(bullet) --snip-- def fire(self): #控制子弹频率 random_num = random.randint(1,80) if random_num == 10 or random_num == 40: self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y)) --snip-- class EnemyBullet(object): def __init__(self,screen_temp,x,y): self.x = x + 25 self.y = y + 40 self.screen = screen_temp self.image = pygame.image.load(‘feiji/bullet1.png‘) def display(self): self.screen.blit(self.image,(self.x,self.y)) def move(self): self.y += 10 def judge(self): if self.y > 852: return True else: return False --snip - - while True: --snip - - enemy.move() enemy.fire() --snip--
import pygame import time from pygame.locals import * import random class HeroPlane(object): def __init__(self,screen_temp): self.x = 210 self.y = 700 self.screen = screen_temp #游戏窗口 self.image = pygame.image.load(‘feiji/hero1.png‘) self.bullet_list = [] #存储发射出去的子弹 def display(self): # 加载玩家飞机到窗口 self.screen.blit(self.image,(self.x,self.y)) for bullet in self.bullet_list: bullet.display() bullet.move() if bullet.judge(): #判断子弹是否越界 self.bullet_list.remove(bullet) def move_left(self): self.x -= 5 def move_right(self): self.x += 5 def fire(self): self.bullet_list.append(Bullet(self.screen,self.x,self.y)) class EnemyPlane(object): def __init__(self,screen_temp): self.x = 0 self.y = 0 self.screen = screen_temp self.image = pygame.image.load(‘feiji/enemy0.png‘) self.bullet_list = [] #存储发射出去的子弹 self.direction = ‘right‘ #定义敌机默认往右移动 def display(self): self.screen.blit(self.image,(self.x,self.y)) for bullet in self.bullet_list: bullet.display() bullet.move() if bullet.judge(): #判断子弹是否越界 self.bullet_list.remove(bullet) def move(self): if self.direction == ‘right‘: self.x += 8 elif self.direction == ‘left‘: self.x -= 8 if self.x > 430: self.direction = ‘left‘ elif self.x < 0: self.direction = ‘right‘ def fire(self): random_num = random.randint(1,80) if random_num == 10 or random_num == 40: self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y)) class Bullet(object): def __init__(self,screen_temp,x,y): self.x = x + 40 self.y = y - 20 self.screen = screen_temp self.image = pygame.image.load(‘feiji/bullet.png‘) def display(self): self.screen.blit(self.image,(self.x,self.y)) def move(self): self.y -= 20 def judge(self): if self.y < 0: return True else: return False class EnemyBullet(object): def __init__(self,screen_temp,x,y): self.x = x + 25 self.y = y + 40 self.screen = screen_temp self.image = pygame.image.load(‘feiji/bullet1.png‘) def display(self): self.screen.blit(self.image,(self.x,self.y)) def move(self): self.y += 10 def judge(self): if self.y > 852: return True else: return False def key_control(hero_temp): # 获取事件,比如按键等 for event in pygame.event.get(): # 判断是否是点击了退出按钮 if event.type == QUIT: print("exit") exit() # 判断是否是按下了键 elif event.type == KEYDOWN: # 检测按键是否是a或者left if event.key == K_a or event.key == K_LEFT: print(‘left‘) hero_temp.move_left() # 检测按键是否是d或者right elif event.key == K_d or event.key == K_RIGHT: print(‘right‘) hero_temp.move_right() # 检测按键是否是空格键 elif event.key == K_SPACE: print(‘space‘) hero_temp.fire() def main(): #1.创建窗口 screen = pygame.display.set_mode((480,852),0,0) #2 创建一个背景图片 background = pygame.image.load(‘./feiji/background.png‘) #3 创建一个飞机对象 hero = HeroPlane(screen) #4 创建一个敌机 enemy = EnemyPlane(screen) while True: #把背景图片放到窗口显示 screen.blit(background,(0,0)) #显示玩家飞机到定义的(x,y)坐标位置 hero.display() enemy.display() enemy.move() enemy.fire() pygame.display.update() #检测键盘,控制玩家飞机移动 key_control(hero) time.sleep(0.05) if __name__ == ‘__main__‘: main()
最终效果
8.优化代码,创建基类
1 import pygame 2 import time 3 from pygame.locals import * 4 import random 5 6 class BasePlane(object): 7 ‘‘‘飞机类‘‘‘ 8 def __init__(self,screen_temp,x,y,image_name): 9 self.x = x 10 self.y = y 11 self.screen = screen_temp #游戏窗口 12 self.image = pygame.image.load(image_name) 13 self.bullet_list = [] #存储发射出去的子弹 14 15 def display(self): 16 # 加载飞机到窗口 17 self.screen.blit(self.image,(self.x,self.y)) 18 #控制子弹 19 for bullet in self.bullet_list: 20 bullet.display() 21 bullet.move() 22 if bullet.judge(): #判断子弹是否越界 23 self.bullet_list.remove(bullet) 24 25 class HeroPlane(BasePlane): 26 ‘‘‘玩家飞机‘‘‘ 27 def __init__(self,screen_temp): 28 BasePlane.__init__(self,screen_temp,210,700,‘feiji/hero1.png‘) 29 30 def move_left(self): 31 self.x -= 5 32 33 def move_right(self): 34 self.x += 5 35 36 def fire(self): 37 #发射子弹 38 self.bullet_list.append(Bullet(self.screen,self.x,self.y)) 39 40 class EnemyPlane(BasePlane): 41 ‘‘‘敌方飞机‘‘‘ 42 def __init__(self, screen_temp): 43 BasePlane.__init__(self, screen_temp, 0, 0, ‘feiji/enemy0.png‘) 44 self.direction = ‘right‘ #定义敌机默认往右移动 45 46 def move(self): 47 if self.direction == ‘right‘: 48 self.x += 8 49 elif self.direction == ‘left‘: 50 self.x -= 8 51 52 if self.x > 430: 53 self.direction = ‘left‘ 54 elif self.x < 0: 55 self.direction = ‘right‘ 56 57 def fire(self): 58 # 发射子弹 59 random_num = random.randint(1,50) 60 if random_num == 10 or random_num == 40: 61 self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y)) 62 63 class BaseBullet(object): 64 ‘‘‘子弹类‘‘‘ 65 def __init__(self,screen_temp,x,y,image_name): 66 self.x = x 67 self.y = y 68 self.screen = screen_temp 69 self.image = pygame.image.load(image_name) 70 71 def display(self): 72 self.screen.blit(self.image,(self.x,self.y)) 73 74 class Bullet(BaseBullet): 75 ‘‘‘玩家子弹‘‘‘ 76 def __init__(self,screen_temp,x,y): 77 BaseBullet.__init__(self,screen_temp,x+40,y-20,‘feiji/bullet.png‘) 78 79 def move(self): 80 self.y -= 20 81 82 def judge(self): 83 if self.y < 0: 84 return True 85 else: 86 return False 87 88 class EnemyBullet(BaseBullet): 89 ‘‘‘敌机子弹‘‘‘ 90 def __init__(self,screen_temp,x,y): 91 BaseBullet.__init__(self, screen_temp, x + 25, y + 40, ‘feiji/bullet1.png‘) 92 93 def move(self): 94 self.y += 10 95 96 def judge(self): 97 if self.y > 852: 98 return True 99 else: 100 return False 101 102 def key_control(hero_temp): 103 # 获取事件,比如按键等 104 for event in pygame.event.get(): 105 # 判断是否是点击了退出按钮 106 if event.type == QUIT: 107 print("exit") 108 exit() 109 # 判断是否是按下了键 110 elif event.type == KEYDOWN: 111 # 检测按键是否是a或者left 112 if event.key == K_a or event.key == K_LEFT: 113 print(‘left‘) 114 hero_temp.move_left() 115 116 # 检测按键是否是d或者right 117 elif event.key == K_d or event.key == K_RIGHT: 118 print(‘right‘) 119 hero_temp.move_right() 120 # 检测按键是否是空格键 121 elif event.key == K_SPACE: 122 print(‘space‘) 123 hero_temp.fire() 124 125 def main(): 126 #1.创建窗口 127 screen = pygame.display.set_mode((480,852),0,0) 128 #2 创建一个背景图片 129 background = pygame.image.load(‘./feiji/background.png‘) 130 #3 创建一个飞机对象 131 hero = HeroPlane(screen) 132 #4 创建一个敌机对象 133 enemy = EnemyPlane(screen) 134 135 while True: 136 #把背景图片放到窗口显示 137 screen.blit(background,(0,0)) 138 #显示玩家飞机到定义的(x,y)坐标位置 139 hero.display() #显示玩家飞机 140 enemy.display() #显示敌机 141 enemy.move() #玩家飞机移动 142 enemy.fire() #玩家飞机发射子弹 143 pygame.display.update() #刷新 144 #检测键盘,控制玩家飞机移动 145 key_control(hero) 146 time.sleep(0.05) 147 148 if __name__ == ‘__main__‘: 149 main()