过去几天,我一直在梳理头发,以使愚蠢的菜单屏幕正常工作。
游戏运行正常,当我第一次尝试调用菜单功能时,我什么都没显示。
import pygame
import time
import random
from pygame.locals import *
pygame.init()
pygame.font.init()
pygame.mixer.init()
screen = pygame.display.set_mode((540,620))
pygame.display.set_caption("Space Invaders")
clock = pygame.time.Clock()
current_screen = 0
White = (255,255,255)
Black = (0,0,0)
Width = 540
Height = 620
def draw_text(surf, text, size, x, y):
font = pygame.font.Font("CaviarDreams.ttf", size)
text_surface = font.render(text, True, White)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
def game_over():
keep_going = False
screen.fill(Black)
draw_text(screen, ("Game Over!"), 40, Width /2, 280)
pygame.draw.rect(screen, Black, (30,200,50,30))
draw_text(screen, ("Play again"), 30, 200, 350)
def paused():
screen.fill(Black)
draw_text(screen, ("Paused!"), 40, Width /2, 280) #Pause
resume_button = pygame.draw.rect(screen, White, (150,350,100,30))
def menu():
screen.fill(Black)
draw_text(screen, ("Space Invaders!"), 50, 270, 180)
draw_text(screen, ("Press Space to play"), 40, 270, 240)
for ev in pygame.event.get():
if ev.type == K_SPACE:
screen.fill(Black)
current_screen = 1
class Ship(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("spaceship.bmp")
self.rect = self.image.get_rect()
self.rect.centerx = Width / 2
self.rect.bottom = Height - 10
self.speedx = 0
def update(self):
self.speedx = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -5
if keystate[pygame.K_RIGHT]:
self.speedx = 5
self.rect.x += self.speedx
if self.rect.right > screen.get_width():
self.rect.right = screen.get_width()
if self.rect.left < 0:
self.rect.left = 0
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
pygame.mixer.Sound.play(pew)
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("laser.bmp")
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.speedy = -10
def update(self):
self.rect.y += self.speedy
if self.rect.bottom < 0:
self.kill()
class Invaders(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("alien.bmp")
self.rect = self.image.get_rect()
self.rect.x = random.randrange(Width - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(3, 8)
self.speedx = random.randrange(-3, 3)
def update(self):
self.rect.x += self.speedx
self.rect.y += self.speedy
if self.rect.top > Height + 10 or self.rect.left < -25 or self.rect.right > Width + 20:
self.rect.x = random.randrange(Width - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
pew = pygame.mixer.Sound("Laser.wav")
player_death = pygame.mixer.Sound("Explosion.wav")
hit_sound = pygame.mixer.Sound("Hit.wav")
score = 0
all_sprites = pygame.sprite.Group()
enemies = pygame.sprite.Group()
bullets = pygame.sprite.Group()
player = Ship()
all_sprites.add(player)
enemy = Invaders()
for i in range(5):
basic_enemy = Invaders()
all_sprites.add(basic_enemy)
enemies.add(basic_enemy)
def game_loop():
current_screen = 0
score = 0
keep_going = True
while keep_going:
screen.fill(Black)
clock.tick(60)
if current_screen 0
menu()
if current_screen == 1:
for ev in pygame.event.get():
if ev.type == pygame.QUIT:
quit()
elif ev.type == pygame.KEYDOWN:
if ev.key == pygame.K_SPACE:
player.shoot()
elif ev.key == pygame.K_ESCAPE:
paused()
if ev.key == pygame.K_ESCAPE:
keep_going = True
all_sprites.update()
hits = pygame.sprite.groupcollide(enemies, bullets, True, True)
for hit in hits:
score += 100
pygame.mixer.Sound.play(hit_sound)
basic_enemy = Invaders()
all_sprites.add(basic_enemy)
enemies.add(basic_enemy)
hits = pygame.sprite.spritecollide(player, enemies, False)
if hits:
pygame.mixer.Sound.play(player_death)
keep_going = False
game_over()
all_sprites.draw(screen)
draw_text(screen, str(score), 20, 500,600)
pygame.display.flip()
game_loop()