forked from segerphilip/impossible_game_sofdes
/
collision_detection_bounceball.py
65 lines (53 loc) · 1.48 KB
/
collision_detection_bounceball.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 5 20:07:38 2014
@author: ihill
"""
import sys, pygame
import time as time
pygame.init()
size = width, height = 320, 480
speed = [2, 2]
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("ball.png")
wall = pygame.image.load("wall.png")
ballred = pygame.image.load("ballred.png")
ballrect = ball.get_rect()
wallrect = wall.get_rect()
wallrect.left = 96
wallrect.top = 240
degree = 0
rotWallrect = wallrect
blitWall = screen.blit(wall, wallrect)
oldcenter = blitWall.center
while 1:
center = ballrect.center
collide = False
screen.fill(black)
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
if ballrect.colliderect(rotWallrect):
collide = True
#blitWall = screen.blit(wall, wallrect)
#oldcenter = blitWall.center
rotWall = pygame.transform.rotate(wall,degree)
rotWallrect = rotWall.get_rect()
rotWallrect.center = oldcenter
screen.blit(rotWall,rotWallrect)
degree += 1
if degree > 360:
degree = 0
if collide:
screen.blit(ballred, ballrect)
else:
screen.blit(ball, ballrect)
oldcenter = blitWall.center
screen.blit(ball, [0,0])
pygame.display.flip()
time.sleep(.05)