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

Recommended Answers

All 4 Replies

You do not describe your problem. At least many of your ifs should be elifs.

My problem is that i can't get the circle to budge.
The box works fine with ifs instead of elifs so thts not the problem.
If my logic behind moving the circle is fine then i think the core of the problem is in updating the circle which i cant find a way to do.

To get the events you need to have them all together.

#Events
for event in pygame.event.get():
    if event.type == pygame.QUIT:
        keepGoing = False#

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_RIGHT:
            direction = "right"

        if event.key == pygame.K_LEFT:
            direction = "left"
        #etc....
        #put all the "key" events under the keypress event type.

Also, you can probably put...

color = 0,255,0
pos = x,y = 100,200
radius = 20
width = 0
circle = pygame.draw.circle(background, color, pos, radius, width)

... above the while loop. You don't need to call it every succession of the loop. That keeps initializing the circle object too which might be part of the problem.

Yeah tht makes sense, dont know why i didnt think of tht!
Thts done but i now need to blit or update the circle which i dont know how to do.
Any ideas??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.