2017-01-05 4 views
0

Ich habe beunruhigende eine Plattform bekommen in Bereichen anzuzeigen, die ich in ‚P‘ string haben. Ich versuche, eine Ebene in pygame mit Strings anstelle von Satz zu erstellen Koordinaten für jede Plattform . Hier ist ein Beispiel ...angezeigte Plattformen Strings (Pygame)

class Level_01(Level): 

    def __init__(self, player): 

     self.background = forest_bg 

     Level.__init__(self, player) 

     platforms = [] 

     x = y = 0 
     level = ['   ', 
       'P   ', 
       '   ', 
       ' P   ', 
       '   ', 
       'PPPPPPPPPPPP',] 

     # build the level 
     for row in level: 
      for col in row: 
       if col == 'P': 
        P = Platform(x, y) 
        platforms.append(P) 
       x += 90 
      y += 90 
      x = 0 

Hier ist das gesamte Projekt ...

import pygame 

pygame.init() 

#Screen Size 
screen_size = 1024, 576 
screen_width = 1024 
screen_height = 576 

#Display Window 
screen = pygame.display.set_mode(screen_size) 
pygame.display.set_caption('The Adventures of Fresco the Explorer') 

#Clock 
clock = pygame.time.Clock() 

#Colors 
black = (0,0,0) 
white = (255,255,255) 

#Game Start 
gameStart = False 

#Forest Background 
forest_bg = pygame.image.load('forest.png') 

#Player Assets and Variables 
fresco = pygame.image.load('fresco v2.png').convert() 
fresco = pygame.transform.scale(fresco,(32,136)) 

velocity = 6 

move_left = False 
move_right = False 

#Grass 
grass = pygame.image.load('grass.png') 
grass = pygame.transform.scale(grass, (90, 90)) 


#Player Class 
class Player(pygame.sprite.Sprite): 

    def __init__(self, x, y): 

     pygame.sprite.Sprite.__init__(self) 

     self.x = 0 
     self.y = 0 
     self.image = fresco 
     self.rect = self.image.get_rect() 

    def handle_keys(self): 

     key = pygame.key.get_pressed() 
     velocity = 8 

     #Move Right 
     if key[pygame.K_d]: 
      self.rect.x += velocity 

     #Move Left 
     elif key[pygame.K_a]: 
      self.rect.x -= velocity 
      pygame.transform.flip(self.image, True, False) 

    def draw (self, surface): 
     surface.blit(self.image, self.rect) 

player = Player(0,0) 


class Platform(pygame.sprite.Sprite): 

    def __init__(self, x, y): 
     super().__init__() 

     self.image = grass 
     self.rect = self.image.get_rect() 


class Level(object): 
    """ 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.player = player 

     # Background image 
     self.background = None 

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

     #self.enemy_list.update() <--- NOTE: Use this late :3 

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

     # Draw all the sprite lists that we have 
     self.platform_list.draw(screen) 
     #self.enemy_list.draw(screen) <--- Use it later :3 


class Level_01(Level): 

    def __init__(self, player): 

     self.background = forest_bg 

     Level.__init__(self, player) 

     platforms = [] 

     x = y = 0 
     level = ['   ', 
       'P   ', 
       '   ', 
       ' P   ', 
       '   ', 
       'PPPPPPPPPPPP',] 

     # build the level 
     for row in level: 
      for col in row: 
       if col == 'P': 
        P = Platform(x, y) 
        platforms.append(P) 
       x += 90 
      y += 90 
      x = 0 

level_list = [] 
level_list.append(Level_01(player)) 

# Set the current level 
current_level_no = 0 
current_level = level_list[current_level_no] 

active_sprite_list = pygame.sprite.Group() 
player.level = current_level 

#Game Loop 
while not gameStart: 

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


    #Background 
    screen.blit(forest_bg,(0,0)) 

    #Player Updates 
    player.handle_keys() 
    player.draw(screen) 

    #Level Updates 
    current_level.update() 
    current_level.draw(screen) 
    active_sprite_list.draw(screen) 
    # Updates Screen 
    pygame.display.update() 

    #FPS 
    clock.tick(60) 

pygame.quit() 
+0

BTW: setzen alle Klassen und Funktion vor 'pygame.init (') es besser lesbar zu machen. Sie erstellen 'player = Player (0,0)' zwischen den Klassen, so dass niemand sie finden kann. Siehe [wie Sie organisieren Code könnte] (https://github.com/furas/python-examples/blob/master/pygame/__templates__/1__simple__.py) – furas

Antwort

0

Ihr Problem ist, weil Sie Platform() erstellen mit argumenst x,y

P = Platform(x, y) 

, aber sie tun nichts mit diese Information.

Sie haben diese Werte remeber in __init__

self.rect.x = x 
self.rect.y = y 

oder kürzer

self.rect = self.image.get_rect(x=x, y=y) 

Und Sie haben das gleiche Problem in der Klasse Player()


BTW: bessere Nutzung self.rect.x, self.rect.y statt self.x, self.y in allen Klassen, weil Sie es Kollisionen überprüfen können - player.rect.colliderect(some_element.rect) oder player.rect.collidepoint(mouse_pos)

Auch pygame.sprite.Group() erwartet, dass alle Elemente Position und Größe in self.rect haben, weil es self.image und self.rect nutzen sie zu zeichnen.