如何在游戏中添加2级?

时间:2016-06-11 03:18:43

标签: python pygame

我正在制作类似太空入侵者的游戏。我已经完成了第一个第一级,有4行10个外星人。我想添加另一个级别,其中有4行17个外星人使其变得困难并缩短时间限制。我已经创建了一个单独的模块名称'LEVEL2',这是我在第一级完成后尝试导入和运行的。有人可以帮我解决这个问题。

提前致谢

主要游戏(第1级):

.save()

这是第2级:

import pygame, random, sys
from time import sleep
from pygame.locals import *
import subprocess
import LEVEL2

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
NAVYBLUE = (0,0,45)
GREY = (128,128,128)
screen_width = 900
screen_height = 650

pygame.init()
OpenScreen = pygame.display.set_mode([screen_width, screen_height])


screen = pygame.display.set_mode([screen_width, screen_height])
class Block(pygame.sprite.Sprite):
    def __init__(self, image):
        aliens = pygame.image.load('aliens.png')
        aliens = pygame.transform.scale(aliens, (20,20))
        super(Block, self).__init__()

        self.image = aliens

        self.rect = self.image.get_rect()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.mouse.set_visible(0)
        ship = pygame.image.load('spaceship.png')
        ship = pygame.transform.scale(ship, (20,30))
        super(Player, self).__init__()

        self.image = ship
        self.rect = self.image.get_rect()
    def update(self):
        pos = pygame.mouse.get_pos()
        self.rect.x = pos[0]

class Bullet(pygame.sprite.Sprite):
    def __init__(self):
        bullets = pygame.image.load('missile.png')
        bullets = pygame.transform.scale(bullets, (15,25))
        super(Bullet, self).__init__()
        self.image = bullets
        self.rect = self.image.get_rect()

    def update(self):
        self.rect.y -= 1.25

all_sprites_list = pygame.sprite.Group()
block_list = pygame.sprite.Group()
bullet_list = pygame.sprite.Group()


for i in range(10):
    block = Block(Block)
    block.rect.x = (40 * i) + 200
    block.rect.y = 50
    block_list.add(block)
    all_sprites_list.add(block)

for i in range(10):
    block = Block(Block)
    block.rect.x = (40 * i) + 200
    block.rect.y = 75
    block_list.add(block)
    all_sprites_list.add(block)

for i in range(10):
    block = Block(Block)
    block.rect.x = (40 * i) + 200
    block.rect.y = 100
    block_list.add(block)
    all_sprites_list.add(block)

for i in range(10):
    block = Block(Block)
    block.rect.x = (40 * i) + 200
    block.rect.y = 125
    block_list.add(block)
    all_sprites_list.add(block)




player = Player()
all_sprites_list.add(player)
done = False
clock = pygame.time.Clock()
score = 0
time = 3000
player.rect.y = 615
font = pygame.font.SysFont(None, 30)



while not done:
    OpenScreen.fill(WHITE)
    fonts = pygame.font.SysFont("impact", 55)
    display = fonts.render('PRESS SPACE TO PLAY', True, (BLACK))
    OpenScreen.blit(display, (200 , 200))
    font1 = pygame.font.SysFont("impact", 25)
    display1 = font.render("Press Escape to quit", True, (BLACK))
    OpenScreen.blit(display1, (670,600))
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        pressed = pygame.key.get_pressed()

        if pressed[pygame.K_ESCAPE]:
            done = True

        elif pressed[pygame.K_SPACE]:
            while not done:

                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        done = True

                    elif event.type == pygame.MOUSEBUTTONDOWN:
                        bullet = Bullet()
                        bullet.rect.x = player.rect.x + 3.5
                        bullet.rect.y = player.rect.y
                        all_sprites_list.add(bullet)
                        bullet_list.add(bullet)



                all_sprites_list.update()

                for bullet in bullet_list:
                    block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True)
                    for block in block_hit_list:

                        bullet_list.remove(bullet)
                        all_sprites_list.remove(bullet)
                        score += 1
                    if bullet.rect.y < -10:
                        bullet_list.remove(bullet)
                        all_sprites_list.remove(bullet)

                screen.fill(NAVYBLUE)
                scoredisplay = font.render("Score {0}".format(score), 1, (WHITE))
                screen.blit(scoredisplay, (5, 10))
                all_sprites_list.draw(screen)
                timedisplay = font.render("Time {0}".format(time), 1, (WHITE))
                screen.blit(timedisplay, (5, 30))
                pygame.display.flip()

                time -= 1
                if time == 0:
                    done = True
                    screen.fill(WHITE)
                    font= pygame.font.SysFont("impact", 36)
                    display = font.render("YOU RAN OUT OF TIME!! :( Your final score was {}".format(score), 1, (BLACK))
                    screen.blit(display, (100,200))
                    pygame.display.flip()
                    pygame.time.wait(1500)

                if len(block_list)  == 0:
                    done == True
                    len(bullet_list) == 0
                    screen.fill(WHITE)
                    time = 0
                    font = pygame.font.SysFont("impact", 55)
                    display = font.render('You Won!',True ,(BLACK))
                    display1 = font.render('Press SPACE for Level 2',True ,(BLACK))
                    display2 = font.render('Escape to quit',True ,(BLACK))
                    screen.blit(display,(350,200))
                    screen.blit(display1,(350,400))
                    screen.blit(display2,(500,500))
                    clock.tick(60)
                    pygame.display.flip()
                    pygame.time.wait(1500)
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            done = True
                        pressed = pygame.key.get_pressed()
                        if pressed[pygame.K_ESCAPE]:
                            done = True
                        elif pressed[pygame.K_SPACE]:
                            done = False
                            subprocess.call("LEVEL2.py", shell=True)


pygame.quit()

1 个答案:

答案 0 :(得分:0)

将第1级中的所有内容除类定义外,并将其放入具有合理名称的函数中,例如“init”和“playGame”。让他们成为一个名为“Level”的班级的成员。

注意Level 1和Level 2中哪些方面有所不同。将这些参数不同的参数添加到Level的__init__成员中,并使用它们初始化一些将由其{{1}重用的局部变量会员。

现在确保playGame和init函数适用于这两个级别。

重写每个级别的所有代码是没有意义的。这样,您可以拥有任意数量的级别。