Bonjour,
Afin d'apprendre le Python, je suis actuellement en cours de programmation d'un petit jeu de type "Dodger". Cependant, je reste bloqué depuis un petit moment à cause d'une fonction que j'ai essayé de coder. Cette fonction a pour objectif de créer un menu "Options" et de modifier des variables qui serait susceptible d'augmenter ou de baisser la difficulté du jeu.

Mon code:
Code python : Sélectionner tout - Visualiser dans une fenêtre à part
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import pygame,random,sys,math
from pygame.locals import *
 
width,height=1024,768
textcolor=(255,255,255)
fps=60
bulletminspd=10
bulletmaxspd=12
addbullet=3
scoretick=60
movement=7
 
if addbullet==1:
    difficulty='Insane'
elif addbullet==2:
    difficulty='Very hard'
elif addbullet==3:
    difficulty='Hard'
elif addbullet==4:
    difficulty='Normal'
elif addbullet==5:
    difficulty='Easy'
 
if bulletminspd==15:
    speed='Speed x1.5 and score x2'
elif bulletminspd==10:
    speed='x1 and score x1'
elif bulletminspd==8:
    speed='x0.75 and score/3'
elif bulletminspd==5:
    speed='x0.5 and score/6'
 
def end():
    pygame.quit()
    sys.exit()
 
def presskey():
    while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                end()
            if event.type==KEYDOWN:
                if event.key==K_ESCAPE:
                    end()
                if event.key==K_SPACE:
                    return
 
def bouton():
    while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                end()
            if event.type==pygame.MOUSEBUTTONDOWN:
                mouse=pygame.mouse.get_pos()
                if buttonsurface2.collidepoint(mouse):
                    setting()
                if buttonsurface3.collidepoint(mouse):
                    end()
                if buttonsurface.collidepoint(mouse):
                    return
 
def hit(playerRect,bullet):
    for b in bullet:
        if playerRect.colliderect(b['rect']):
            return True
    return False
 
def texte(text,font,surface,x,y):
    cont=font.render(text,1,textcolor)
    contrect=cont.get_rect()
    contrect.topleft=(x,y)
    surface.blit(cont,contrect)
 
def setting():
    global addbullet, bulletminspd,bulletmaxspd,speed,difficulty
    window.blit(bg,(0,0))
    setbut=window.blit(button,((width/2.7),(height/4)))
    texte(difficulty,font,window,(width/2.15),(height/3.55))
    texte('Difficulté :',font,window,(width/2.3),(height/5))
    setbut2=window.blit(button,((width/2.7),(height/2.5)))
    texte(speed,font,window,(width/2.5),(height/2.34))
    texte('Speed and score :',font,window,(width/2.58),(height/2.8))
    buttonsurface=window.blit(button,((width/2.7),(height/(3/2))))
    texte('Start Game',font,window,(width/2.6),(height/1.43))
    buttonsurface3=window.blit(button,((width/2.7),(height/1.14)))
    texte('Quit Game',font,window,(width/2.6),(height/1.10))
    pygame.display.update()
 
    while True:
        for event in pygame.event.get():
            if event.type==pygame.MOUSEBUTTONDOWN:
                mouse=pygame.mouse.get_pos()
                if setbut.collidepoint(mouse):
                    if addbullet==3:
                        addbullet=2
                    elif addbullet==2:
                        addbullet=1
                    elif addbullet==1:
                        addbullet=5
                    elif addbullet==5:
                        addbullet=4
                    elif addbullet==4:
                        addbullet=3
                if setbut2.collidepoint(mouse):
                    if bulletminspd==10:
                        bulletminspd,bulletmaxspd=15,18
                    elif bulletminspd==15:
                        bulletminspd,bulletmaxspd=5,6
                    elif bulletminspd==5:
                        bulletminspd,bulletmaxspd=8,9
                    elif bulletminspd==8:
                        bulletminspd,bulletmaxspd=10,12
                pygame.display.update()
        break
 
