# -*- coding: utf-8 -*- import pygame from sys import exit import random pygame.init() screen = pygame.display.set_mode((450, 600), 0, 32) background = pygame.image.load("back.jpg").convert() plane = pygame.image.load("plane.png").convert_alpha() pygame.display.set_icon(plane) pygame.display.set_caption("打飞机") #pygame.mouse.set_visible(False) class Enemy: def restart(self): #重置敌机位置和速度 self.x = random.randint(50, 400) self.y = random.randint(-200, -50) self.speed = random.random() + 0.1 def __init__(self): #初始化 self.restart() self.image = pygame.image.load('enemy.png').convert_alpha() def move(self): if self.y < 600: #向下移动 self.y += 0.3 else: #重置 self.restart() #enemy = Enemy() enemies = [] for i in range(3): enemies.append(Enemy()) class Bullet: def __init__(self): #初始化成员变量,x,y,image self.x = 0 self.y = -1 self.image = pygame.image.load('bullet.png').convert_alpha() self.active = False def restart(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 def move(self): #处理子弹的运动 if self.y < 0: self.active = False if self.active: self.y -= 3 #创建子弹的list bullets = [] #向list中添加5发子弹 for i in range(5): bullets.append(Bullet()) #子弹总数 count_b = len(bullets) #即将激活的子弹序号 index_b = 0 #发射子弹的间隔 interval_b = 0 class Plane: def restart(self): self.x = 200 self.y = 600 def __init__(self): self.restart() 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 plane = Plane() bullet = Bullet() enemy = Enemy() def checkHit(enemy, bullet): if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and (bullet.y > enemy.y and bullet.y < enemy.y + enemy.image.get_height()): enemy.restart() bullet.active = False return True return False def checkCrash(enemy, plane): 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 gameover = False score = 0 Hscore = [0] font = pygame.font.SysFont('楷体', 16) text1 = font.render(u"点击鼠标左键重新开始", 1, (0, 0, 0)) text2 = font.render(u"HScore:0", 1, (0, 0, 0)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() #判断在gameover状态下点击了鼠标 if gameover and event.type == pygame.MOUSEBUTTONUP: #重置游戏 plane.restart() for e in enemies: e.restart() for b in bullets: b.active = False score = 0 gameover = False screen.blit(background, (0,0)) if not gameover: interval_b -= 1 if interval_b < 0: bullets[index_b].restart() interval_b = 100 index_b = (index_b+1) % count_b for b in bullets: if b.active: for e in enemies: if checkHit(e, b): score += 100 if checkCrash(e, plane): gameover = True b.move() screen.blit(b.image, (b.x-23, b.y)) screen.blit(b.image, (b.x+25, b.y)) screen.blit(b.image, (b.x, b.y)) for e in enemies: e.move() screen.blit(e.image, (e.x, e.y)) plane.move() screen.blit(plane.image, (plane.x, plane.y)) text = font.render("Socre:%d" % score, 1, (0, 0, 0)) else: #screen.blit(text, (0, 0)) screen.blit(text1,(110,40)) Hscore.append(score) text2 = font.render(u"HScore:%d" % sorted(Hscore)[-1], 1, (0, 0, 0)) pass screen.blit(text, (0, 0)) screen.blit(text2, (0, 20)) pygame.display.update()