2

so I am making a game where this character(a circle) has to pop balloons falling from the sky to get points. But I am having trouble making my character move in the first place.

Code:

import pygame
from pygame.locals import *
pygame.init()

#Variables
white = (255, 255, 255)
blue = (70,130,180)
black = (0,0,0)
x = 400
y = 450
#screen stuff
screenwidth = 800
screenheight = 600

screen = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption("Balloon Game!")
#end of screen stuff

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
    # Draw Character
    character = pygame.draw.circle(screen, (blue), (x, y), 50, 50)
    #End of Drawing Character

    # Making Arrow Keys
    keyPressed = pygame.key.get_pressed()
    if keyPressed[pygame.K_LEFT]:
        character.x -= 1
    if keyPressed[pygame.K_RIGHT]:
        character.x += 1

    pygame.display.update()

I would appreciate it if someone could tell me why it wasn't working with a fixed code. Thanks!

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Ravishankar D
  • 131
  • 10

1 Answers1

2

pygame.draw.circle returns the bounding rectangle (pygame.Rect object) of the circle. However the center of the circle is always (x, y). Therefore you need to change x instead of character.x:

while True:
    # [...]

    pygame.draw.circle(screen, (blue), (x, y), 50, 50)

    keyPressed = pygame.key.get_pressed()
    if keyPressed[pygame.K_LEFT]:
        x -= 1
    if keyPressed[pygame.K_RIGHT]:
        x += 1

This code can even be simplified:

while True:
    # [...]

    pygame.draw.circle(screen, (blue), (x, y), 50, 50)

    keyPressed = pygame.key.get_pressed()
    x += (keyPressed[pygame.K_RIGHT] - keyPressed[pygame.K_LEFT])
Rabbid76
  • 202,892
  • 27
  • 131
  • 174