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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| ###### BALLE REBONDISSANTE PAR CEDRIC B ######
import pygame
from math import sqrt
from pygame.locals import *
pygame.init()
# Defining some colors
black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
gray=(115,115,115)
# Defining screen dimension constants
WIDTH = 900
HEIGHT = 650
# Defining initial position and initial speed
position=[150,325]
speed=[25,29]
# Defining balls
redBall=(position, speed)
greenBall1=((300,275), 70)
greenBall2=((600,275), 70)
greenBall3=((450,450), 70)
# Set the height and width and title of the screen
size=[WIDTH,HEIGHT]
screen=pygame.display.set_mode(size, pygame.HWSURFACE)
pygame.display.set_caption("Collisions")
#Loop until the user clicks the close button
done=False
# Used to manage how fast the screen updates
clock=pygame.time.Clock()
def Draw(redBall, greenBall1,greenBall2,greenBall3):
screen.fill(white)
pygame.draw.circle(screen, red, (redBall[0][0],redBall[0][1]) ,20,0)
pygame.draw.circle(screen, green, greenBall1[0],greenBall1[1] ,0)
pygame.draw.circle(screen, green, greenBall2[0],greenBall2[1],0)
pygame.draw.circle(screen, green, greenBall3[0],greenBall3[1],0)
def CircularCollide(position, speed, center, radius):
collideSquaredDistance=(center[0]-position[0])**2+(center[1]-position[1])**2
if (collideSquaredDistance<(20+radius)**2):
pygame.draw.circle(screen, red, center, radius,0)
unitaryNormalVect=[(position[0]-center[0])/sqrt(collideSquaredDistance),(position[1]-center[1])/sqrt(collideSquaredDistance)]
unitaryTangentVect=[-unitaryNormalVect[1],unitaryNormalVect[0]]
normalSpeed=unitaryNormalVect[0]*speed[0]+unitaryNormalVect[1]*speed[1]
tangentSpeed=unitaryTangentVect[0]*speed[0]+unitaryTangentVect[1]*speed[1]
normalSpeed=(-99*normalSpeed)/101
normalSpeedVect=[normalSpeed*unitaryNormalVect[0],normalSpeed*unitaryNormalVect[1]]
tangentSpeedVect=[tangentSpeed*unitaryTangentVect[0],tangentSpeed*unitaryTangentVect[0]]
resultSpeed=[normalSpeedVect[0]+tangentSpeedVect[0],normalSpeedVect[1]+tangentSpeedVect[1]]
return resultSpeed
else:
return speed
def RectangularCollide(position, speed):
# rectangular collision with screen boundaries
if (position[0]>880 and speed[1]>0):
return (880, position[1],-speed[0],speed[1])
elif (position[0]>880 and speed[1]<0):
return (880,position[1],-speed[0],speed[1])
elif (position[1]>630 and speed[0]>0):
return (position[0], 630,speed[0],-speed[1])
elif (position[1]>630 and speed[0]<0):
return (position[0], 630,speed[0],-speed[1])
elif (position[0]<20 and speed[1]>0):
return (20, position[1],-speed[0],speed[1])
elif (position[0]<20 and speed[1]<0):
return (20, position[1],-speed[0],speed[1])
elif (position[1]<20 and speed[0]>0):
return (position[0], 20,speed[0],-speed[1])
elif (position[1]<20 and speed[0]<0):
return (position[0], 20,speed[0],-speed[1])
else:
return (position[0],position[1], speed[0],speed[1])
# -------- Main Program Loop ---------
while done==False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done=True
Draw(redBall, greenBall1,greenBall2,greenBall3)
position[0]+=speed[0]
position[1]+=speed[1]
rectCollideResult=RectangularCollide(position,speed)
position[0]=rectCollideResult[0]
position[1]=rectCollideResult[1]
speed[0]=rectCollideResult[2]
speed[1]=rectCollideResult[3]
speed=CircularCollide(position, speed, greenBall1[0], greenBall1[1])
speed=CircularCollide(position, speed, greenBall2[0], greenBall2[1])
speed=CircularCollide(position, speed, greenBall3[0], greenBall3[1])
clock.tick(30)
pygame.display.update()
pygame.quit() |
Partager