Pygame lab:使用“draw”创建Rectangle类& “移动”的方法

时间:2013-11-03 07:52:04

标签: pygame

我正在开发一个关于programarcadegames的实验室8:http://programarcadegames.com/index.php?chapter=lab_classes_and_graphics - 并且在Rectangle类中遇到了move方法。将欣赏任何提示。

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()

2 个答案:

答案 0 :(得分:1)

你必须更改draw()因为在每个循环中你在同一个地方绘制矩形,所以你看不到移动的结果。

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

答案 1 :(得分: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()
相关问题