- 帖子
- 2
- 精华
- 0
- 积分
- 18
- 阅读权限
- 10
- 注册时间
- 2015-9-28
- 最后登录
- 2015-10-6
|
下面是我改编了一点点的打飞机游戏,本意是:每次游戏只有5次机会(n<5),分数是累加的,最后一条命用完就输出 GAME OVER。可是写完之后发现,只玩5次可以实现,但是不能输出GEME OVER,请师父指点下我呗?多谢啦。
# -*- coding: cp936 -*-
import pygame
import random
from sys import exit
class Bullet(): #建一个子弹类
def __init__(self): #初始化位置
self.x = 0
self.y = -1
self.image = pygame.image.load('bullet.png').convert_alpha() #载入子弹图片
self.active = False #默认不激活
def move(self):
if self.active: #激活状态下向上移动
self.y -= 1
if self.y < 0: #当飞出屏幕就设为不激活
self.active = False
def reset(self): #重置子弹位置
mousex, mousey = pygame.mouse .get_pos() #获取鼠标位置,也就是飞机的位置
self.x = mousex - self.image.get_width()/2
self.y = mousey - self.image.get_height()/2
self.active = True #重新激活
class Enemy(): #创建敌机类
def reset(self): #初始化敌机位置
self.x = random.randint(50,400)
self.y = random.randint(-200,-50)
self.speed = random.random()/5 + 0.1
#print self.speed
def __init__(self): #初始化的时候调用reset()
self.reset()
self.image = pygame.image.load('enemy.png').convert_alpha()
def move(self):
if self.y < 800:
self.y += self.speed
else: #若飞机掉出屏幕则重置
self.reset()
class Plane():
def reset(self):
self.x = 200
self.y = 600
def __init__(self):
self.reset()
self.image = pygame.image.load('plane.png').convert_alpha()#载入飞机图片
def move(self):
x, y = pygame.mouse .get_pos() #子弹和飞机的位置都是由鼠标决定的
x -= self.image.get_width()/2
y -= self.image.get_height()/2
self.x = x
self.y = y
def checkhit(bullet, enemy): #命中目标函数
if (bullet.x > enemy.x and bullet.x < enemy.image.get_width() + enemy.x) and (
bullet.y > enemy.y and bullet.y < enemy.image.get_height() + enemy.y
):
enemy.reset()
bullet.active = False
return True
return False
def checkcrash(plane,enemy): #飞机与敌机相撞
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 (
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()
):
return True
return False
#以上代码都基本没有变化
pygame.init()
screen = pygame .display.set_mode((450, 800), 0, 32) #创建了一个窗口,其大小与背景图片相同
pygame.display .set_caption("Hello world")
background = pygame.image.load ('back.png').convert() #载入背景图片
plane = Plane()
bullets = [] #创建子弹list
for i in range(5):
bullets.append(Bullet())
count_b = len(bullets)
index_b = 0
interval_b = 0
enemies = [] #创建敌机list
for i in range(4):
enemies.append(Enemy())
gameover = False
n = 0 #记录游戏次数
flag = True #判断游戏是否结束的标志
score = 0
font = pygame.font.Font(None, 32)
while True :
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#判断在gameover状态下点击了鼠标
if n < 5: #最多只有5次复活机会
if gameover and event.type == pygame.MOUSEBUTTONUP:
plane.reset()
for e in enemies:
e.reset()
for b in bullets:
b.active = False
gameover = False
else:
flag = False
text = font.render('GAME OVER !!!', 1, (0,0,0))
screen.blit(text,(190,400))
break
if not flag: #判断是否结束游戏,输出GAME OVER
text = font.render('GAME OVER !!!', 1, (0,0,0))
screen.blit(text,(190,400))
continue
screen.blit(background, (0,0)) #画出背景图
if not gameover and n < 5:
interval_b -= 1
if interval_b < 0:
bullets[index_b].reset()
interval_b = 200
index_b = (index_b + 1) % count_b
for b in bullets:
if b.active:
for e in enemies:
if checkhit(b,e):
score += 100
b.move()
screen.blit(b.image, (b.x, b.y)) #画出子弹
for e in enemies:
if checkcrash(plane,e):
n += 1
gameover = True
e.move()
screen.blit(e.image, (e.x,e.y))
plane.move()
screen.blit(plane.image,(plane.x,plane.y)) #画出飞机 先画子弹再画飞机,使得看起来子弹像是从飞机里出来的
text = font.render('Score:%d' % score, 1, (0,0,0))
screen.blit(text,(0,0))
text1 = font.render('%d chances' % (5-n),1, (0,0,0))
screen.blit(text1,(300,0))
else:
text = font.render('Score:%d' % score, 1, (0,0,0))
screen.blit(text,(190,400))
pygame.display.update()
|
|