2017-02-04 81 views
1

我正在製作遊戲,並且在此遊戲中,我有一個從遊戲屏幕頂部落下的對象。當對象的y> = 200時,我想用隨機的x位置重繪對象。我試圖編碼它,但它不起作用。我懷疑在重繪對象的位置存在一些問題。有人能指出我的錯誤嗎?無法獲取對象來重繪

的問題是在draw()

PYTHON

# IMPORTS 
import pygame, random 

# GLOBALS 
global screen, displayW, displayH 
global clock, FPS 
global end, food, player 

# SETGLOBALVALUES 
def setGlobalValues(): 
    global screen, displayW, displayH 
    global clock, FPS 
    global end, food, player 

    displayW = 800 
    displayH = 600 
    screen = pygame.display.set_mode((displayW, displayH)) 

    clock = pygame.time.Clock() 
    FPS = 60 

    end = False 
    food = Food() 
    player = Player() 

# MAIN 
def main(): 
    pygame.init() 

    setGlobalValues() 
    setup() 
    gameLoop() 
    quitGame() 

# GAMELOOP 
def gameLoop(): 
    global end, player 

    while(not end): 
     for event in pygame.event.get(): 
      # ONCLICK QUIT 
      if(event.type == pygame.QUIT): 
       end = True; 

      # KEYDOWN 
      if(event.type == pygame.KEYDOWN): 
       if(event.key == pygame.K_LEFT): 
        player.velX -= 10 
       if(event.key == pygame.K_RIGHT): 
        player.velX += 10 

      # KEYUP 
      if(event.type == pygame.KEYUP): 
       if(event.key == pygame.K_LEFT): 
        player.velX = 0 
       if(event.key == pygame.K_RIGHT): 
        player.velX = 0 

     draw() 
     animate() 
     collision() 
     setFPS() 


# DRAW 
def draw(): 
    global screen, food, player 

    # fill background 
    screen.fill((255, 255, 255)) 

    player.draw() 

    if player.y >= 200: 
     player.draw() 

    food.draw() 

    # update 
    pygame.display.update() 

# ANIMATE 
def animate(): 
    global food, player 

    food.animate() 
    player.animate() 

# COLLISION 
def collision(): 
    player.collision() 

# SETFPS 
def setFPS(): 
    global clock, FPS 

    clock.tick(FPS); 

# CLASSES 
class Food(): 
    def __init__(self, img="", x=0, h=0, velY=0, color=()): 
     global displayW 

     self.img = pygame.image.load("assets/img/rsz_burger.png") 
     self.x = random.randrange(0, displayW) 
     self.y = -100 
     self.velY = 3 
     self.color = (255, 0, 0) 

    def draw(self): 
     global screen 

     screen.blit(self.img, (self.x, self.y)) 

    def animate(self): 
     self.y += self.velY 

    def collision(self): 
     global displayW, displayH 

     pass 

class Player(): 
    def __init__(self, x=0, y=0, velX=0, velY=0, w=0, h=0, color=()): 
     global displayW, displayH 

     self.w = 20 
     self.h = 20 
     self.x = displayW/2 - self.w/2 
     self.y = displayH - 100 
     self.velX = 0 
     self.velY = 0 
     self.color = (0, 0, 0) 

    def draw(self): 
     global screen 

     pygame.draw.ellipse(screen, self.color, (self.x, self.y, self.w, self.h)) 

    def animate(self): 
     self.x += self.velX 
     self.y += self.velY 

    def collision(self): 
     global displayW 

     # collision to walls 
     if(self.x <= 0): 
      self.velX = 0 
     elif(self.x + self.h) >= displayW: 
      self.velX = 0 


# SETUP 
def setup(): 
    pygame.display.set_caption("Food Catcher") 

# QUIT GAME 
def quitGame(): 
    pygame.quit() 
    quit() 

# CALL MAIN 
if(__name__ == "__main__"): 
    main() 

回答

1

你繪製函數似乎罰款(雖然if player.y >= 200: player.draw()聲明似乎是不必要的)。

你可以通過在Food類的動畫函數中添加一個條件來實現。

def animate(self): 
    self.y += self.velY 
    # reset food if y position over 200 
    if self.y > 200: 
     self.y = -100 
     self.x = random.randrange(0, displayW) 

你的做法是行不通的,因爲你生成Food__init__功能的隨機x值。此行僅運行一次,因爲您只是實例化一個Food對象(在setGlobalValues函數中)。