Hi!
I am @AndreiM294.
I am making a simple basketball game (using Pygame) in which you have to move the ball into the hoop. It isn’t done yet, but I have already ran into an error. I used all the right functions, but when I press the arrow keys, the ball doesn’t move at all!
Please help!
Here is the code:
import pygame
import random
pygame.init()
surface = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('Basketball!')
color = (255, 254, 220)
surface.fill(color)
width = 5
height = 5
place_circ_x = random.randint(1, 1000)
place_circ_y = random.randint(1, 600)
pygame.draw.circle(surface,"orange",(place_circ_x, place_circ_y), 15)
place_rect_x = random.randint(1, 1000)
place_rect_y = random.randint(1, 600)
pygame.draw.rect(surface,(220,220,220),pygame.Rect(place_rect_x, place_rect_y, 120, 70))
pygame.draw.ellipse(surface, "black",(place_rect_x + 40, place_rect_y + 50,40, 40), 5)
while True:
for i in pygame.event.get():
keys = pygame.key.get_pressed()
if i.type == pygame.KEYDOWN:
if keys[pygame.K_LEFT] and place_circ_x > 0:
place_circ_x -= 7
if keys[pygame.K_RIGHT] and place_circ_x > 1000 - width:
place_circ_x += 7
if keys[pygame.K_UP] and place_circ_y > 0:
place_circ_y -= 7
if keys[pygame.K_DOWN] and place_circ_y > 600 - height:
place_circ_y += 7
pygame.display.flip()
P.S. I know the code isn’t indented, but try to ignore that.
I’ll be very grateful to whoever does help. 
1 Like
Hi there! If it’s at no inconvenience, can you post a repl link so that we can get a better idea of the code as a whole? Sometimes the line producing the dysfunction cannot be found in the code snippet. Looking at this, though, my guess would be that i.type
is not defined.
1 Like
Oops! I got confused with another post.
1 Like
This…this isn’t the Repl I believe that OP was talking about. Do you have the actual link by any chance?
1 Like
Sorry, got confused with another post. 
Ah no worries. Got confused as to why I saw Mario trapped in a wall instead of a basketball 
Sorry, I can’t. I just tried the link, but it said no links allowed in posts.
But I now see that you’re right; i.type
actually isn’t defined. If it won’t be too much work, can you explain to me how to define it without using too much excess code (like 1-10 lines)?
Thanks!
Some settings were changed and we are currently working on reverting those changes. Feel free to message me so I can post it for you
1 Like
But if you can’t, no worries! You helped me a lot anyway.
P.S. I’ll follow you now.
Thanks for helping once again!!!

Can you post the link please?
Yes if you message it to me. I think you should be able to
Maybe I could email it to you?
Odd. Read a few more topics/posts and you will automatically promoted and you should be able to post links then
Its very little. It just take less than 15 minutes
Hey, sorry for late response, got busy doing some stuff. Through some quick research, i.type
is either type(var)
, which just get’s a data’s type (not very useful here) or instead of i.type
, it is really event.type
. Most likely the second option. Feel free to copy and paste this code in place of your original snippet in your file. You can rewrite your snippet doing this:
import pygame
import random
pygame.init()
surface = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('Basketball!')
color = (255, 254, 220)
surface.fill(color)
i = pygame.event
width = 5
height = 5
place_circ_x = random.randint(1, 1000)
place_circ_y = random.randint(1, 600)
pygame.draw.circle(surface,"orange",(place_circ_x, place_circ_y), 15)
place_rect_x = random.randint(1, 1000)
place_rect_y = random.randint(1, 600)
pygame.draw.rect(surface,(220,220,220),pygame.Rect(place_rect_x, place_rect_y, 120, 70))
pygame.draw.ellipse(surface, "black",(place_rect_x + 40, place_rect_y + 50,40, 40), 5)
while True:
for i in i.get():
keys = pygame.key.get_pressed()
if i.type == pygame.KEYDOWN:
if keys[pygame.K_LEFT] and place_circ_x > 0:
place_circ_x -= 7
if keys[pygame.K_RIGHT] and place_circ_x > 1000 - width:
place_circ_x += 7
if keys[pygame.K_UP] and place_circ_y > 0:
place_circ_y -= 7
if keys[pygame.K_DOWN] and place_circ_y > 600 - height:
place_circ_y += 7
pygame.display.flip()
Hope this helps!
If that doesn’t work, simply change if i.type == pygame.KEYDOWN
to if event.type == pygame.KEYDOWN
.
One other thing is that you dont actually need to have pygame.key.get_pressed()
inside of the event loop. This function will tell you which keys are currently being pressed and held down and you don’t need to call it every time you loop through an event. You should put it outside of the for loop like this.
import pygame
import random
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
if keys[pygame.K_RIGHT] and place_circ_x > 1000 - width:
place_circ_x += 7
if keys[pygame.K_UP] and place_circ_y > 0:
place_circ_y -= 7
if keys[pygame.K_DOWN] and place_circ_y > 600 - height:
place_circ_y += 7
// 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()
1 Like