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