准备用python写一个打飞机的游戏,相信能够写完这个项目,我对python的学习应该也算可以了。
首先,我们要使用python的一个pygame的库来写,这个库的安装比较简单就是普通的pip安装就可以了。
简单的介绍一下这个库的使用,当然,是代码的方式:
import pygame import time def main(): #1. 创建窗口(无背景,大小与背景一样) screen = pygame.display.set_mode((480,852),0,32) #2. 创建一个背景图片() background = pygame.image.load("./img/background.png") while True:#不停的刷新感觉好像一直都在一样 screen.blit(background, (0,0)) pygame.display.update() time.sleep(0.01)#防cpu使用率过高
import pygame import time from pygame.locals import *#按键的左右控制 def main(): #1. 创建窗口 screen = pygame.display.set_mode((480,852),0,32) #2. 创建一个背景图片 background = pygame.image.load("./img/background.png") #3. 创建一个飞机图片 hero = pygame.image.load("./img/hero1.png") x = 210 y = 700 while True:#左上的位置是(0,0),下面坐标就是放置的位置 screen.blit(background, (0,0)) screen.blit(hero, (x, y)) pygame.display.update() # x+=1#让飞机动态动起来 # y-=1 # 获取事件,比如按键等 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') x -= 5 # 检测按键是否是d或者right elif event.key == K_d or event.key == K_RIGHT: print('right') x += 5 # 检测按键是否是空格键 elif event.key == K_SPACE: print('space') time.sleep(0.01)#防cpu使用率过高
有了飞机以后我们会多次操作游戏里的对象(fps的刷新导致图像的动态操作),所以我们把他封装成对象来操作更为方便。
# -*- coding:utf-8 -*- import pygame from pygame.locals import * import time class HeroPlane():#定义飞机类 def __init__(self, screen_temp):#实例化飞机对象 self.x = 210 self.y = 700 self.screen = screen_temp self.image = pygame.image.load("./img/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("./img/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,32) #2. 创建一个背景图片 background = pygame.image.load("./img/background.png") #3. 创建一个飞机对象 hero = HeroPlane(screen) while True: screen.blit(background, (0,0)) hero.display() pygame.display.update() key_control(hero) time.sleep(0.01) main()
有枪有炮,这个时候我们需要一个敌人来锤一波。
# -*- coding:utf-8 -*- import pygame from pygame.locals import * import time import random class HeroPlane():#定义飞机类 def __init__(self, screen_temp):#实例化飞机对象 self.x = 210 self.y = 700 self.screen = screen_temp self.image = pygame.image.load("./img/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 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("./img/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 EnemyPlane(): """敌机的类和我们的一样""" def __init__(self, screen_temp): self.x = 0 self.y = 0 self.screen = screen_temp self.image = pygame.image.load("./img/enemy0.png") self.direction = "right" # 用来存储飞机默认的显示方向 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(self): if self.direction == "right": self.x += 5 elif self.direction == "left": self.x -= 5 if self.x > 480 - 50: self.direction = "left" elif self.x < 0: self.direction = "right" def fire(self): random_num = random.randint(1,100) if random_num == 8 or random_num == 20: self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) 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("./img/bullet1.png") def display(self): self.screen.blit(self.image, (self.x, self.y)) def move(self): self.y+=5 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,32) #2. 创建一个背景图片 background = pygame.image.load("./img/background.png") #3. 创建一个飞机对象 hero = HeroPlane(screen) #4. 创建一个敌机 enemy = EnemyPlane(screen) while True: screen.blit(background, (0,0)) hero.display() enemy.display() enemy.move()#调用敌机的移动方法 enemy.fire() # 敌机开火 pygame.display.update() key_control(hero) time.sleep(0.01) main()
真的,代码超多的,我们来简化一点,用面向对象的继承,组合来解决这个问题。
# -*- coding:utf-8 -*- import pygame from pygame.locals import * import time import random class Base():#所有对象都要初始化位置 def __init__(self, screen_temp, x, y, image_name): self.x = x self.y = y self.screen = screen_temp self.image = pygame.image.load(image_name) class BasePlane(Base):#所有飞机特性抽象出来 def __init__(self, screen_temp, x, y, image_name): super().__init__(screen_temp, x, y, image_name) 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) class HeroPlane(BasePlane): def __init__(self, screen_temp): super().__init__(screen_temp, 210, 700, "./img/hero1.png") 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(BasePlane): """敌机的类""" def __init__(self, screen_temp): super().__init__(screen_temp, 0, 0, "./img/enemy0.png") self.direction = "right" # 用来存储飞机默认的显示方向 def move(self): if self.direction == "right": self.x += 5 elif self.direction == "left": self.x -= 5 if self.x > 480 - 50: self.direction = "left" elif self.x < 0: self.direction = "right" def fire(self): random_num = random.randint(1, 100) if random_num == 8 or random_num == 20: self.bullet_list.append(EnemyBullet(self.screen, self.x, self.y)) class BaseBullet(Base): def display(self): self.screen.blit(self.image, (self.x, self.y)) class Bullet(BaseBullet): def __init__(self, screen_temp, x, y): super().__init__(screen_temp, x + 40, y - 20, "./img/bullet.png") def move(self): self.y -= 20 def judge(self): if self.y < 0: return True else: return False class EnemyBullet(BaseBullet): def __init__(self, screen_temp, x, y): super().__init__(screen_temp, x + 25, y + 40, "./img/bullet1.png") def move(self): self.y += 5 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, 32) # 2. 创建一个背景图片 background = pygame.image.load("./img/background.png") # 3. 创建一个飞机对象 hero = HeroPlane(screen) # 4. 创建一个敌机 enemy = EnemyPlane(screen) while True: screen.blit(background, (0, 0)) hero.display() enemy.display() enemy.move() # 调用敌机的移动方法 enemy.fire() # 敌机开火 pygame.display.update() key_control(hero) time.sleep(0.01) if __name__ == "__main__": main()
然后,然后游戏就完成了。
PlayPlane/ |-- bin/ | |-- main.py 程序运行主体程序 |-- config/ | |-- settings.py 程序配置(例如: 游戏背景音乐的加载等) |-- material 程序素材放置(打飞机游戏素材放置) |-- ... |-- src/ 程序主体模块存放 | |-- __init__.py | |-- bullet.py 我方飞机发射子弹实现代码存放 | |-- enemy.py 敌方飞机实现代码存放 | |-- plane.py 我方飞机实现代码存放 |-- manage.py 程序启动文件 |-- README.md
可以说是非常的暴力,非常的简单。
这个因为今天翻到了刚开始学python的时候买的一本《从入门到实践的书》,最初也想写一个打飞机游戏一定很有意思,然后经过这么长的时间的学习,现在对python的代码也有了一定的认知,前段时间看到github上一位老哥做的打飞机,今天做了一下,发现挺简单的。希望不忘初心,再接再厉。时间有限后面我就不写了,那位飞机老哥的打飞机地址:
https://github.com/triaquae/jerkoff
晚上还要看视频,溜了溜了。