用户:
执着的胆小菇tCrJ查看:0 回复:0 评论:0 创建时间:2021-02-28T17:06:59
# 导入库
import pgzrun
import random
# 宽高
WIDTH = 450
HEIGHT = 650
# 分数
score = 0
# 游戏模式
game_state = "start"
# 是否发喵弹
shoot = False
# 速度
t = 5
# 等级
game_integral = 0
# 血量
life = 1
# 创建角色
ship = Actor("ship")
alien = Actor("alien")
bullet = Actor("bullet")
# 角色坐标
ship.pos = WIDTH // 2, HEIGHT - 60
alien.pos = WIDTH // 2, 50
# 飞船及外星人控制
def update():
global game_state, shoot, score, t, game_integral, life, count
if game_state == "play":
if keyboard.a:
ship.x -= t
elif keyboard.d:
ship.x += t
elif ship.x >= WIDTH - 30:
ship.x = WIDTH - 30
elif ship.x <= 20:
ship.x = 20
alien.y += t
if keyboard.w:
shoot = True
sounds.bullet.play()
initpos = ship.x, ship.y - 60
bullet.pos = initpos[0], initpos[1]
if shoot:
if bullet.y > -20:
bullet.y -= 10
else:
shoot = False
bullet.x = -160
if alien.collidepoint(bullet.pos):
alien_place(alien)
score += 10
if alien.collidepoint(ship.pos):
life -= 1
if alien.y >= HEIGHT:
alien_place(alien)
if score == 1000:
game_integral = 1
elif score == 2000:
game_integral = 2
elif score == 3000:
game_integral = 3
elif score == 4000:
game_integral = 4
elif score == 5000:
game_integral = 5
if life == 0:
game_state = "game_over"
# 位置
def alien_place(obj):
obj.x = random.randint(0 + 20, WIDTH - 20)
obj.y = random.randint(0 - 50, HEIGHT // 2)
# 显示角色
def draw():
global game_state, life, diamond, heart
if game_state == "start":
screen.fill("sky blue")
screen.draw.text("Alien is coming!!!", (WIDTH // 2 -
175, HEIGHT // 2 - 80), fontsize=60)
screen.draw.text("Press SPACE to start!",
(WIDTH // 2 - 165, HEIGHT // 2), fontsize=40)
if keyboard.space:
game_state = "play"
elif game_state == "play":
screen.fill("sky blue")
screen.draw.text("score:" + str(score), (10, 10), fontsize=30)
screen.draw.text("YOU GRADE:" + str(game_integral),
(WIDTH // 2 - 100, 10), fontsize=30)
screen.draw.text("life:" + str(life),
(WIDTH - 60, 10), fontsize=30)
sounds.bg_music.play()
ship.draw()
alien.draw()
bullet.draw()
elif game_state == "game_over":
screen.fill("sky blue")
screen.draw.text("GAME OVER", (WIDTH // 2 - 145,
HEIGHT // 2 - 80), fontsize=60)
screen.draw.text("YOUR SCORE:" + str(score),
(WIDTH // 2 - 130, HEIGHT // 2 - 10), fontsize=30)
screen.draw.text("YOU GRADE:" + str(game_integral),
(WIDTH // 2 - 125, HEIGHT // 2 + 40), fontsize=30)
elif game_state == "win":
screen.fill("sky blue")
screen.draw.text("YOU WIN!!!", (WIDTH // 2 - 170,
HEIGHT // 2 - 80), fontsize=60)
screen.draw.text("YOUR SCORE:" + str(score),
(WIDTH // 2 - 170, HEIGHT // 2), fontsize=40)
screen.draw.text("YOU GRADE:" + str(game_integral),
(WIDTH // 2 - 170, HEIGHT // 2 - 60), fontsize=30)
# 使代码正常运行
pgzrun.go()