Skip to content

Instantly share code, notes, and snippets.

@benhoyt
Created October 11, 2012 05:11
Show Gist options
  • Save benhoyt/3870305 to your computer and use it in GitHub Desktop.
Save benhoyt/3870305 to your computer and use it in GitHub Desktop.
Bouncing ball with rotation
"""Simple bouncing ball demo."""
import sys
import pygame
pygame.init()
size = (1024, 768)
speed = [1, 1]
black = (0, 0, 0)
screen = pygame.display.set_mode(size)
ball = pygame.image.load('ball2.png')
ballrect = ball.get_rect()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > size[0]:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > size[1]:
speed[1] = -speed[1]
screen.fill(black)
if speed == [1, 1]:
angle = -45
if speed == [1, -1]:
angle = 45
if speed == [-1, -1]:
angle = -225
if speed == [-1, 1]:
angle = -135
rotated_ball = pygame.transform.rotate(ball, angle)
screen.blit(rotated_ball, ballrect)
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment