本文主要是介绍python飞机大战版管道鸟(简易) pygame单线程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
python飞机大战版管道鸟(简易)
相信当年的管道鸟,令许多的暴躁老哥砸了手机.
突发奇想把管道鸟和飞机大战结合会变成什么样呢?
还是老样子素材我就不提供了
# coding:utf-8
import random
import timeimport pygame
from pygame.locals import *# 初始化pygame环境
pygame.init()
pygame.font.init()
enemyTime = 0
score = 0
START = 0
LIVE = 1
OVER = 2
state = START# 创建一个长宽分别为480/650窗口
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 650
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))# 设置窗口标题
pygame.display.set_caption("飞机大战")
bg = pygame.image.load("images/bg1.png")
start = pygame.image.load("images/startGame.png")
over = pygame.image.load("images/again.png")
enemy = pygame.image.load("images/enemy1.png")
e1 = pygame.transform.rotate(enemy, -90)
hero = pygame.image.load("images/hero.png")
h = pygame.transform.rotate(hero, -90)# 父类
class FltObj():def __init__(self, x, y, width, height, img):self.x = xself.y = yself.width = widthself.height = heightself.img = imgdef hit(self, obj):x1 = self.x - obj.widthx2 = self.x + self.widthy1 = self.y - obj.heighty2 = self.y + self.heightreturn obj.x > x1 and obj.x < x2 and obj.y > y1 and obj.y < y2def paint(self):screen.blit(self.img, (self.x, self.y))# 地人类
class Enemy(FltObj):def __init__(self, x, y, width, height, life, type, img):FltObj.__init__(self, x, random.randint(y, sky.height - height), width, height, img)self.life = lifeself.type = typeself.flag = Truedef paint(self):screen.blit(self.img, (self.x, self.y))def move(self):self.x -= 3# 背景类
class Sky(FltObj):def __init__(self, x, y, width, height, img):FltObj.__init__(self, x, y, width, height, img)def paint(self):screen.blit(self.img, (self.x, self.y))# 英雄类
class Hero(FltObj):def __init__(self, x, y, width, height, life, img):FltObj.__init__(self, x, y, width, height, img)self.life = lifeself.leap = Falseself.stepY = 0# 画英雄def paint(self):angle = self.stepY * -1 * 6hero = pygame.transform.rotate(self.img, angle)screen.blit(hero, (self.x, self.y))# 英雄移动def move(self):self.y += self.stepYif self.stepY <= 15:self.stepY += 0.3if self.leap:self.stepY = -7bird.leap = False# 实例化用到的对象
sky = Sky(0, 0, 480, 852, bg)
bird = Hero(50, 100, 65, 75, 1, h)
enemys = []
TEXT_FONT = pygame.font.SysFont("console", 30, True)# 事件处理器
def handleEvent():global state, enemys, birdfor event in pygame.event.get():if event.type == QUIT:pygame.quit()exit()elif event.type == pygame.MOUSEBUTTONDOWN:if state == START:state = LIVEelif state == OVER:enemys = []bird = Hero(50, 100, 75, 65, 1, h)state = STARTelif event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE:bird.leap = True# 添加敌人方法
def addEnemy():global enemyTimeif enemyTime % 30 == 0:rnum = random.randint(0, 5)if rnum == 0:enemys.append(Enemy(sky.width, 0, 50, 60, 1, "小敌机", e1))elif rnum == 1:enemys.append(Enemy(sky.width, 0, 50, 60, 1, "小敌机", e1))else:enemys.append(Enemy(sky.width, 0, 50, 60, 1, "小敌机", e1))# 碰撞处理器
def hit():global state, scorefor e in enemys:if e.hit(bird) or bird.y > sky.height or bird.y < 0 - bird.height:state = OVERif e.x < 0 - e.width:enemys.remove(e)breakif e.x < bird.x + bird.width and e.flag:score += 1e.flag = False# 执行程序
while True:sky.paint()hit()sc = TEXT_FONT.render("Score:{}".format(score), True, (255, 255, 0))screen.blit(sc, (10, 10))if state == START:screen.blit(start, (150, SCREEN_HEIGHT / 2.5))elif state == LIVE:bird.paint()bird.move()addEnemy()for ene in enemys:ene.paint()ene.move()enemyTime += 1elif state == OVER:screen.blit(over, (150, SCREEN_HEIGHT / 2.5))# 更新屏幕内容pygame.display.update()# 处理关闭游戏handleEvent()time.sleep(0.01)
这篇关于python飞机大战版管道鸟(简易) pygame单线程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!