python飞机大战版管道鸟(简易) pygame单线程

python飞机大战版管道鸟(简易)

相信当年的管道鸟,令许多的暴躁老哥砸了手机.
突发奇想把管道鸟和飞机大战结合会变成什么样呢?
还是老样子素材我就不提供了

# coding:utf-8
import random
import time

import 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 = x
        self.y = y
        self.width = width
        self.height = height
        self.img = img

    def hit(self, obj):
        x1 = self.x - obj.width
        x2 = self.x + self.width
        y1 = self.y - obj.height
        y2 = self.y + self.height
        return obj.x > x1 and obj.x < x2 and obj.y > y1 and obj.y < y2

    def 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 = life
        self.type = type
        self.flag = True

    def 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 = life
        self.leap = False
        self.stepY = 0

    # 画英雄
    def paint(self):
        angle = self.stepY * -1 * 6
        hero = pygame.transform.rotate(self.img, angle)
        screen.blit(hero, (self.x, self.y))

    # 英雄移动
    def move(self):
        self.y += self.stepY
        if self.stepY <= 15:
            self.stepY += 0.3
        if self.leap:
            self.stepY = -7
            brid.leap = False


# 实例化用到的对象
sky = Sky(0, 0, 480, 852, bg)
brid = Hero(50, 100, 65, 75, 1, h)
enemys = []
TEXT_FONT = pygame.font.SysFont("console", 30, True)


# 事件处理器
def handleEvent():
    global state, enemys, brid
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if state == START:
                state = LIVE
            elif state == OVER:
                enemys = []
                brid = Hero(50, 100, 75, 65, 1, h)
                state = START
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                brid.leap = True


# 添加敌人方法
def addEnemy():
    global enemyTime
    if 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, score
    for e in enemys:
        if e.hit(brid) or brid.y > sky.height or brid.y < 0 - brid.height:
            state = OVER
        if e.x < 0 - e.width:
            enemys.remove(e)
            break
        if e.x < brid.x + brid.width and e.flag:
            score += 1
            e.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:
        brid.paint()
        brid.move()
        addEnemy()
        for ene in enemys:
            ene.paint()
            ene.move()
        enemyTime += 1
    elif state == OVER:
        screen.blit(over, (150, SCREEN_HEIGHT / 2.5))
    # 更新屏幕内容
    pygame.display.update()
    # 处理关闭游戏
    handleEvent()
    time.sleep(0.01)


发布了3 篇原创文章 · 获赞 0 · 访问量 80

猜你喜欢

转载自blog.csdn.net/u011245132/article/details/105553902
今日推荐