Skip to content

Instantly share code, notes, and snippets.

@JeffersGlass
Created September 27, 2020 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffersGlass/8d98082d25f886f6566026c8a8d23fb6 to your computer and use it in GitHub Desktop.
Save JeffersGlass/8d98082d25f886f6566026c8a8d23fb6 to your computer and use it in GitHub Desktop.
import pygame
import sys #will use the exit function
#Loads all the main pygame modules and initializes them
pygame.init()
screen_width=640
screen_height=480
blue = (0,0,255)
location = [320,240]
direction = [1,1]
baseSpeed = 60 #speed in pixels per second
radius = 30
#create clock to keep time
myClock = pygame.time.Clock()
#Create a new Surface with the dimensions listed above
screen=pygame.display.set_mode((screen_width, screen_height))
#Loop forever:
while True:
dt = myClock.tick(1)
#The following makes the window close when clicking the "X" or exit button
#Without this, the window will not close, and will need to be killed
#with the task manager
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
screen.fill((0,0,0))
#(surface, color, center, radius)
pygame.draw.circle(screen, blue, (int(location[0]), int(location[1])), 30)
#move the circle
if location[0] <= radius: direction[0] = 1
if location[0] >= screen_width-radius: direction[0] = -1
if location[1] <= radius: direction[1] = 1
if location[1] >= screen_height-radius: direction[1] = -1
location[0] += direction[0] * baseSpeed * (dt/1000)
location[1] += direction[1] * baseSpeed * (dt/1000)
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment