2016-03-07 20 views
3

我一直在與我的遊戲水平有問題,我想知道爲什麼我的遊戲中的敵人不是從屏幕的頂部落下,我試圖讓水平,但我的班級級別無法正常工作。請有人幫忙!太空射擊遊戲有輕微的錯誤

我的代碼:

import pygame 
import random 

# Define some colors 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
BLUE = (0, 0, 255) 
LAV = (209, 95, 250) 


#Enemy 
class Block(pygame.sprite.Sprite): 
    """ This class represents the block. """ 
    def __init__(self, color): 
     # Call the parent class (Sprite) constructor 
     super().__init__() 

     self.image = pygame.image.load("enemy2.png") 

     self.rect = self.image.get_rect() 
    def reset_pos(self): 


     self.rect.y = random.randrange(-300,-20) 
     self.rect.x = random.randrange(0,screen_width) 
#Player 
class Player(pygame.sprite.Sprite): 
    """ This class represents the Player. """ 

    def __init__(self): 
     """ Set up the player on creation. """ 
     # Call the parent class (Sprite) constructor 
     super().__init__() 
     self.image = pygame.image.load("SS1.png") 



     self.rect = self.image.get_rect() 
#Controls 
    def update(self): 
     """ Update the player's position. """ 
     # Get the current mouse position. This returns the position 
     # as a list of two numbers. 
     pos = pygame.mouse.get_pos() 

     # Set the player x position to the mouse x position 
     self.rect.x = pos[0] 



class Bullet(pygame.sprite.Sprite): 
    """ This class represents the bullet . """ 
    def __init__(self): 
     # Call the parent class (Sprite) constructor 
     super().__init__() 

     self.image = pygame.image.load("fireball2.png") 

     self.rect = self.image.get_rect() 

    def update(self): 
     """ Move the bullet. """ 
     self.rect.y -= 6 



# --- Create the window 

# Initialize Pygame 
pygame.init() 

pygame.display.set_caption("Space Shooter") 

# Set the height and width of the screen 
screen_width = 850 
screen_height = 850 
screen = pygame.display.set_mode([screen_width, screen_height]) 
BG = pygame.image.load("Bg.png") 
# --- Sprite lists 

# This is a list of every sprite. All blocks and the player block as well. 
all_sprites_list = pygame.sprite.Group() 

# List of each block in the game 
block_list = pygame.sprite.Group() 

# List of each bullet 
bullet_list = pygame.sprite.Group() 

# --- Create the sprites 
class Level(): 
    """ This is a generic super-class used to define a level. 
     Create a child class for each level with level-specific 
     info. """ 

    def __init__(self, player): 
     """ Constructor. Pass in a handle to player. Needed for when moving 
      platforms collide with the player. """ 
     self.platform_list = pygame.sprite.Group() 
     self.enemy_list = pygame.sprite.Group() 
     self.player = player 

     # How far this world has been scrolled left/right 
     self.world_shift = 0 

    # Update everythign on this level 
    def update(self): 
     """ Update everything in this level.""" 
     self.platform_list.update() 
     self.enemy_list.update() 

    def draw(self, screen): 
     """ Draw everything on this level. """ 

     # Draw the background 
     screen.fill(BLUE) 

     # Draw all the sprite lists that we have 
     self.platform_list.draw(screen) 
     self.enemy_list.draw(screen) 

    def shift_world(self, shift_x): 
     """ When the user moves left/right and we need to scroll 
     everything: """ 

     # Keep track of the shift amount 
     self.world_shift += shift_x 

     # Go through all the sprite lists and shift 
     for platform in self.platform_list: 
      platform.rect.x += shift_x 

     for enemy in self.enemy_list: 
      enemy.rect.x += shift_x 


# Create platforms for the level 
class Level_01(Level): 
    """ Definition for level 1. """ 

    def __init__(self, player): 
     """ Create level 1. """ 

     # Call the parent constructor 
     Level.__init__(self, player) 

     self.level_limit = 10 

    for i in range(15): 
    # This represents a block 
     block = Block(BLUE) 

    # Set a random location for the block 
     block.rect.x = random.randrange(825) 
     block.rect.y = random.randrange(500) 

    # Add the block to the list of objects 
     block_list.add(block) 
     all_sprites_list.add(block) 

