-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbode.py
More file actions
213 lines (161 loc) · 5.99 KB
/
bode.py
File metadata and controls
213 lines (161 loc) · 5.99 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# bode.py - a game by guexel@gmail.com
import math
import random
import pygame
width=1100
height=800
numPlatforms=40
sizeFrame=5
slowMo=0.015
colorBG=(0,0,0)
colorPlat1=(40,200,100)
colorPlat2=(120,250,180)
colorBut=(250,30,30)
colorBode=(255,255,255)
def dessineNouvellePlateforme(win,cestLeBut):
if cestLeBut:
couleur = colorBut
else:
couleur = (
random.randint ( colorPlat1[0], colorPlat2[0] ),
random.randint ( colorPlat1[1], colorPlat2[1] ),
random.randint ( colorPlat1[2], colorPlat2[2] )
)
larg = random.randint ( width//20, width//6 )
x1 = random.randint ( sizeFrame, width-sizeFrame-larg )
x2 = x1+larg
y = random.randint ( sizeFrame+height//20, height-sizeFrame-height//15-larg//5 )
while (x1 < x2):
pygame.draw.line ( win, couleur, (x1,y), (x2,y) )
x1 = x1 + random.randint(0,3)
x2 = x2 - random.randint(0,3)
y=y+1
def dessinePlatformes(win):
for n in range(numPlatforms-1):
dessineNouvellePlateforme ( win, False )
dessineNouvellePlateforme ( win, True ) # le tout dernier sera le But
def dessineFrame(win):
pygame.draw.rect ( win, colorPlat1, ((0,0),(sizeFrame,height)) )
pygame.draw.rect ( win, colorPlat1, ((width-sizeFrame,0),(sizeFrame,height)) )
pygame.draw.rect ( win, colorPlat1, ((0,0),(width,sizeFrame)) )
pygame.draw.rect ( win, colorPlat1, ((0,height-sizeFrame),(width,sizeFrame)) )
def getCollisions(win,x,y):
coll=[[0,0,0],[0,0,0],[0,0,0]]
numCollisions=0
for dx in (-1,0,1):
for dy in (-1,0,1):
couleurPixel = win.get_at((x+dx,y+dy))
if (not (couleurPixel==colorBG)):
numCollisions = numCollisions+1
coll[dx+1][dy+1]=1
if (couleurPixel==colorBut):
coll[dx+1][dy+1]=2
return ( numCollisions, coll )
def bodeAvance ( win, bode, vitesse ):
# efface l'ancienne position
pygame.draw.rect ( win, colorBG, ((int(bode[0])-1,int(bode[1])-1),(3,3)) )
bode[0] = bode[0]+vitesse[0]*slowMo;
bode[1] = bode[1]-vitesse[1]*slowMo; # moins vitesse, au lieu de plus, parce que les ordonnees montent vers en bas
collisions = getCollisions ( win, int(bode[0]), int(bode[1]) )
if (collisions[0] > 0):
# revient `a la position de depart
bode[0] = bode[0]+vitesse[0]*slowMo*-1;
bode[1] = bode[1]-vitesse[1]*slowMo*-1;
pygame.draw.rect ( win, colorBode, ((int(bode[0])-1,int(bode[1])-1),(3,3)) )
return collisions
def touteLaColonne(coll,x):
return (coll[x][0] > 0) and (coll[x][1] > 0) and (coll[x][2] > 0)
def touteLaLigne(coll,y):
return (coll[0][y] > 0) and (coll[1][y] > 0) and (coll[2][y] > 0)
def unDeLaLigne(coll,y,min=1):
return (coll[0][y] >= min) or (coll[1][y] >= min) or (coll[2][y] >= min)
def bodeJump ( win, bode, vitesse ):
clock = pygame.time.Clock()
finJeu=False
finJump=False
while (not finJump):
for event in pygame.event.get():
if event.type == pygame.QUIT:
finJeu=True
finJump=True
vitesse[1] = vitesse[1]-8.5*slowMo # gravite
collisions = bodeAvance ( win, bode, vitesse )
if (collisions[0] > 0):
vitesseTotale = math.sqrt ( vitesse[0]*vitesse[0] + vitesse[1]*vitesse[1] )
if (
touteLaColonne ( collisions[1], 0 )
or
touteLaColonne ( collisions[1], 2 )
):
# rebond dans un mur lateral
vitesse[0]=-vitesse[0]
elif (touteLaLigne ( collisions[1], 0 )):
# rebond dans le plafond
vitesse[1]=-vitesse[1]
elif (collisions[1][0][0] > 0):
# coin haut a gauche
vitesse[0] = abs(vitesse[0])*0.4
vitesse[1] = 0
elif (collisions[1][2][0] > 0):
# coin haut a droite
vitesse[0] = -abs(vitesse[0])*0.4
vitesse[1] = 0
elif (unDeLaLigne ( collisions[1], 2 )):
# par terre
if (vitesseTotale < 12):
# il s'arrete
finJump = True
finJeu = (unDeLaLigne ( collisions[1], 2, 2 ))
else:
# rebond par terre
vitesse[0] = vitesse[0]*0.7;
vitesse[1] = -vitesse[1]*0.7;
else:
# autre collision...
if (vitesseTotale < 1):
finJump=True
else:
vitesse[0] = 0
vitesse[1] = 0
pygame.display.flip()
clock.tick(500//slowMo)
return finJeu
def choisirPositionInitiale(win):
cherche=True
while (cherche):
p = [
random.randint(sizeFrame+10,width-sizeFrame-10),
random.randint(height-height//4,height-height//10)
]
collisions = getCollisions ( win, int(p[0]), int(p[1]) )
cherche = (collisions[0] > 0)
return p
def main():
pygame.init()
win=pygame.display.set_mode((width,height))
dessineFrame(win)
dessinePlatformes(win)
pygame.display.flip()
finJeu=False
bode = choisirPositionInitiale(win)
finJeu = bodeJump ( win, bode, [5,0] ) # laisse tomber
numTirs = 0
while (not finJeu):
numTirs = numTirs+1
angle = float ( input ( "Tir #"+str(numTirs)+" - Angle : " ) )
linearSpeed = float ( input ( "Vitesse : " ) )
linearSpeed = ((linearSpeed / 100) ** 0.5) * 100 # ajuste quadratico para vitesse 50 subir metade da vitesse 100
if (linearSpeed > 200):
linearSpeed = 200
finJeu = bodeJump (
win,
bode,
[
math.cos(math.radians(angle))*linearSpeed,
math.sin(math.radians(angle))*linearSpeed
]
)
print ( 'THE END !!!' )
input ( "Appuyez sur Enter..." )
pygame.quit()
main()