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
bird.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, bird
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 = []
bird = Hero(50, 100, 75, 65, 1, h)
state = START
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
bird.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(bird) or bird.y > sky.height or bird.y < 0 - bird.height:
state = OVER
if e.x < 0 - e.width:
enemys.remove(e)
break
if e.x < bird.x + bird.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:
bird.paint()
bird.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)