Skip to content

Instantly share code, notes, and snippets.

@benhoyt benhoyt/gist:3870305
Created Oct 11, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.