def menu():
    pygame.mouse.set_visible(True)
    pygame.mixer.music.play(-1,0.0)
    texte('Bullet hell dodge game',pygame.font.Font('freesansbold.ttf',48),window,(width/4),(height/3))
    texte('Start Game',font,window,(width/2.6),(height/1.43))
    texte('Settings',font,window,(width/2.6),(height/1.25))
    texte('Quit Game',font,window,(width/2.6),(height/1.10))
    texte('Déplacement: HAUT, BAS, GAUCHE, DROITE', font2, window,(width-400),10)
    texte('Ralentir le vaisseau: SHIFT', font2, window,(width-400),35)
    pygame.display.update()
    bouton()
    pygame.mixer.music.stop()
 
pygame.init()
mainClock=pygame.time.Clock()
window=pygame.display.set_mode((width,height))
pygame.display.set_caption('Bullet hell dodge')
font=pygame.font.Font('freesansbold.ttf',24)
font2=pygame.font.Font('freesansbold.ttf',16)
playerimg=pygame.image.load('ship.png')
playerRect=playerimg.get_rect()
bulletimg=pygame.image.load('bullet.png')
bg=pygame.image.load('bg.jpg').convert()
pygame.mixer.music.load('menu.mp3')
window.blit(bg,(0,0))
button=pygame.image.load('button.png')
buttonsurface=window.blit(button,((width/2.7),(height/(3/2))))
buttonsurface2=window.blit(button,((width/2.7),(height/1.3)))
buttonsurface3=window.blit(button,((width/2.7),(height/1.14)))
menu()
pygame.mouse.set_visible(False)
 
top=0
while True:
    bullet=[]
    score=0
    playerRect.topleft=(width/2,height-32)
    moveLeft=moveRight=moveUp=moveDown=False
    baddcounter=0
    scorecounter=0
    songselect=[1,2,3,4]
    if random.choice(songselect)==1:
        pygame.mixer.music.load('music.mp3')
    elif random.choice(songselect)==2:
        pygame.mixer.music.load('music2.mp3')
    elif random.choice(songselect)==3:
        pygame.mixer.music.load('music3.mp3')
    elif random.choice(songselect)==4:
        pygame.mixer.music.load('music4.mp3')
    pygame.mixer.music.play(-1,0.0)
 
    while True:
 
        for event in pygame.event.get():
            if event.type==QUIT:
                end()
            if event.type==KEYDOWN:
                if event.key==K_LSHIFT:
                    movement=5
                if event.key==K_LEFT:
                    moveRight=False
                    moveLeft=True
                if event.key==K_RIGHT:
                    moveLeft=False
                    moveRight=True
                if event.key==K_UP:
                    moveDown=False
                    moveUp=True
                if event.key==K_DOWN:
                    moveUp=False
                    moveDown=True
 
            if event.type==KEYUP:
                if event.key==K_LSHIFT:
                    movement=7
                if event.key==K_ESCAPE:
                    end()
                if event.key==K_LEFT:
                    moveLeft=False
                if event.key==K_RIGHT:
                    moveRight=False
                if event.key==K_UP:
                    moveUp=False
                if event.key==K_DOWN:
                    moveDown=False
 
        scorecounter += 1
        if scorecounter==scoretick:
            scorecounter=0
            score=score+1
 
        baddcounter += 1
        if baddcounter==addbullet:
            baddcounter=0
            newbullet={'rect':pygame.Rect(random.randint(0,width-16),-16,16,16),
                       'speed':random.randint(bulletminspd,bulletmaxspd),
                       }
            bullet.append(newbullet)
 
        if moveLeft and playerRect.left>0:
            playerRect.move_ip(-1*movement,0)
        if moveRight and playerRect.right<width:
            playerRect.move_ip(movement,0)
        if moveUp and playerRect.top>0:
            playerRect.move_ip(0,-1*movement)
        if moveDown and playerRect.bottom<height:
            playerRect.move_ip(0,movement)
 
        for b in bullet:
            b['rect'].move_ip(0,b['speed'])
 
        for b in bullet[:]:
            if b['rect'].top>height:
                bullet.remove(b)
 
        window.blit(bg,(0,0))
        texte('Score: %s' % (score),font,window,10,0)
        texte('Top Score: %s' % (top),font,window,10,40)
 
        window.blit(playerimg,playerRect)
        for b in bullet:
            window.blit(bulletimg,b['rect'])
        pygame.display.update()
 
        if hit(playerRect,bullet):
            if score>top:
                top=score
            break
 
        mainClock.tick(fps)
 
    texte('GAME OVER',pygame.font.Font("freesansbold.ttf",48),window,(width/3),(height/3))
    texte('Appuyez sur espace pour rejouer.',font,window,(width/3),(height/(3/2)))
    pygame.display.update()
    presskey()
    pygame.mixer.music.stop()

