# coding:utf-8
# 先导入游戏中需要的工具包
import pygame
from pygame import *
import random, time
# 先检查pygame工具包
pygame.init()
# 创建窗口,并设计大小
window = pygame.display.set_mode((500, 600))
# 设置标题
pygame.display.set_caption("飞机大战")
# 加载图片
app_ico = 'res/app.ico'
app = pygame.image.load(app_ico)
# 把logo设置上去
pygame.display.set_icon(app)
# 加载背景图片
bg = pygame.image.load('res/img_bg_level_3(1).jpg')
# 加载英雄机图片
h = pygame.image.load('res/hero2.png')
# 加载子弹图片
b = pygame.image.load('res/bullet_8.png')
# 加载三种敌机图片
e1 = pygame.image.load('res/img-plane_2.png')
e2 = pygame.image.load('res/img-plane_4.png')
e3 = pygame.image.load('res/img-plane_7.png')
# 撞击后的图片
boom = pygame.image.load('res/bomb-2.png')
# 开始游戏图片
startGame = pygame.image.load('res/startGame.png')
# 结束游戏图片
over = pygame.image.load('res/over.png')
# 将图片等比例缩放
gameover = pygame.transform.scale(over, (343, 109))
# 游戏暂停图片
pause = pygame.image.load('res/game_pause_nor.png')
# 倒计时图片
t1 = pygame.image.load('res/1.jpg')
time1 = pygame.transform.scale(t1, (300, 300))
t2 = pygame.image.load('res/2.jpg')
time2 = pygame.transform.scale(t1, (300, 300))
t3 = pygame.image.load('res/3.jpg')
time3 = pygame.transform.scale(t1, (300, 300))
# 用来存储多个子弹对象
bullets = []
# 创建敌机列表,用来存储敌机对象
enemies = []
# 定义分数
score = 0
# 假设自己知道,1代表准备阶段,2代表开始阶段,3代表暂停阶段,4代表结束阶段
state = 1
# 设计一个模板类
class Model:
# 1.设计属性
def __init__(self, x, y, width, height, img):
self.x = x
self.y = y
self.width = width
self.height = height
self.img = img
# 2.设计功能
def paint(self):
window.blit(self.img, (self.x, self.y))
# 设计背景类
class Background(Model):
def paint(self):
# 把图片传输到窗口上的方法
window.blit(self.img, (self.x, self.y))
window.blit(self.img, (self.x, self.y - 600))
def move(self):
if self.y < 600:
self.y = self.y + 1
else:
self.y = 0
# 设计敌机类
class Enemy(Model):
def move(self):
self.y += 2
# 设计英雄机类
class Hero(Model):
def __init__(self, x, y, width, height, img, life):
Model.__init__(self, x, y, width, height, img)
self.life = life
def shoot(self):
# 把生成的子弹对象加入到子弹列表
bullets.append(Bullet(self.x + self.width / 2 - 10, self.y - 56 - 10, 20, 56, b))
def hit(self, who):
return (self.x + self.width > who.x and
who.x + who.width > self.x and
who.y + who.height > self.y and
self.y + self.height + who.y)
# 设计子弹类
class Bullet(Model):
# 让子弹每次移动2
def step(self):
self.y -= 2
def hit(self, who):
return (self.x + self.width > who.x and
who.x + who.width > self.x and
who.y + who.height > self.y and
self.y + self.height + who.y)
# 创建背景对象
background = Background(0, 0, 500, 600, bg)
# 创建英雄机对象
hero = Hero(196, 450, 120, 78, h, 3)
# 敌机生成方法
def enemyBirth():
# 把lastTime变成全局变量
global lastTime
if not isAction(interval, lastTime):
return
else:
lastTime = time.time()
# 假设本游戏有三种飞机
x1 = random.randint(0, 500 - 100)
x2 = random.randint(0, 400)
x3 = random.randint(0, 400)
n = random.randint(1, 10)
# 使用随机数控制三种飞机的生成概率
if n <= 6:
enemies.append(Enemy(x1, 0, 100, 68, e1))
elif 6 < n <= 9:
enemies.append(Enemy(x2, 0, 100, 68, e2))
else:
enemies.append(Enemy(x3, 0, 100, 68, e3))
# 时间间隔方法
interval = 0.5 # 表示时间间隔
lastTime = 0 # 表示上一次时间的初始值
def isAction(interval, lastTime):
# 先判断是不是第一次
if lastTime == 0:
return True
# 用time.time()可以记录实时的时间
current = time.time()
return current - lastTime >= interval
# 专门定义一个方法,来处理碰撞的事情
def checkHit():
global score, state, bomb, bx, by
# 英雄机和敌机
for enemy in enemies:
if hero.hit(enemy): # 只能得到True or False
bx = enemy.x
by = enemy.y
enemies.remove(enemy)
bomb = 2
hero.life -= 1
if hero.life <= 0:
hero.x = 196
hero.y = 450
hero.life = 0
state = 4
for bullet in bullets:
if bullet.hit(enemy):
enemies.remove(enemy)
score += 5
bullets.remove(bullet)
# 写文字方法
def fillText(text, postion):
# pygame.font.SysFont('宋体',30)
# 设置字体样式及大小
TextFont = pygame.font.Font('res/font1.ttf', 30)
# 渲染方法 写什么 抗锯齿
TF = TextFont.render(text, True, (255, 255, 255))
# 把设置好样式的文本传输到窗口上
window.blit(TF, postion)
# 暂停
def mouseOut(mx, my):
if mx <= 0 or mx >= 495 or my <= 0 or my >= 595:
return True
else:
return False
def mouseOver(mx, my):
if mx > 0 or mx < 495 or my > 0 or my < 595:
return True
else:
return False
# 定义事件处理方法
def handleEvent():
global state
# 点击叉叉关闭窗口
# 获取窗口上所有的事件
list = pygame.event.get()
# 获取每一个事件
for event in list:
# 如果事件类型==退出事件
if event.type == QUIT:
# 关闭pygame窗口
pygame.quit()
# 关闭程序
exit()
# 如果事件类型等于鼠标移动事件
if event.type == pygame.MOUSEMOTION:
if state == 2:
if mouseOut(event.pos[0], event.pos[1]):
state = 3
# 飞机的x坐标,event.pos[0]表示鼠标的x坐标
# event.pos[1]表示鼠标y坐标
hero.x = event.pos[0] - hero.width / 2
hero.y = event.pos[1] - hero.height / 2
elif state == 3:
if mouseOver(event.pos[0], event.pos[1]):
state = 2
# 如果鼠标被按下了
if event.type == pygame.MOUSEBUTTONDOWN:
# 而且是鼠标左键被按下了,左键代表一
if event.button == 1:
if state == 1:
if 164 <= event.pos[0] <= 335 and 300 <= event.pos[1] <= 345:
state = 6
elif state == 2:
hero.shoot()
# 静态
def cPaint():
fillText('score:' + str(score), (20, 20))
fillText('life:' + str(hero.life), (430, 20))
hero.paint()
for enemy in enemies:
enemy.paint()
# 遍历子弹列表,得到每一颗子弹
for bullet in bullets:
bullet.paint()
# 动态
def cStep():
for enemy in enemies:
enemy.move()
if enemy.y >= 600:
enemies.remove(enemy)
# 遍历子弹列表,得到每一颗子弹
for bullet in bullets:
bullet.step()
if bullet.y <= 0:
bullets.remove(bullet)
# 时间间隔方法
timeCell = 1 # 表示时间间隔
finalTime= 0 # 表示上一次时间的初始值
# 定义控制状态方法
def controlState():
global state
if state == 1:
window.blit(startGame, (164, 300))
elif state == 2:
# 调用敌机生成方法
enemyBirth()
# 调用碰撞检测方法
checkHit()
cPaint()
cStep()
elif state == 3:
cPaint()
window.blit(pause, (220, 277))
elif state == 4:
hero.paint()
window.blit(gameover, (78, 250))
elif state==6:
totalTime=3
while totalTime > 0:
print('还剩%d秒' % totalTime)
if totalTime == 3:
window.blit(time3, (100, 150))
elif totalTime == 2:
window.blit(time2, (100, 150))
elif totalTime == 1:
window.blit(time1, (100, 150))
time.sleep(1)
totalTime -= 1
state=2
while True:
# 背景图片用画画方法
background.paint()
background.move()
controlState()
# 刷新窗口/屏幕
pygame.display.update()
handleEvent()
# 延迟刷新
pygame.time.delay(10)
为什么这样设计图片不会显示出来