打飞机小游戏 python+pygame
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了打飞机小游戏 python+pygame相关的知识,希望对你有一定的参考价值。
首先安装python和pygame的环境
sudo apt0get install python
sudo apt-get install python-pygame
代码中有好几个图,自己找
1 # -*- coding: utf-8 -*- 2 import pygame 3 import random 4 from sys import exit 5 #定义敌机类 6 class Enemy: 7 def restart(self): #重置敌机的位置和速度 8 self.x = random.randint(50, 400) 9 self.y = random.randint(-200, -50) 10 self.speed = random.random()+0.1 11 def __init__(self): #初始化 12 self.restart() 13 self.image = pygame.image.load(‘enemy.png‘).convert_alpha() 14 def move(self): #向下移动 15 if self.y < 800 : 16 self.y +=self.speed 17 else : #重置 18 self.restart() 19 20 #定义Bullet类。封装子弹相关的数据和方法 21 class Bullet: 22 def __init__(self): #初始变量,两个_,该类被创建时会被自动,不然会出现AttributeError: Bullet instance has no attribute 23 self.x = 0 24 self.y = -1 25 self.image = pygame.image.load(‘bullet.png‘).convert_alpha() 26 self.active = False #默认不激活 27 def move(self): #处理子弹的运动 28 if self.active: #激活状态下,向上移动 29 self.y -= 3 30 if self.y < 0: 31 self.active = False 32 def restart(self): #重置子弹位置 33 mouseX, mouseY = pygame.mouse.get_pos() 34 self.x = mouseX - self.image.get_width()/2 35 self.y = mouseY - self.image.get_height()/2 36 self.active = True #激活子弹 37 #定义飞机类 38 class Plane: 39 def restart(self): 40 self.x = 200 41 self.y = 300 42 def __init__(self): 43 self.restart() 44 self.image = pygame.image.load(‘plane.png‘).convert_alpha() 45 def move(self): 46 x,y = pygame.mouse.get_pos() #获取鼠标的位置 47 x -= self.image.get_width() / 2 #使得鼠标在飞机图像的最中间 48 y -= self.image.get_height() / 2 49 self.x = x 50 self.y = y 51 52 def checkHit(enemy, bullet): #检测子弹和飞机是否相撞 53 if(bullet.x > enemy.x and bullet.x < enemy.x+enemy.image.get_width()) and 54 (bullet.y > enemy.y and bullet.y < enemy.y+enemy.image.get_height()): #空格+\为代码换行 55 enemy.restart() 56 bullet.active = False 57 return True 58 return False 59 60 def checkCrash(enemy, plane):#检测敌机与飞机是否相撞 61 if (plane.x + 0.7*plane.image.get_width() > enemy.x) and (plane.x + 0.3*plane.image.get_width() < enemy.x + enemy.image.get_width()) and 62 (plane.y + 0.7*plane.image.get_height() > enemy.y) and (plane.y + 0.3*plane.image.get_height() < enemy.y + enemy.image.get_height()): 63 return True 64 return False 65 66 pygame.init() 67 screen = pygame.display.set_mode((450,700), 0, 32) #制作窗口 68 pygame.display.set_caption(‘fight plane‘) #窗口名称 69 background = pygame.image.load(‘background.jpg‘).convert() #加载背景图 70 plane = Plane() #创建飞机对象 71 enemies = [] #创建敌机list 72 for i in range(5): 73 enemies.append(Enemy()) 74 bullets = [] #创建子弹list 75 for i in range(5): #list中添加5发子弹 76 bullets.append(Bullet()) 77 count_b = len(bullets) #子弹总数 78 index_b = 0 #即将激活的子弹序号 79 interval_b = 0 #发射子弹间隔 80 gameover = False 81 score = 0 82 font = pygame.font.Font(None,32) #创建一个font对象,None为默认字体,32为字号 83 while True: 84 for event in pygame.event.get(): 85 if event.type == pygame.QUIT: 86 pygame.quit() 87 exit() 88 if gameover and event.type == pygame.MOUSEBUTTONUP: #点击鼠标游戏重置 89 plane.restart() 90 for e in enemies : 91 e.restart() 92 for b in bullets : 93 b.active = False 94 score = 0 95 gameover = False 96 index_b = 0 97 interval_b = 0 98 screen.blit(background,(0,0)) #画背景图像,从最左上角开始即(0,0) 99 100 if not gameover : 101 interval_b -= 1 #发射间隔递减,实际发射间隔受cpu影响 102 if interval_b < 0: #间隔小于0时,激发一个子弹 103 bullets[index_b].restart() 104 interval_b = 100 #重置时间间隔 105 index_b = (index_b + 1) %count_b #子弹序号周期性递减 106 for b in bullets: #绘制激活的子弹 107 if b.active: 108 b.move() 109 screen.blit(b.image,(b.x,b.y)) 110 for e in enemies: 111 if checkHit(e, b) :#检测激活的子弹是否和飞机相撞 112 score += 10 113 for e in enemies : #敌机的移动和描述 114 if checkCrash(e, plane) : 115 gameover = True 116 e.move() 117 screen.blit(e.image,(e.x, e.y)) 118 plane.move() 119 screen.blit(plane.image,(plane.x,plane.y)) #画飞机 120 text = font.render("Score:%d" %score,1,(0,0,0)) 121 screen.blit(text,(0,0)) #在屏幕左上角显示分数 122 else : 123 text1 = font.render("Score:%d" %score,1,(0,0,0)) 124 text2 = font.render("click restart",1,(0,0,0)) 125 screen.blit(text1,(190,400)) #在屏幕中间显示分数 126 screen.blit(text2,(170,420)) 127 pygame.display.update() #刷新
以上是关于打飞机小游戏 python+pygame的主要内容,如果未能解决你的问题,请参考以下文章