C'est la fonction setting() dont il est question, j'ai essayé de mettre en place 2 boutons qui changent la valeur des variables lorsque l'on clique dessus mais aucun des 2 ne semblent fonctionner.
Je viens donc solliciter votre aide afin de trouver une solution à ce problème.

Merci par avance.

Edit: J'ai réussi à faire ce que je voulais en faisant de nombreux test pour voir où était mon erreur et j'en suis arrivé à cela:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import pygame,random,sys,math
from pygame.locals import *
 
width,height=1024,768
textcolor=(255,255,255)
fps=60
bulletminspd=10
bulletmaxspd=12
addbullet=3
scoretick=60
movement=7
 
def end():
    pygame.quit()
    sys.exit()
 
def presskey():
    while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                end()
            if event.type==KEYDOWN:
                if event.key==K_ESCAPE:
                    end()
                if event.key==K_SPACE:
                    return
 
def bouton():
    while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                end()
            if event.type==pygame.MOUSEBUTTONDOWN:
                mouse=pygame.mouse.get_pos()
                if buttonsurface2.collidepoint(mouse):
                    end()
                if buttonsurface.collidepoint(mouse):
                    return
 
def hit(playerRect,bullet):
    for b in bullet:
        if playerRect.colliderect(b['rect']):
            return True
    return False
 
def texte(text,font,surface,x,y):
    cont=font.render(text,1,textcolor)
    contrect=cont.get_rect()
    contrect.topleft=(x,y)
    surface.blit(cont,contrect)
 
def setting():
    window.blit(bg,(0,0))
    setbut=window.blit(button,((width/2.7),(height/4)))
    texte('Hard',font,window,(width/2.15),(height/3.55))
    texte('Difficulté :',font,window,(width/2.3),(height/5))
    setbut2=window.blit(button,((width/2.7),(height/2.5)))
    texte('Speed x1',font2,window,(width/2.15),(height/2.34))
    texte('Score x1',font2,window,(width/2.15),(height/2.22))
    texte('Speed and score :',font,window,(width/2.58),(height/2.8))
    buttonsurface=window.blit(button,((width/2.7),(height/(3/2))))
    texte('Start Game',font,window,(width/2.6),(height/1.43))
    pygame.display.update()
    a,b=0,0 
    if b==5:
        speed='Speed x1.5 and score x2'
    elif b==0:
        speed='x1 and score x1'
    elif b==15:
        speed='x0.75 and score/3'
    elif b==10:
        speed='x0.5 and score/6'
    while True:
        for event in pygame.event.get():
            if event.type==pygame.MOUSEBUTTONDOWN:
                mouse=pygame.mouse.get_pos()
                if setbut.collidepoint(mouse):
                    if a==4:
                        a=0
                    else:
                        a += 1
                    window.blit(button,((width/2.7),(height/4)))
                    if a==0:
                        texte('Hard',font,window,(width/2.15),(height/3.55))
                        pygame.display.update()
                    elif a==1:
                        texte('Very Hard',font,window,(width/2.27),(height/3.55))
                        pygame.display.update()
                    elif a==2:
                        texte('Insane',font,window,(width/2.18),(height/3.55))
                        pygame.display.update()
                    elif a==3:
                        texte('Easy',font,window,(width/2.15),(height/3.55))
                        pygame.display.update()
                    elif a==4:
                        texte('Normal',font,window,(width/2.22),(height/3.55))
                        pygame.display.update()
                if setbut2.collidepoint(mouse):
                    if b==15:
                        b=0
                    else:
                        b += 5
                    window.blit(button,((width/2.7),(height/2.5)))
                    if b==0:
                        texte('Speed x1',font2,window,(width/2.15),(height/2.34))
                        texte('Score x1',font2,window,(width/2.15),(height/2.22))
                        pygame.display.update()
                    if b==5:
                        texte('Speed x1.5',font2,window,(width/2.15),(height/2.34))
                        texte('Score x2',font2,window,(width/2.15),(height/2.22))
                        pygame.display.update()
                    if b==10:
                        texte('Speed x0.5',font2,window,(width/2.15),(height/2.34))
                        texte('Score *0.33',font2,window,(width/2.15),(height/2.22))
                        pygame.display.update()
                    if b==15:
                        texte('Speed x0.75',font2,window,(width/2.15),(height/2.34))
                        texte('Score x0.5',font2,window,(width/2.15),(height/2.22))
                        pygame.display.update()
                c=a+b
                if buttonsurface.collidepoint(mouse):
                    return c
 
