Pygame arrow keys not working

Sometimes it bugs and you have to restart a few times (don’t forget to click the screen when u start)

1 Like

Hello again. I have one final question about the code.

import pygame
import random
import time

pygame.init()
surface = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('Basketball!')
color = (255, 254, 220)

width = 5
height = 5

place_circ_x = random.randint(1, 1000)
place_circ_y = random.randint(1, 600)
place_rect = pygame.Rect(random.randint(1, 1000), random.randint(1, 600), 120, 70)

while True:
    # handle events
    for event in pygame.event.get():
        # if you press the X button to close the window
        if event.type == pygame.QUIT:
            break

    # check for keys held down
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and place_circ_x > 0:
    	place_circ_x -= 7
		time.sleep(2)
    if keys[pygame.K_RIGHT] and place_circ_x > 1000 - width:
        place_circ_x += 7
		time.sleep(2)
    if keys[pygame.K_UP] and place_circ_y > 0:
        place_circ_y -= 7
		time.sleep(2)
    if keys[pygame.K_DOWN] and place_circ_y > 600 - height: 
        place_circ_y += 7
		time.sleep(2)
    
    # clear the screen and redraw objects
    surface.fill(color)
    pygame.draw.circle(surface,"orange", (place_circ_x, place_circ_y), 15)
    pygame.draw.rect(surface, (220, 220, 220), place_rect)
    pygame.draw.ellipse(surface, "black", (place_rect.x + 40, place_rect.y + 50, 40, 40), 5)

	# update the screen
    pygame.display.flip()

In the

 if keys[pygame.K_LEFT] and place_circ_x > 0:
    	place_circ_x -= 7
		time.sleep(2)

block, the repl says that there is incorrect indentation in the time.sleep(2) part.

Why? I don’t know. Maybe someone can explain? Please?

Either it’s a bug with Replit or you 2 tabbed it instead of 1 tab. Try to put 1 tab of indentation for the block instead of 2.

1 Like

The indentation is inconsistent, I recommend pressing Ctrl + S as it formats your code which fixes the indentation.

2 Likes

So, i tried both of your ways and neither worked.

I’m thinking this is just a bug. But how do I get rid of it?

2 Likes

Refresh the page, or highlight the whole file and CTRL + S.

1 Like

Sorry, format code feature is pretty buggy. Maybe try closing the tab and opening a new one, or refresh the page as @anon45021817 suggested.

Hate to say this, but it still didn’t work.

I appreciate you for going out of the way to help me with this so much!

CtrlH replace \t with (4 spaces), or vice versa

3 Likes

If none of our options work, it’s most likely your repl. Can you send a screencap of the error you’re getting?

2 Likes


Here.

Try Ctrl + H find \t and replace with two spaces

No error now, but the ball isn’t moving! :dotted_line_face:

I see a couple things that won’t quite work here, but are an easy fix. The first is that pygame provides a clock object that will provide you with a constant frame rate. That way as processing increases or decreases, the motion will remain smooth. This also has the effect of giving back processing cycles so you don’t end up with such a tight loop that you are only ever drawing.

First, add this up where you initialize the surface:
clock = pygame.time.Clock()

Then, get rid of any sleeps in your loop. Continue to react to keystrokes as you are – just don’t sleep. Instead, at the end of your loop, add this:
clock.tick(30)

The ‘30’ here is the frame rate you are targeting, so it can be adjusted to whatever works best for your game. More documentation on clock.tick is here.

Next, your loop currently runs very fast except for the sleeps, and it is redrawing every time in the loop (and the fill and other graphics operations are very expensive). So the next step would be to introduce a variable for knowing when a redraw is needed. Create a variable called “redrawNeeded” before your loop (set to False). Whenever your position actually changes, set it to true and put your redraw logic inside an if statement that only runs when this is true. And of course set it to false inside the statement.

That should make it very reactive to keystrokes and provide smooth motion with minimal compute.

2 Likes

Change keys[pygame.K_LEFT] to just pygame.K_LEFT (do that for all the directions, not just left)

1 Like

Welcome to replit ask, aka replit but not replit