- 帖子
- 7
- 精华
- 0
- 积分
- 47
- 阅读权限
- 10
- 注册时间
- 2018-11-15
- 最后登录
- 2018-12-4
|
学Python两个多星期,看很多贴说要多独立打些代码,所以上星期看完pygame课程,这星期试着自己重写了一遍,部分代码跟先生不一样,纯粹纪念一下,欢迎高手指点指点。
整个做完感觉敌机一击就死没啥意思,所以敌机类里多了个血量的变量,而且速度越快的血量越少。
*****************************************************************************************************************************
import pygame
from sys import exit
import random
#飞机类
class Plane:
def __init__(self):
self.image = pygame.image.load('plane.png').convert_alpha()
self.x = 0
self.y = -1
def move(self):
x, y = pygame.mouse.get_pos()
self.x = x - self.image.get_width()/2
self.y = y - self.image.get_height()/2
#子弹类
class Bullet:
def __init__(self):
self.image = pygame.image.load('bullet.png').convert_alpha()
self.x = 0
self.y = -1
self.state = False
def move(self):
if self.state:
if self.y > -10:
self.y -= 1
else:
self.state = False
else:
self.y = -10
def restart(self):
self.state = True
self.x, self.y = pygame.mouse.get_pos()
self.y -= plane.image.get_height()/2-5
#敌机类
class Enemy:
def __init__(self):
self.image = pygame.image.load('enemy.png').convert_alpha()
self.x = random.uniform(0,450 - self.image.get_width())
self.y = -self.image.get_height()
self.speed = random.random()
# 此处增加了个功能,敌机状态用血量衡量
# self.state = False
self.blood = 0
def move(self):
if self.blood > 0:
if self.y < 850:
self.y += self.speed
else:
self.blood = 0
else:
self.y = -100
def restart(self):
# self.state = True
self.x = random.uniform(0, 450 - self.image.get_width())
self.y = -self.image.get_height()
self.speed = random.random()
# 速度越快的血量越少
self.blood = 5 - int(self.speed * 10/2)
def hit(e, b):
if e.x < b.x < e.x + e.image.get_width() and e.y < b.y < e.y + e.image.get_height():
e.blood -= 1
b.state = False
if e.blood == 0:
return 1
return 0
def crash(e, p):
if (e.x - 0.7*p.image.get_width() < p.x
< e.x + e.image.get_width() - 0.3*p.image.get_width()
and e.y - 0.7*p.image.get_height() < p.y
< e.y + e.image.get_height() - 0.3*p.image.get_height()):
return True
return False
pygame.init()
screen = pygame.display.set_mode((450,800),0,32)
pygame.display.set_caption('Plane game')
background = pygame.image.load('background.jpg').convert()
plane = Plane()
bullets = []
for i in range(10):
bullets.append(Bullet())
b_interval = 0
b_num = 0
enemies = []
num = 10
for i in range(num):
enemies.append(Enemy())
e_interval = 0
e_num = 0
scores = 0
font = pygame.font.Font(None,32)
gameover = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if gameover and event.type == pygame.MOUSEBUTTONDOWN:
gameover = False
scores = 0
for e in enemies:
e.blood = 0
for b in bullets:
b.state = False
screen.blit(background,(0,0))
if not gameover:
if b_interval == 0:
bullets[b_num].restart()
b_num = (b_num + 1) % 10
b_interval = (b_interval + 1) % 100
for b in bullets:
b.move()
screen.blit(b.image,(b.x,b.y))
#此处逻辑跟先生不同,先生保证屏幕有固定的敌机数,
#我这里还是用子弹的逻辑,敌机出现的间隔保持一致
if e_interval == 0:
if enemies[e_num].blood == 0:
enemies[e_num].restart()
e_num = (e_num + 1) % num
e_interval = (e_interval + 1) % 100
for e in enemies:
if e.blood > 0:
for b in bullets:
if b.state:
scores += hit(e, b)
if crash(e, plane):
gameover = True
e.move()
screen.blit(e.image,(e.x,e.y))
text = font.render('Scores:%d' % scores, True, (0, 0, 0))
screen.blit(text, (0, 0))
plane.move()
screen.blit(plane.image,(plane.x,plane.y))
else:
text = font.render('Scores:%d' % scores, True, (0, 0, 0))
screen.blit(text, (180, 300))
pygame.display.update()
|
|