Oscar Wilson
Oscar Wilson

Reputation: 59

Updating the position of a rectangle

I am trying to get familiar with pygame so am starting with a simple program that draws a rectangle and moves it around with the mouse. The rectangle draws fine however it does not move with the mouse position and I cannot think why.

I found one other with this problem however this fix didnt work for me and was much more long winded than I felt it needed to be.

pygame.init()

screen = pygame.display.set_mode((500,300))

# --- mainloop / event loop ---
running = True
playerstartposX = 100
playerstartposY = 100
playerwidth = 50
playerheight = 50
screen.fill((30,30,30))
newrect = pygame.draw.rect(screen, (255,0,0) , ( playerstartposX ,
                                                 playerstartposY ,
                                                 playerwidth ,
                                                 playerheight))

pygame.display.update()

while running:
    pygame.time.delay(100)
    for event in pygame.event.get():

        if event.type == pygame.QUIT: 
            running = False

        elif event.type == 4: #if event is mouse motion
            newrect.move(event.pos) #move the rectangle to mouse pos

    pygame.display.update()


pygame.quit()

Upvotes: 2

Views: 997

Answers (2)

luis.parravicini
luis.parravicini

Reputation: 1217

First, the event type you need to check against is pygame.MOUSEMOTION (I believe it's 1024?) and you are checking with 4.

Then you have to redraw the rect on every iteration of the main loop to reflect the updated position

Upvotes: 0

Rabbid76
Rabbid76

Reputation: 210878

4 is not an event type. An event type is MOUSEMOTION (see pygame.event).

Create a pygame.Rect object:

newrect = pygame.Rect(playerstartposX, playerstartposY, playerwidth, playerheight)

Change its position when the event occurs:

if event.type == pygame.MOUSEMOTION: 
    newrect.center = event.pos

In the main application loop you've to continuously

  • handle the events
  • clear the display
  • draw the scene respectively rectangle
  • update the display

If you want to control the frames per second, then you can pass a parameter to the method .tick() of pygame.time.Clock rather than pygame.time.delay:

import pygame

pygame.init()

screen = pygame.display.set_mode((500,300))
clock = pygame.time.Clock()

running = True
playerstartposX = 100
playerstartposY = 100
playerwidth = 50
playerheight = 50
newrect = pygame.Rect(playerstartposX, playerstartposY, playerwidth, playerheight)

while running:
    clock.tick(60)

    # handle the events
    for event in pygame.event.get():

        if event.type == pygame.QUIT: 
            running = False

        if event.type == pygame.MOUSEMOTION: 
            newrect.center = event.pos

    # clear the display
    screen.fill((30,30,30))

    # draw the rectangle
    pygame.draw.rect(screen, (255,0,0), newrect)

    # update the display
    pygame.display.update()

pygame.quit()

Upvotes: 3

Related Questions