Python 如何在游戏中添加2级?

Python 如何在游戏中添加2级?,python,pygame,Python,Pygame,我正在做一个太空入侵者喜欢的游戏。我已经完成了第一关,有4排10个外星人。我想添加另一个级别,其中有4行17个外星人,以使其变得困难,并减少时间限制。我已经创建了一个单独的模块名“LEVEL2”,这是我在完成一级后尝试导入和运行的模块名。有人能帮我弄清楚吗 提前谢谢 主要游戏(一级): 导入pygame,随机,系统 从时间上导入睡眠 从pygame.locals导入* 导入子流程 进口水平2 黑色=(0,0,0) 白色=(255,255,255) 红色=(255,0,0) 蓝色=(0,0255)

我正在做一个太空入侵者喜欢的游戏。我已经完成了第一关,有4排10个外星人。我想添加另一个级别,其中有4行17个外星人,以使其变得困难,并减少时间限制。我已经创建了一个单独的模块名“LEVEL2”,这是我在完成一级后尝试导入和运行的模块名。有人能帮我弄清楚吗

提前谢谢

主要游戏(一级):

导入pygame,随机,系统
从时间上导入睡眠
从pygame.locals导入*
导入子流程
进口水平2
黑色=(0,0,0)
白色=(255,255,255)
红色=(255,0,0)
蓝色=(0,0255)
NAVYBLUE=(0,0,45)
灰色=(128128)
屏幕宽度=900
屏幕高度=650
pygame.init()
OpenScreen=pygame.display.set_模式([屏幕宽度,屏幕高度])
screen=pygame.display.set_模式([屏幕宽度,屏幕高度])
类块(pygame.sprite.sprite):
定义初始化(自我,图像):
外星人=pygame.image.load('alienses.png')
外星人=pygame.transform.scale(外星人,(20,20))
超级(块,自).\uuu初始化
self.image=外星人
self.rect=self.image.get_rect()
职业玩家(pygame.sprite.sprite):
定义初始化(自):
pygame.mouse.set_可见(0)
ship=pygame.image.load('spaceship.png')
ship=pygame.transform.scale(ship,(20,30))
超级(玩家,自我)。\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu()
self.image=船
self.rect=self.image.get_rect()
def更新(自我):
pos=pygame.mouse.get_pos()
self.rect.x=pos[0]
类子弹(pygame.sprite.sprite):
定义初始化(自):
bullets=pygame.image.load('employee.png')
子弹=pygame.transform.scale(子弹,(15,25))
超级(子弹,自我)。\uuuu初始化
self.image=项目符号
self.rect=self.image.get_rect()
def更新(自我):
自校正y-=1.25
所有精灵列表=pygame.sprite.Group()
block_list=pygame.sprite.Group()
bullet\u list=pygame.sprite.Group()
对于范围(10)内的i:
块=块(块)
block.rect.x=(40*i)+200
块矩形y=50
块列表。添加(块)
所有精灵列表。添加(块)
对于范围(10)内的i:
块=块(块)
block.rect.x=(40*i)+200
块矩形y=75
块列表。添加(块)
所有精灵列表。添加(块)
对于范围(10)内的i:
块=块(块)
block.rect.x=(40*i)+200
块矩形y=100
块列表。添加(块)
所有精灵列表。添加(块)
对于范围(10)内的i:
块=块(块)
block.rect.x=(40*i)+200
块矩形y=125
块列表。添加(块)
所有精灵列表。添加(块)
player=player()
所有精灵列表。添加(玩家)
完成=错误
clock=pygame.time.clock()
分数=0
时间=3000
player.rect.y=615
font=pygame.font.SysFont(无,30)
虽然没有这样做:
OpenScreen.fill(白色)
fonts=pygame.font.SysFont(“impact”,55)
display=font.render('按空格播放',True,(黑色))
OpenScreen.blit(显示,(200200))
font1=pygame.font.SysFont(“impact”,25)
display1=font.render(“按Escape退出”,True,(黑色))
OpenScreen.blit(display1,(670600))
pygame.display.flip()
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
pressed=pygame.key.get_pressed()
如果按下[pygame.K_ESCAPE]:
完成=正确
elif按下[pygame.K_SPACE]:
虽然没有这样做:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
elif event.type==pygame.MOUSEBUTTONDOWN:
bullet=bullet()
bullet.rect.x=player.rect.x+3.5
bullet.rect.y=player.rect.y
所有精灵列表。添加(项目符号)
项目符号列表。添加(项目符号)
所有精灵列表。更新()
对于项目符号列表中的项目符号:
block\u hit\u list=pygame.sprite.spritecollide(bullet,block\u list,True)
对于block_命中列表中的block_:
项目符号列表。删除(项目符号)
所有精灵列表。删除(项目符号)
分数+=1
如果项目符号矩形y<-10:
项目符号列表。删除(项目符号)
所有精灵列表。删除(项目符号)
屏幕填充(导航蓝色)
scoredisplay=font.render(“分数{0}”。格式(分数),1,(白色))
屏幕。blit(分数显示,(5,10))
所有精灵列表。绘制(屏幕)
timedisplay=font.render(“时间{0}”。格式(时间),1,(白色))
屏幕blit(时间显示,(5,30))
pygame.display.flip()
时间-=1
如果时间=0:
完成=正确
屏幕填充(白色)
font=pygame.font.SysFont(“impact”,36)
display=font.render(“您的时间用完了!!:(您的最终分数是{})。格式(分数),1,(黑色))
屏幕显示(显示,(100200))
pygame.display.flip()
pygame.time.wait(1500)
如果len(块列表)=0:
完成==真
len(项目符号列表)=0
屏幕填充(白色)
时间=0
font=pygame.font.SysFont(“impact”,55)
display=font.render('youwand!',True,(黑色))
display1=font.render('按空格键表示级别2',True,(黑色))
display2=font.render('Escape to quit',True,(黑色))
屏幕blit(显示器,(350200))
屏幕blit(显示器1,(350400))
屏幕blit(显示器2,(500500))
时钟滴答(60)
pygame.display.flip()
pygame.ti
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()
import pygame, random, sys
from time import sleep
from pygame.locals import *
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 -= 3

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


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

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

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

for i in range(17):
    block = Block(Block)
    block.rect.x = (40 * i) + 110
    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 = 2000
player.rect.y = 615
font = pygame.font.SysFont(None, 30)



while not done:
    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
                    screen.fill(WHITE)
                    font = pygame.font.SysFont("impact", 55)
                    display = font.render('You Won!',True ,(BLACK))
                    screen.blit(display,(350,200))
                    pygame.display.flip()
                    pygame.time.wait(1500)
                clock.tick(60)

pygame.quit()