2016-12-30 46 views
0

我正試圖在特定區域將草的圖像上傳到我的遊戲中,但由於它在課程中,因此我遇到了麻煩。我想要做這樣的事情..在一個類中定位一個實體(Pygame)

grass_platform.draw(screen, (200, 200)) 

注:我知道這其實不工作

這裏是我的代碼...

import pygame 

pygame.init() 

#Screen Size 
screen_size = [1024,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 

#Backgrounds 
forest = pygame.image.load('forest.png') 

#Gravity 
gravity =-10 
fall = True 

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

class Player(pygame.sprite.Sprite): 

    def __init__(self, x, y, filename): 

     pygame.sprite.Sprite.__init__(self) 
     self.x = x 
     self.y = y 
     self.image = fresco 
     self.rect = self.image.get_rect() 

    def update (self): 
     if event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_a: 
       self.rect.x -= 6 
       self.image = pygame.transform.flip(fresco, True, False) 

      elif event.key == pygame.K_d: 
       self.rect.x += 6 
       self.image = pygame.transform.flip(fresco, False, False) 

      elif event.key == pygame.K_w: 
       self.rect.y -= 20 

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

player = Player(0, 0, 'fresco v2.png') 

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

class Grass (pygame.sprite.Sprite): 

    def __init__(self, filename): 

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


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

grass_platform = Grass('grass.png') 

#Game Loop 
while not gameStart: 

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

    #Background Blitted 
    screen.blit(forest,(0,0)) 

    #Falling Event 
    if fall == True: 
     if gravity: 
      player.rect.y -= gravity 

    #Class Blitted 
    player.update() 
    player.draw(screen) 
    grass_platform.draw(screen) 

    #Updates Screen 
    pygame.display.update() 

    #FPS 
    clock.tick(30) 

pygame.quit() 
+0

in'__init__'你需要'self.rect.x = x'和'self.rect.y = y' – furas

+0

使編碼更具可讀性,所有類都放在開頭。並使用加載的圖像作爲參數ie。「草(草)」或在課堂內加載圖像。 – furas

回答

0

我不知道如果我理解你正確,但如果你的意思只是傳遞一個座標參數和blitting到那個應該很容易的位置。

class Grass (pygame.sprite.Sprite): 

    def __init__(self, filename): 

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


    def draw(self, screen, pos): 
     screen.blit(self.image, pos) 

,然後當你畫()你只是這樣做:

grass_platform.draw(screen, (300, 200)) 

或在另一方面,如果你想保留的位置,你可以將此代碼添加 到INIT方法:

def __init__(self, filename, x, y): 
    self.x = x 
    self.y = y 

,然後存取權限後來

def draw(self, screen): 
    window.blit(img, (self.x, self.y)) 
0

一些注意事項:

  • PlayerGrass來自Sprite子類,所以他們已經平局功能,做你正在做什麼。只要刪除它們。

  • 以下部分

    #Falling Event 
    if fall == True: 
        if gravity: 
         player.rect.y -= gravity 
    

    是特定於Player類和應該被移動到Player類的更新功能

  • 當使用Sprite類中,rect屬性被用來存儲精靈的位置,因此您的Player類中的xy屬性沒有多大意義。刪除它們,並使用xy參數來改變xy屬性rect代替:

    self.rect = self.image.get_rect(x=x, y=y) 
    
  • 因此,如果你想在一個特定的位置畫出grass_platform,只是改變它的rect,如:

    grass_platform.rect.topleft = (200, 200) 
    
  • ,而不是手動調用每個精靈drawupdate,只是但其中成Group,並調用drawupdate關於那個Group

相關問題