2013-11-03 112 views
0

我正在研究programarcadegames上的實驗8:http://programarcadegames.com/index.php?chapter=lab_classes_and_graphics - 並被Rectangle類中的移動方法卡住。將欣賞任何提示。Pygame實驗:用「繪製」和「移動」方法創建矩形類

import pygame 

black = ( 0, 0, 0) 
white = (255, 255, 255) 
green = ( 0, 255, 0) 
red  = (255, 0, 0) 

class Rectangle(): 
    def draw(self, x, y, height, width, screen): 
     self.x = x 
     self.y = y 
     self.height = height 
     self.width = width 
     self.screen = screen 
     pygame.draw.rect(self.screen, white, [self.x, self.y, self.height, self.width]) 
     print "meth draw: x,y", self.x, self.y 
    def move(self, change_x, change_y): 
     self.change_x = change_x 
     self.change_y = change_y 

     self.x += self.change_x 
     self.y += self.change_y 

     if self.x > 300 or self.x < 0: 
      self.change_x = -self.change_x 
     if self.y > 300 or self.y < 0: 
      self.change_y = -self.change_y 

     print "x,y,s_x,s_y", self.x, self.y, self.change_x, self.change_y # debug 

pygame.init() 
size = [300,300] 
screen = pygame.display.set_mode(size) 
pygame.display.set_caption("My Game") 
done = False 
clock = pygame.time.Clock() 

myObject = Rectangle() 

# -------- Main Program Loop ----------- 
while done == False: 
    # ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT 
    for event in pygame.event.get(): # User did something 
     if event.type == pygame.QUIT: # If user clicked close 
      done = True # Flag that we are done so we exit this loop 
    screen.fill(black) 

    myObject.draw(100, 100, 50, 50, screen) 
    myObject.move(10, 10) 
    pygame.display.flip() 
    clock.tick(20) 
pygame.quit() 

回答

1

你必須改變draw()因爲每一次循環,你在同一個地方畫retangle所以你不能看到你的舉動的結果。

class Rectangle: 

    def __init__(self, x, y, width, height, screen): 
     self.x = x 
     self.y = y 
     self.width = width 
     self.height = height 
     self.screen = screen 

    def draw(self): 
     pygame.draw.rect(self.screen, white, [self.x, self.y, self.height, self.width]) 
     print "meth draw: x,y", self.x, self.y 

    def move(self, change_x, change_y): 
     # rest of your code 

# rest of your code 

myObject = Rectangle(100, 100, 50, 50, screen) 

# rest of your code 

myObject.move(10, 10) # move to new place 
myObject.draw() # draw in new place 
0

很好的提示furas - 特別是在屏幕上繪圖的順序。我確實玩了一些,設置了很少的調試(打印),並最終將其分類。問題在於「移動」方法 - 因爲每次繪製屏幕時 - 我都將「change_x」和「change_y」重置爲方法調用中給出的值。當然這是錯誤的。我所做的是我創建了額外的方法「速度」,我在主程序循環之前調用它。 下面列出了正確的版本。爲了清楚起見,我將「change_x」和「change_y」更改爲「speed_x」和「speed_y」。 我們可能會關閉此主題

import pygame 

# Define some colors 
black = ( 0, 0, 0) 
white = (255, 255, 255) 
green = ( 0, 255, 0) 
red  = (255, 0, 0) 

class Rectangle(): 
    def __init__(self, x, y, width, height): 
     self.x = x 
     self.y = y 
     self.height = height 
     self.width = width 
    def speed(self,*speed): 
     self.speed_x = speed[0] 
     self.speed_y = speed[1] 
    def draw(self, screen): 
     self.screen = screen 
     pygame.draw.rect(self.screen, white, [self.x, self.y, self.height, self.width]) 
     print "meth draw: x,y", self.x, self.y 
    def move(self): 
     print "self.x", self.x 
     print "self.speed_x:", self.speed_x 
     self.x += self.speed_x 
     self.y += self.speed_y 
     if self.x > 250 or self.x < 0: 
      self.speed_x = -self.speed_x 
     if self.y > 250 or self.y < 0: 
      self.speed_y = -self.speed_y 


pygame.init() 

# Set the width and height of the screen [width,height] 
size = [300,300] 
screen = pygame.display.set_mode(size) 

pygame.display.set_caption("My Game") 

done = False 

clock = pygame.time.Clock() 

myObject = Rectangle(100, 100, 50, 50,) 
myObject.speed(2,4) 


# -------- Main Program Loop ----------- 
while done == False: 
    for event in pygame.event.get(): # User did something 
     if event.type == pygame.QUIT: # If user clicked close 
      done = True # Flag that we are done so we exit this loop 


    screen.fill(black) 


    myObject.move() 
    myObject.draw(screen)              

    pygame.display.flip() 

    clock.tick(20) 

pygame.quit() 
+0

您可以將我的答案標記爲已接受。順便說一下:通常你應該編輯你的問題來添加更多信息 - 你不必創建答案。然後,您可以在我的答案下方發表評論,並收到消息。當有人添加答案時,我不會收到消息。 – furas