Hey guys, hope I'm in the right section here, I presume Pygame falls under python.
I'm trying to make a circle move left,right,up or down depending on the users input (e.g if the user presses the "a" key then the circle will move left by itself, rebound off the edge of the screen and move right and repeat until the user inputs another direction or quits the program). but for the life of me cant't get it to work. Here is the code, any help would be much appreciated :)
'''Moves a box in the direction the user inputs.
box controls: up or down - moves box up and down. left or right - moves box left and right.
Circle controls: w or s - moves circle up and down. a or d - moves circle left and right.
'''
import pygame
pygame.init()
screen = pygame.display.set_mode((600,480))
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255,255,0))
box = pygame.Surface((25,25))
box = box.convert()
box.fill((255,0,0))
box_x = 300
box_y = 200
direction = "still"
dire = "still"
clock = pygame.time.Clock()
keepGoing = True
#Loop
while keepGoing:
#Time
clock.tick(30)
#Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False#
color = 0,255,0
pos = x,y = 100,200
radius = 20
width = 0
circle = pygame.draw.circle(background, color, pos, radius, width)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
direction = "right"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
direction = "left"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
direction = "up"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
direction = "down"
if event.type == pygame.KEYDOWN:
if event.type == pygame.K_a:
dire = "left"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_d:
dire = "right"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
dire = "up"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
dire = "down"
if dire == "left":
x -= 5
if x <= 0:
dire = "right"
if dire == "right":
x += 5
if x >= screen.get_width():
dire = "left"
if direction == "right":
box_x += 5
if box_x >= screen.get_width():
direction = "left"
if direction == "left":
box_x -= 5
if box_x <= 0:
direction = "right"
if direction == "right":
box_x += 5
if direction == "up":
box_y -= 5
if box_y <= 0:
direction = "down"
if direction == "down":
box_y += 5
if box_y >= screen.get_height():
direction = "up"
screen.blit(background, (0, 0))
screen.blit(box, (box_x,box_y))
pygame.display.update(circle, (pos))
pygame.display.flip()