import random
import time
import pygame
import sys
import plane
import enemy
def key_control(hero):
global game_start
for shi_jian in pygame.event.get():
if shi_jian.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif shi_jian.type == pygame.KEYDOWN:
if shi_jian.key == pygame.K_a :
hero.move_left()
if shi_jian.key == pygame.K_d:
hero.move_right()
if shi_jian.key == pygame.K_w:
hero.move_up()
if shi_jian.key == pygame.K_s:
hero.move_down()
if shi_jian.key == pygame.K_f:
hero.fire()
if shi_jian.key == pygame.K_k:
game_start = False
def set_ziti(window, content, ziti_size, xy, color = (0,0,0)):
font = pygame.font.Font("ziti.ttf", ziti_size)
imgText = font.render(content, True, color)
window.blit(imgText, xy)
def main():
global game_start
pygame.init()
window = pygame.display.set_mode((400, 600))
pygame.display.set_caption("星球大战")
background = pygame.image.load("图片/background.png")
hero = plane.plane(window)
bad_egg = []
pygame.key.set_repeat(100, 1)
score = 0
game_start = True
game_over = False
while True:
window.blit(background, (0, 0))
key_control(hero)
if game_start:
set_ziti(window, "开始游戏",66, (75, 270))
set_ziti(window, "按k键开始游戏",30, (120,350),(100,0,0))
elif game_over:
pass
else:
num_bad_egg = random.randint(1, 50)
if num_bad_egg == 30:
bad_egg.append(enemy.enemy(window))
zidan_x, zidan_y = hero.show()
for e in bad_egg:
e.show()
e.move()
if hero.x < e.x < hero.x + 120 and hero.y < e.y < hero.y + 100:
hero.boom()
game_over = True
for zi_i in range(len(zidan_x)):
if e.x < zidan_x[zi_i] < e.x + 51 and e.y < zidan_y[zi_i] < e.y + 39:
e.boom()
try:
bad_egg.remove(e)
score = score + 1
except:
pass
set_ziti(window, "分数%d"%score, 30, (0, 0))
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()