def menu():
    pygame.mouse.set_visible(True)
    pygame.mixer.music.play(-1,0.0)
    texte('Bullet hell dodge game',pygame.font.Font('freesansbold.ttf',48),window,(width/4),(height/3))
    texte('Start Game',font,window,(width/2.6),(height/1.43))
    texte('Quit Game',font,window,(width/2.6),(height/1.25))
    texte('Déplacement: HAUT, BAS, GAUCHE, DROITE', font2, window,(width-400),10)
    texte('Ralentir le vaisseau: SHIFT', font2, window,(width-400),35)
    pygame.display.update()
    bouton()
 
pygame.init()
mainClock=pygame.time.Clock()
window=pygame.display.set_mode((width,height))
pygame.display.set_caption('Bullet hell dodge')
font=pygame.font.Font('freesansbold.ttf',24)
font2=pygame.font.Font('freesansbold.ttf',16)
playerimg=pygame.image.load('ship.png')
playerRect=playerimg.get_rect()
bulletimg=pygame.image.load('bullet.png')
bg=pygame.image.load('bg.jpg').convert()
pygame.mixer.music.load('menu.mp3')
window.blit(bg,(0,0))
button=pygame.image.load('button.png')
buttonsurface=window.blit(button,((width/2.7),(height/(3/2))))
buttonsurface2=window.blit(button,((width/2.7),(height/1.3)))
menu()
c=setting()
 
if c==1 or c==6 or c==11 or c==16:
    addbullet=2
    if c==6:
        bulletminspd=15
        bulletmaxspd=18
        scoretick=30
        movement=12
    if c==11:
        bulletminspd=5
        bulletmaxspd=6
        scoretick=180
        movement=4
    if c==16:
        bulletminspd=8
        bulletmaxspd=9
        scoretick=120
        movement=6
elif c==2 or c==7 or c==12 or c==17:
    addbullet=1
    if c==7:
        bulletminspd=15
        bulletmaxspd=18
        scoretick=30
        movement=12
    if c==12:
        bulletminspd=5
        bulletmaxspd=6
        scoretick=180
        movement=4
    if c==17:
        bulletminspd=8
        bulletmaxspd=9
        scoretick=120
        movement=6
elif c==3 or c==8 or c==13 or c==18:
    addbullet=5
    if c==8:
        bulletminspd=15
        bulletmaxspd=18
        scoretick=30
        movement=12
    if c==13:
        bulletminspd=5
        bulletmaxspd=6
        scoretick=180
        movement=4
    if c==18:
        bulletminspd=8
        bulletmaxspd=9
        scoretick=120
        movement=6
elif c==4 or c==9 or c==14 or c==19:
    addbullet=4
    if c==9:
        bulletminspd=15
        bulletmaxspd=18
        scoretick=30
        movement=12
    if c==14:
        bulletminspd=5
        bulletmaxspd=6
        scoretick=180
        movement=4
    if c==19:
        bulletminspd=8
        bulletmaxspd=9
        scoretick=120
        movement=6
elif c==0 or c==5 or c==10 or c==15:
    if c==5:
        bulletminspd=15
        bulletmaxspd=18
        scoretick=30
        movement=12
    if c==10:
        bulletminspd=5
        bulletmaxspd=6
        scoretick=180
        movement=4
    if c==15:
        bulletminspd=8
        bulletmaxspd=9
        scoretick=120
        movement=6
 
pygame.mouse.set_visible(False)
pygame.mixer.music.stop()
 
top=0
while True:
    bullet=[]
    score=0
    playerRect.topleft=(width/2,height-32)
    moveLeft=moveRight=moveUp=moveDown=False
    baddcounter=0
    scorecounter=0
    songselect=[1,2,3,4]
    if random.choice(songselect)==1:
        pygame.mixer.music.load('music.mp3')
    elif random.choice(songselect)==2:
        pygame.mixer.music.load('music2.mp3')
    elif random.choice(songselect)==3:
        pygame.mixer.music.load('music3.mp3')
    elif random.choice(songselect)==4:
        pygame.mixer.music.load('music4.mp3')
    pygame.mixer.music.play(-1,0.0)
 
    while True:
 
        for event in pygame.event.get():
            if event.type==QUIT:
                end()
            if event.type==KEYDOWN:
                if event.key==K_LSHIFT:
                    movement=5
                if event.key==K_LEFT:
                    moveRight=False
                    moveLeft=True
                if event.key==K_RIGHT:
                    moveLeft=False
                    moveRight=True
                if event.key==K_UP:
                    moveDown=False
                    moveUp=True
                if event.key==K_DOWN:
                    moveUp=False
                    moveDown=True
 
            if event.type==KEYUP:
                if event.key==K_LSHIFT:
                    movement=7
                if event.key==K_ESCAPE:
                    end()
                if event.key==K_LEFT:
                    moveLeft=False
                if event.key==K_RIGHT:
                    moveRight=False
                if event.key==K_UP:
                    moveUp=False
                if event.key==K_DOWN:
                    moveDown=False
 
        scorecounter += 1
        if scorecounter==scoretick:
            scorecounter=0
            score=score+1
 
        baddcounter += 1
        if baddcounter==addbullet:
            baddcounter=0
            newbullet={'rect':pygame.Rect(random.randint(0,width-16),-16,16,16),
                       'speed':random.randint(bulletminspd,bulletmaxspd),
                       }
            bullet.append(newbullet)
 
        if moveLeft and playerRect.left>0:
            playerRect.move_ip(-1*movement,0)
        if moveRight and playerRect.right<width:
            playerRect.move_ip(movement,0)
        if moveUp and playerRect.top>0:
            playerRect.move_ip(0,-1*movement)
        if moveDown and playerRect.bottom<height:
            playerRect.move_ip(0,movement)
 
        for b in bullet:
            b['rect'].move_ip(0,b['speed'])
 
        for b in bullet[:]:
            if b['rect'].top>height:
                bullet.remove(b)
 
        window.blit(bg,(0,0))
        texte('Score: %s' % (score),font,window,10,0)
        texte('Top Score: %s' % (top),font,window,10,40)
 
        window.blit(playerimg,playerRect)
        for b in bullet:
            window.blit(bulletimg,b['rect'])
        pygame.display.update()
 
        if hit(playerRect,bullet):
            if score>top:
                top=score
            break
 
        mainClock.tick(fps)
 
    texte('GAME OVER',pygame.font.Font("freesansbold.ttf",48),window,(width/3),(height/3))
    texte('Appuyez sur espace pour rejouer.',font,window,(width/3),(height/(3/2)))
    pygame.display.update()
    presskey()
    pygame.mixer.music.stop()
Si jamais cela peut aider quelqu'un un jour. Je ne sais pas si il y avait moyen de faire plus simple, je ne connais pas encore toutes les fonctionnalités de Python et j'ai essayé de m'en sortir avec ce qui était à ma disposition et ça a donné ceci.