# Create a red player block 
class Level_02(Level): 
    """ Definition for level 2. """ 

    def __init__(self, player): 
     """ Create level 1. """ 

     # Call the parent constructor 
     Level.__init__(self, player) 

     self.level_limit = -3000 

     # Array with type of platform, and x, y location of the platform. 
     for i in range(30): 
    # This represents a block 
      block = Block(BLUE) 

    # Set a random location for the block¸  
      block.rect.x = random.randrange(650) 
      block.rect.y = random.randrange(350) 

    # Add the block to the list of objects 
      block_list.add(block) 
      all_sprites_list.add(block) 

     # Go through the arrzay above and add platforms 


player = Player() 
all_sprites_list.add(player) 

# Loop until the user clicks the close button. 
done = False 

# Used to manage how fast the screen updates 
clock = pygame.time.Clock() 

font = pygame.font.Font(None, 25) 

frame_count = 0 
frame_rate = 60 
start_time = 90 

score = 0 
player.rect.y = 600 
bg_sound = pygame.mixer.Sound("Countdown - Timer.wav") 
fire_sound = pygame.mixer.Sound("Laser-SoundBible.com-602495617.wav") 
bg_sound.play(-1) 

# -------- Main Program Loop ----------- 
while not done: 
    # --- Event Processing 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 



     elif event.type == pygame.MOUSEBUTTONDOWN: 
      # Fire a bullet if the user clicks the mouse button 
      bullet = Bullet() 
      # Set the bullet so it is where the player is 
      bullet.rect.x = player.rect.x 
      bullet.rect.y = player.rect.y 
      # Add the bullet to the lists 
      all_sprites_list.add(bullet) 
      bullet_list.add(bullet) 
      fire_sound.play() 




    # --- Game logic 

    # Call the update() method on all the sprites 
    all_sprites_list.update() 

    # Calculate mechanics for each bullet 
    for bullet in bullet_list: 

     # See if it hit a block 
     block_hit_list = pygame.sprite.spritecollide(bullet, block_list, True) 

     # For each block hit, remove the bullet and add to the score 
     for block in block_hit_list: 
      bullet_list.remove(bullet) 
      all_sprites_list.remove(bullet) 
      score += 1 
      print(score) 

     # Remove the bullet if it flies up off the screen 
     if bullet.rect.y < -50: 
      bullet_list.remove(bullet) 
      all_sprites_list.remove(bullet) 



    # --- Draw a frame 
    screen.blit(BG, [0, 0]) 

    # Clear the screen 


    font = pygame.font.SysFont('Calibri', 25,True, False) 
    text = font.render('Enemies Shot:'+ str(score),True, LAV) 
    screen.blit(text,[650,500]) 


    # Draw all the spites 
    all_sprites_list.draw(screen) 

    total_seconds = start_time - (frame_count // frame_rate) 
    if total_seconds == 0: 
     font = pygame.font.SysFont('Calibri', 25,True, False) 
     text = font.render('YOURE DONE',True, LAV) 
     screen.blit(text,[650,500]) 


    # Divide by 60 to get total minutes 
    minutes = total_seconds // 60 

    # Use modulus (remainder) to get seconds 
    seconds = total_seconds % 60 

    # Use python string formatting to format in leading zeros 
    output_string = "Time left: {0:00}:{1:02}".format(minutes, seconds) 

    # Blit to the screen 
    text = font.render(output_string, True, LAV) 

    screen.blit(text, [650, 525]) 

    # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT 
    frame_count += 10 

    # Limit frames per second 
    clock.tick(frame_rate) 

    # Go ahead and update the screen with what we've drawn. 
    pygame.display.flip() 

pygame.quit() 
+0

有趣的是我在同一個項目上工作。儘管如此,我並沒有看到任何關於y軸上敵人的特定動作的代碼。也許在主遊戲循環中添加一個比較說明,看敵人的x座標是否大於WINDOWWIDTH,並且改變+改變敵人的方向? – Jerrybibo

回答

0

,你可能要注意的是要清除更新所有的子彈後,屏幕一分快的事情。我建議清理屏幕作爲while循環內的第一件事。另一件事是我沒有看到任何你想要移動敵人的地方(我可能是錯的)。這樣一個塊類中的功能可能會有所幫助:

def render(self): 
    self.rect.y +=3 

,您可以更改您的渲染功能塊移動,這只是一個例子。
在您輸入,在你需要把這樣的:

for block in block_list: 
    block.render() 

這會去你的while循環中您清除後任何地方。您可能需要創建一個阻止列表才能使用。希望這有助於:)