Why isnt my player movement system working when I change the rectangle to a circle?

Question:

I made a system player movement system that works well with a rectangle but when I change it to a circle it just stops working!

import pygame as pg

# making screen
screen = pg.display.set_mode((500, 500))
# making clock to count ticks
clock = pg.time.Clock()
# making a rectangle
rect = pg.Rect(200, 400, 100, 10)
circ = pg.draw.circle(screen, (0, 255, 0), [300, 300], 10, 0)
speed = 6

done = False

while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    # moving the rectangle with player input
    keys = pg.key.get_pressed()
    if keys[pg.K_a] and not rect.x <= 0:
        rect.x -= speed
    if keys[pg.K_d] and not rect.x >= 400:
        rect.x += speed
    if keys[pg.K_s] and not rect.y >= 480:
        rect.y += speed
    if keys[pg.K_w] and not rect.y <= 320:
        rect.y -= speed
    if keys[pg.K_t]:
        circ.y -= speed

    screen.fill((40, 40, 40))

    pg.draw.rect(screen, (150, 200, 20), rect)
    pg.draw.circle(screen, (0, 255, 0), [300, 300], 10, 0)

    pg.display.flip()
    clock.tick(30)

I only want to move the circle with the ‘t’ key

Asked By: anothaone

||

Answers:

You must draw the circle at the center of the rectangle that sourounds the circle (circ), not at (300, 300):

pg.draw.circle(screen, (0, 255, 0), [300, 300], 10, 0)

pg.draw.circle(screen, (0, 255, 0), circ.center, 10, 0)

Note, pg.draw.circle does not create a circle object. It draw a circle on the screen and a pygame.Rect objet that rounds the circle. Therefore only create pygame.Rect object that indicates the position and size of the circle:

import pygame as pg

# making screen
screen = pg.display.set_mode((500, 500))
# making clock to count ticks
clock = pg.time.Clock()
# making a rectangle
rect = pg.Rect(200, 400, 100, 10)
circ = pg.Rect(290, 290, 20, 20)
speed = 6

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    # moving the rectangle with player input
    keys = pg.key.get_pressed()
    if keys[pg.K_a] and not rect.x <= 0:
        rect.x -= speed
    if keys[pg.K_d] and not rect.x >= 400:
        rect.x += speed
    if keys[pg.K_s] and not rect.y >= 480:
        rect.y += speed
    if keys[pg.K_w] and not rect.y <= 320:
        rect.y -= speed
    if keys[pg.K_t]:
        circ.y -= speed

    screen.fill((40, 40, 40))

    pg.draw.rect(screen, (150, 200, 20), rect)
    pg.draw.circle(screen, (0, 255, 0), circ.center, circ.width // 2, 0)

    pg.display.flip()
    clock.tick(30)
Answered By: Rabbid76

You will have to add coordinate-variables for the circle:

cx, cy = 300, 300

if keys[pg.K_t]:
    cy -= speed

pg.draw.circle(screen, (0, 255, 0), [cx, cy], 10, 0)
Answered By: zwitsch
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.