Skip to content

Instantly share code, notes, and snippets.

@gerryjenkinslb
Created July 30, 2020 06:34
Show Gist options
  • Save gerryjenkinslb/606f2688bd0d33f3c75f8f80fe01b088 to your computer and use it in GitHub Desktop.
Save gerryjenkinslb/606f2688bd0d33f3c75f8f80fe01b088 to your computer and use it in GitHub Desktop.
Basic Animation Technique in PyGame shown in my video: https://youtu.be/gUa1j7gY0yY
import math
import pygame
# initial setups
pygame.init()
screen_size = 500
surface = pygame.display.set_mode((screen_size, screen_size)) # window size and pixels width/height
# animation state variables
angle_per_step = .05 # degrees
step = 0
# animation loop
while True:
# erase background each time
surface.fill((0, 0, 0)) # erase surface memory before we draw new things
# calculate and draw rotating line
angle = step * angle_per_step
line_len = screen_size * .8
cx = cy = screen_size // 2 # center of rotation
x = line_len/2.0*math.sin(angle)
y = line_len/2.0*math.cos(angle)
pygame.draw.line(surface, (255,255,0), ((cx+x),(cy+y)),((cx-x),(cy-y)), 4)
# update to display, await clock check for quit advance animation state
pygame.display.update()
pygame.time.Clock().tick(60)
if pygame.event.peek(pygame.QUIT): # detect user quit
break
step += 1 # advance state of animation
pygame.quit() # close window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment