user2949385
user2949385

Reputation: 1

Pygame lab: creating Rectangle class with "draw" & "move" method

I'm working on a lab 8 on programarcadegames: http://programarcadegames.com/index.php?chapter=lab_classes_and_graphics - and got stuck with move method in a Rectangle class. Will appreciate any hints.

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

Upvotes: 0

Views: 2517

Answers (2)

user2949385
user2949385

Reputation: 1

Very good hints furas - especially on order of drawing on screen. I did play a little bit more, set few debugs (prints) and eventually got it sorted. The issue was with "move" method - as each time the screen was drawn - I was resetting "change_x" and "change_y" to values given in method call. Of course that was wrong. What I did is I created extra method "speed" and I'm calling it BEFORE main program loop. The correct version is listed below. For the sake of clarity I changed "change_x" and "change_y" to "speed_x" and "speed_y". We may close this topic

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

Upvotes: 0

furas
furas

Reputation: 142681

You have to change draw() because in every loop you draw retangle in the same place so you can't see result of your move.

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

Upvotes: 1

Related Questions