2017-04-08 4 views
0

Ich arbeite durch die Programmearcadegames Labs mit Python und Pygame und ich bin fest. Wenn ich diesen Code ausführe, mache ich etwas falsch, wenn ich die Block- und Player-Klassen initialisiere. Die Player-Klasse soll eine eigene Klasse sein und keine Instanz der Block-Klasse.python/pygame Hilfe für Labor 13 programmarcadegames

Wenn ich dieses Programm ausführen, erhalte ich diese Fehlermeldung: 'Block' Objekt hat kein Attribut 'changespeed'

import pygame 
import random 

# Define some colors 
BLACK = ( 0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
BLUE = (0, 0, 255) 

class Block(pygame.sprite.Sprite): 

    def __init__(self, color, width, height): 
     """ Constructor. Pass in the color of the block, 
     and its x and y position. """ 

     # Call the parent class (Sprite) constructor 
     super().__init__() 

     # Create an image of the block, and fill it with a color. 
     # This could also be an image loaded from the disk. 
     self.image = pygame.Surface([width, height]) 
     self.image.fill(color) 

     # Fetch the rectangle object that has the dimensions of the image 
     # image. 
     # Update the position of this object by setting the values 
     # of rect.x and rect.y 
     self.rect = self.image.get_rect() 

class Player (pygame.sprite.Sprite): 
    """ The class is the player-controlled sprite. """ 

    def __init__(self,x,y): 
     """Constructor function""" 
     # Call the parent's constructor 
     super().__init__() 

     # -- methods 
     # Set height, width 
     self.image = pygame.Surface([15, 15]) 
     self.image.fill(BLACK) 

     # Make our top-left corner the passed-in location. 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 

     # -- Attributes 
     # Set speed vector 
     self.change_x = 0 
     self.change_y = 0 

    def changespeed(self, x, y): 
     """ Change the speed of the player""" 
     self.change_x += x 
     self.change_y += y 

    def update(self): 
     """ Find a new position for the player""" 
     self.rect.x += self.change_x 
     self.rect.y += self.change_y 

# Initialize Pygame 
pygame.init() 

# Set the height and width of the screen 
screen_width = 700 
screen_height = 400 
screen = pygame.display.set_mode([screen_width, screen_height]) 

# This is a list of 'sprites.' Each block in the program is 
# added to this list. The list is managed by a class called 'Group.' 
block_list = pygame.sprite.Group() 

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

for i in range(50): 
    # This represents a block 
    block = Block(BLACK, 20, 15) 

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

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

# Create a RED player block 
player = Block(BLUE, 20, 15) 
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() 

score = 0 

# -------- Main Program Loop ----------- 
while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 
     # Set the speed based on the key pressed 
     elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_LEFT: 
       player.changespeed(-3, 0) 
      elif event.key == pygame.K_RIGHT: 
       player.changespeed(3, 0) 
      elif event.key == pygame.K_UP: 
       player.changespeed(0, -3) 
      elif event.key == pygame.K_DOWN: 
       player.changespeed(0, 3) 

     # Reset speed when key goes up 
     elif event.type == pygame.KEYUP: 
      if event.key == pygame.K_LEFT: 
       player.changespeed(3, 0) 
      elif event.key == pygame.K_RIGHT: 
       player.changespeed(-3, 0) 
      elif event.key == pygame.K_UP: 
       player.changespeed(0, 3) 
      elif event.key == pygame.K_DOWN: 
       player.changespeed(0, -3) 

    # Clear the screen 
    screen.fill(WHITE) 

    # Get the current mouse position. This returns the position 
    # as a list of two numbers. 
    #pos = pygame.mouse.get_pos() 

    # Fetch the x and y out of the list, 
     # just like we'd fetch letters out of a string. 
    # Set the player object to the mouse location 
    #player.rect.x = pos[0] 
    #player.rect.y = pos[1] 

    # See if the player block has collided with anything. 
    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True) 

    # Check the list of collisions. 
    for block in blocks_hit_list: 
     score += 1 
     print(score) 

    # This calls update on all the sprites 
    all_sprites_list.update() 

    # Draw all the spites 
    all_sprites_list.draw(screen) 

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

    # Limit to 60 frames per second 
    clock.tick(60) 

pygame.quit() 
+1

Der Fehler scheint darauf hinzudeuten, dass Sie einen Block erstellen und versuchen, die Methode 'changespeed' zu verwenden. Die Klassen sind überhaupt nicht verwandt, so dass der Fehler nicht darin besteht, dass "sie keine eigenen Klassen sind". Was ich denke, ist, dass Sie die Spielervariable mit einer Blockinstanz überschreiben, oder indem Sie sowohl den Spieler als auch den Block in eine Liste setzen, die Sie durchlaufen und versuchen, 'Änderungsgeschwindigkeit' für jedes Objekt zu nennen. Es ist jedoch nicht möglich, Ihnen ohne [mcve] weiter zu helfen. –

+1

Ich habe den Code in seiner Gesamtheit veröffentlicht. Wenn ich versuche, das Player-Objekt zu verschieben, wird ein Fehler ausgegeben. – celtics33

+1

Das Erstellen eines [mcve] wird Ihnen helfen, das Problem selbst ohne Hilfe zu lösen und ist sehr schnell erledigt (maximal 5-10 Minuten). Wenn Sie den Fehler nicht finden, machen Sie die Frage zumindest für andere Personen hilfreich, die nach Antworten in Stackoverflow suchen. Wenn Benutzer auf Stackoverflow suchen, finden sie leichter eine Lösung, wenn das Problem in einem minimalen Beispiel mit vollständiger Erläuterung des Problems enthalten ist und leicht überprüfbar und überprüfbar ist. Aber um Ihnen zwei wichtige Hinweise zu geben: 1. In welcher Zeile des Codes initialisieren Sie den Player? 2. Wann nennst du die Player-Klasse? –

Antwort

1

Hier ist das Problem: player = Block(BLUE, 20, 15). Die player ist eine Block Instanz keine Player Instanz und Blöcke haben keine changespeed Methode.

+1

Danke für die große Hilfe! – celtics33