forked from charvimehradu/SnakeGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_edit.py
202 lines (156 loc) · 7.72 KB
/
new_edit.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
import pygame
import random
import math
import time
from pygame import mixer
# initialize oygame
pygame.init()
# creating a game window
# width, height
screen = pygame.display.set_mode((1000, 800))
background_image = pygame.image.load("home.jpg")
play_button_image = pygame.image.load("play2.png")
# Blit the images to the screen
screen.blit(background_image, (0, 0))
screen.blit(play_button_image, (1000 // 2 - play_button_image.get_width() // 2, 800 // 2 - play_button_image.get_height() // 2))
# Update the screen
pygame.display.update()
# Set up the game loop
game_running = True
while game_running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
# Check if the mouse was clicked on the play button
mouse_pos = pygame.mouse.get_pos()
play_button_rect = play_button_image.get_rect()
play_button_rect.center = (1000// 2, 800 // 2)
if play_button_rect.collidepoint(mouse_pos):
# title and icon
pygame.display.set_caption("Snake")
background=pygame.image.load("images (1).jpeg")
# mixer.music.load("bg_music.mp3")
# mixer.music.play(-1)
# importing snake head img
# snake_headimg = pygame.image.load("snake_head_img.png")
# snake_headX = random.randint(80, 920)
# snake_headY = random.randint(80, 720)
snake_head_state = "play"
snake_body_img=[]
snake_bodyX=[]
snake_bodyY=[]
snake_bodyX.append(80)
snake_bodyY.append(80)
snake_headX_change = 0
snake_headY_change = 0
if snake_head_state!='rest':
mixer.music.load("bg_music.mp3")
mixer.music.play(-1)
snake_body_img.append(pygame.image.load("snake_head.jpeg"))
snake_body_img.append(pygame.image.load("snake.jpg"))
def snake_head(x, y, state):
if state == "play":
screen.blit(snake_body_img[0], (x, y))
def snake_body(x, y, state):
if state == "play":
screen.blit(snake_body_img[1], (x, y))
# importing fruit
fruitimg = pygame.image.load("fruit(1).png")
fruitX = int(random.uniform(0,1)*((920/40)))*40 +40
fruitY = int(random.uniform(0,1)*((720/40)))*40 +40
def fruit(x, y):
screen.blit(fruitimg, (x, y))
def is_collision(x, y, z, w):
distance = math.sqrt((math.pow(x-z, 2))+(math.pow(y-w, 2)))
if distance < 30:
return True
else:
return False
# importing tail
# snake_body_img=[]
# snake_bodyX=[]
# snake_bodyY=[]
# snake_bodyX.append(snake_headX)
# snake_bodyY.append(snake_headY)
# for i in range(100):
# snake_body_img.append(pygame.image.load("snake copy.png"))
color=(0,0,0)
running = True
# game loop
# infinitely running
while running:
screen.fill((255, 255, 255))
screen.blit(background,(40,40))
pygame.draw.rect(screen, color, pygame.Rect(0, 0, 1000, 800), 40)
# checking for events in game windiw
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
snake_headY_change = 0
snake_headX_change = -20
if event.key == pygame.K_w or event.key == pygame.K_UP:
snake_headX_change = 0
snake_headY_change = -20
if event.key == pygame.K_s or event.key == pygame.K_DOWN:
snake_headX_change = 0
snake_headY_change = 20
if event.key == pygame.K_d or event.key == pygame.K_RIGHT:
snake_headY_change = 0
snake_headX_change = 20
# boundary detection
if snake_bodyX[0]<= 20:
snake_bodyX[0] = 20
snake_head_state = "rest"
# end_sound=mixer.Sound("collision.mp3")
# end_sound.play()
if snake_bodyX[0] >= 950:
snake_bodyX[0] = 950
snake_head_state = "rest"
# end_sound=mixer.Sound("collision.mp3")
# end_sound.play()
if snake_bodyY[0] <= 20:
snake_bodyY[0] = 20
snake_head_state = "rest"
# end_sound=mixer.Sound("collision.mp3")
# end_sound.play()
if snake_bodyY[0] >= 740:
snake_bodyY[0] = 740
snake_head_state = "rest"
# end_sound=mixer.Sound("collision.mp3")
# end_sound.play()
# snake_bodyX[0]=snake_headX
# snake_bodyY[0]=snake_headY
fruit(fruitX, fruitY)
# appearing of snake head on the screen
collision = is_collision( snake_bodyX[0], snake_bodyY[0], fruitX, fruitY)
if collision:
eat_sound=mixer.Sound('eat.mp3')
eat_sound.play()
snake_bodyX.append(-80)
snake_bodyY.append(-80)
fruit(fruitX,fruitY)
fruitX = int(random.uniform(0,1)*((920/40)))*40 +40
fruitY = int(random.uniform(0,1)*((720/40)))*40 +40
for i in range(len(snake_bodyX)-1,-1,-1):
if i==0:
snake_head( snake_bodyX[i], snake_bodyY[i], snake_head_state)
if i!=0:
snake_body( snake_bodyX[i], snake_bodyY[i], snake_head_state)
snake_bodyX[i]=snake_bodyX[i-1]
snake_bodyY[i]=snake_bodyY[i-1]
snake_bodyX[0] = snake_bodyX[0]+snake_headX_change
snake_bodyY[0] = snake_bodyY[0]+snake_headY_change
for i in range(len(snake_bodyX)-1,0,-1):
if snake_bodyX[0]==snake_bodyX[i] and snake_bodyY[0]==snake_bodyY[i]:
snake_head_state="rest"
# end_sound=mixer.Sound("collision.mp3")
# end_sound.play()
# print(snake_bodyX[0])
# print(snake_bodyY[0])
time.sleep(0.1)
# updating screen in continuous time
pygame.display.update()