Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Created February 12, 2023 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save primaryobjects/d1ee3bc616f2d75872e3bdf85fd0c6e7 to your computer and use it in GitHub Desktop.
Save primaryobjects/d1ee3bc616f2d75872e3bdf85fd0c6e7 to your computer and use it in GitHub Desktop.
Example of a simple game with smooth keyboard movement of a ball around the screen. Uses pygame and Python.
import pygame
# Initialize pygame
pygame.init()
# Define colors
black = (0, 0, 0)
# Set the height and width of the screen
size=[800, 600]
screen=pygame.display.set_mode(size)
# Set title of screen
pygame.display.set_caption("My Game")
# Used to manage how fast the screen updates
clock=pygame.time.Clock()
#Load Image
ball = pygame.image.load("ball.png")
# Set speed, direction and position of ball
x_speed = 5
y_speed = 5
def handle_keypress(keys_pressed, velocity_x, velocity_y):
if keys_pressed[pygame.K_LEFT]:
velocity_x -= x_speed
elif keys_pressed[pygame.K_RIGHT]:
velocity_x += x_speed
else:
if velocity_x < 0:
velocity_x += x_speed
if velocity_x > 0:
velocity_x = 0
if velocity_x > 0:
velocity_x -= x_speed
if velocity_x < 0:
velocity_x = 0
if keys_pressed[pygame.K_UP]:
velocity_y -= y_speed
elif keys_pressed[pygame.K_DOWN]:
velocity_y += y_speed
else:
if velocity_y < 0:
velocity_y += y_speed
if velocity_y > 0:
velocity_y = 0
if velocity_y > 0:
velocity_y -= y_speed
if velocity_y < 0:
velocity_y = 0
print(velocity_x, velocity_y)
return (velocity_x, velocity_y)
def move(x_coord, y_coord, velocity_x, velocity_y):
x_coord += velocity_x
if x_coord < 0:
x_coord = 0
velocity_x = 0
elif x_coord > size[0] - ball.get_width():
x_coord = size[0] - ball.get_width()
velocity_x = 0
y_coord += velocity_y
if y_coord < 0:
y_coord = 0
velocity_y = 0
elif y_coord > size[1] - ball.get_height():
y_coord = size[1] - ball.get_height()
velocity_y = 0
return (x_coord, y_coord, velocity_x, velocity_y)
# -------- Main Program Loop -----------
def game_loop():
velocity_x = 0
velocity_y = 0
x_coord = 10
y_coord = 10
done = False
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
done = True
keys_pressed = pygame.key.get_pressed()
velocity_x, velocity_y = handle_keypress(keys_pressed, velocity_x, velocity_y)
x_coord, y_coord, velocity_x, velocity_y = move(x_coord, y_coord, velocity_x, velocity_y)
#Clears the screen
screen.fill(black)
#Draw image onto screen
screen.blit(ball, [x_coord, y_coord])
# Limit to 20 frames per second
clock.tick(20)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
game_loop()
pygame.quit()
@primaryobjects
Copy link
Author

primaryobjects commented Feb 12, 2023

screenshot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment