-
Notifications
You must be signed in to change notification settings - Fork 0
/
version1.py
219 lines (150 loc) · 6.81 KB
/
version1.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
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
from graphics import Canvas
import time
import random
import threading
import simpleaudio as sa
CANVAS_WIDTH = 500
CANVAS_HEIGHT = 500
SIZE = 50
eat_sound = sa.WaveObject.from_wave_file('sounds/gulp.wav')
game_over_sound = sa.WaveObject.from_wave_file('sounds/game-over.wav')
youWin = sa.WaveObject.from_wave_file('sounds/you-win.wav')
Backgroundsound = sa.WaveObject.from_wave_file('sounds/nature.wav')
class Game1:
def __init__(self, master=None):
self.master = master
self.won = False
self.high_score = 0
def main(self):
def check_collisions(canvas, player):
x1, y1 = canvas.coords(player)
x2 = x1 + SIZE
y2 = y1 + SIZE
if x1 < 0 or x2 > CANVAS_WIDTH or y1 < 0 or y2 > CANVAS_HEIGHT:
return True
return False
def generate_random_goal_position():
x = random.randint(0, CANVAS_WIDTH - SIZE)
y = random.randint(0, CANVAS_HEIGHT - SIZE)
x -= x % SIZE
y -= y % SIZE
return x, y
canvas = Canvas(CANVAS_WIDTH, CANVAS_HEIGHT,"MY BABY SNAKE GAME")
canvas.master.iconbitmap('Game Logo.ico')
canvas.update()
sd = eat_sound.play()
sd.stop()
canvas.create_image_with_size(0,0,CANVAS_WIDTH,CANVAS_HEIGHT,"Game Logo.png",0)
canvas.update()
background_sound_play_obj = Backgroundsound.play()
time.sleep(3)
image = canvas.create_image_with_size(0,0,CANVAS_WIDTH,CANVAS_HEIGHT,"bg.png",0)
time.sleep(1)
canvas.create_text(30,100,text = 'Welcome to Baby Snake Game ',font = 'Arial Rounded MT Bold',font_size = 23,color ='red')
canvas.create_text(20,160,text = 'To win this game you need to ',font = 'Arial Rounded MT Bold',font_size = 23,color ='blue')
canvas.create_text(20,195,text = 'score 50 points and if you touch ',font = 'Arial Rounded MT Bold',font_size = 23,color ='blue')
canvas.create_text(20,230,text = 'the walls you will lose the game ',font = 'Arial Rounded MT Bold',font_size = 23,color ='blue')
canvas.create_text(25,265,text = 'and use Arrow key to move.',font = 'Arial Rounded MT Bold',font_size = 23,color ='blue')
canvas.update()
time.sleep(5)
canvas.clear()
canvas.update()
for i in range(30):
canvas.create_line(50 *i, 0,50 *i, CANVAS_HEIGHT,"green")
canvas.create_line(0, 50*i,CANVAS_WIDTH,50*i,"green")
score = 0
HS = 0
sc = canvas.create_text(20,10,text = 'Score:- ' + str(score),font = 'Arial Rounded MT Bold',font_size = 25,color ='black')
player = canvas.create_image_with_size(0,0,50,50,"snake.png",0)
goal = canvas.create_image_with_size(350,350,50,50,"apple.jpg",0)
DELAY = 0.1
direction = 'right'
direction_lock = threading.Lock()
canvas.update()
while True:
canvas.update()
keys = canvas.get_new_key_presses()
canvas.update()
for key in keys:
if key.keysym in ['Left', 'Right', 'Up', 'Down']:
if (key.keysym == 'Left' and direction!= 'right') or \
(key.keysym == 'Right' and direction!= 'left') or \
(key.keysym == 'Up' and direction!= 'down') or \
(key.keysym == 'Down' and direction!= 'up'):
if key.keysym == 'Left':
direction = 'left'
elif key.keysym == 'Right':
direction = 'right'
elif key.keysym == 'Up':
direction = 'up'
elif key.keysym == 'Down':
direction = 'down'
canvas.update()
time.sleep(DELAY)
#...
if direction == 'left':
canvas.move(player, -SIZE, 0)
elif direction == 'right':
canvas.move(player, SIZE, 0)
elif direction == 'up':
canvas.move(player, 0, -SIZE)
elif direction == 'down':
canvas.move(player, 0, SIZE)
canvas.update()
time.sleep(DELAY)
#time.sleep(DELAY)
player_x1, player_y1 = canvas.coords(player)
player_x2 = player_x1 + SIZE
player_y2 = player_y1 + SIZE
goal_x1, goal_y1 = canvas.coords(goal)
goal_x2 = goal_x1 + SIZE
goal_y2 = goal_y1 + SIZE
canvas.update()
if player_x1 == goal_x1 and player_y1 == goal_y1:
canvas.delete(goal)
canvas.delete(sc)
new_x, new_y = generate_random_goal_position()
goal = canvas.create_image_with_size(new_x, new_y, SIZE, SIZE, "apple.jpg",0)
DELAY -= 0.001
score += 1
sc = canvas.create_text(20,10,text = 'Score:- ' + str(score),font = 'Arial Rounded MT Bold',font_size = 25,color ='black')
canvas.update()
sd = eat_sound.play()
# Sleep
if score == 50:
print("You win")
canvas.clear()
background_sound_play_obj.stop()
sd.stop()
canvas.update()
HS += score
canvas.create_text(25,(CANVAS_WIDTH/2)-70,text = 'High Score:- ' + str(HS),font = 'Arial Rounded MT Bold',font_size = 45,color ='brown')
canvas.update()
time.sleep(1)
image = canvas.create_image_with_size(0,0,CANVAS_HEIGHT,CANVAS_WIDTH,"win.png",0)
canvas.update()
youWin.play()
time.sleep(1)
self.won = True
canvas.destroy()
break
if check_collisions(canvas, player):
print("game over")
canvas.clear()
background_sound_play_obj.stop()
sd.stop()
canvas.update()
HS += score
canvas.create_text(25,(CANVAS_WIDTH/2)-70,text = 'High Score:- ' + str(HS),font = 'Arial Rounded MT Bold',font_size = 45,color ='brown')
canvas.update()
time.sleep(1)
image = canvas.create_image_with_size(0,0,CANVAS_HEIGHT,CANVAS_WIDTH,"game over.png",0)
canvas.update()
game_over_sound.play()
time.sleep(1.5)
canvas.destroy()
break
canvas.update()
time.sleep(DELAY)
if __name__ == '__main__':
main(Canvas)