2016-06-11 42 views
-1

我想提出一個太空侵略者般的遊戲。我已經完成了第一個有四排10個外星人的第一個關卡。我想增加另一個級別,其中有4行17個外星人,以使其變得困難並縮短時間限制。我已經創建了一個單獨的模塊名稱'LEVEL2',這是我嘗試導入並在第一級完成後運行的模塊名稱。有人可以幫我解決這個問題。如何在我的遊戲中添加2級?

在此先感謝

主要的遊戲(1級):

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() 

這裏是2級:

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() 
+0

而不是拷貝和粘貼代碼的最佳實踐的2個模塊將是使傳遞變量,船舶,線路,速度,水平數數你的遊戲代碼中的條件,然後再用不同的參數 – gbtimmon

+0

我同樣的遊戲代碼試過了,但不知道究竟如何調用不同的值,例如for循環的射程(外星人的數目),我不知道該怎麼稱呼它,並用不同的設定值 – user108

+0

的爲什麼不使用像level = 1的變量,並有一個if語句,根據該變量運行級別1或級別2? – numbermaniac

回答

0

採取所有的東西在1級除了類定義,並將其放入具有合理名稱(如「init」和「playGame」)的函數中。讓他們成爲一個名爲「級別」的成員。

請注意Level 1和Level 2中哪些方面不同。將這些參數不同的參數放入Level的__init__成員中,並使用它們初始化某個局部變量,該局部變量將由playGame成員重新使用。

現在可以確保瑣事和初始化功能兩個層面的工作。

這只是沒有任何意義重寫所有的代碼爲每個級別。這樣,你可以有你想要的水平。

+0

好的,謝謝我嘗試。 – user108

+0

我是初學者,我仍然堅持。你能告訴我怎麼樣? – user108

+0

我寧願看到你寫的是什麼,在哪裏,當你寫它卡住了。你爲什麼不試一試? –