1
+ import pygame
2
+ import numpy as np
3
+ import time
4
+ import random
5
+
6
+ def collision_with_apple (apple_position , score ):
7
+ apple_position = [random .randrange (1 ,50 )* 10 ,random .randrange (1 ,50 )* 10 ]
8
+ score += 1
9
+ return apple_position , score
10
+
11
+ def collision_with_boundaries (snake_head ):
12
+ if snake_head [0 ]>= 500 or snake_head [0 ]< 0 or snake_head [1 ]>= 500 or snake_head [1 ]< 0 :
13
+ return 1
14
+ else :
15
+ return 0
16
+
17
+ def collision_with_self (snake_position ):
18
+ snake_head = snake_position [0 ]
19
+ if snake_head in snake_position [1 :]:
20
+ return 1
21
+ else :
22
+ return 0
23
+
24
+ def is_direction_blocked (snake_position , current_direction_vector ):
25
+ next_step = snake_position [0 ]+ current_direction_vector
26
+ snake_head = snake_position [0 ]
27
+ if collision_with_boundaries (snake_head ) == 1 or collision_with_self (snake_position ) == 1 :
28
+ return 1
29
+ else :
30
+ return 0
31
+
32
+
33
+ def generate_snake (snake_head , snake_position , apple_position , button_direction , score ):
34
+ if button_direction == 1 :
35
+ snake_head [0 ] += 10
36
+ elif button_direction == 0 :
37
+ snake_head [0 ] -= 10
38
+ elif button_direction == 2 :
39
+ snake_head [1 ] += 10
40
+ elif button_direction == 3 :
41
+ snake_head [1 ] -= 10
42
+ else :
43
+ pass
44
+
45
+ if snake_head == apple_position :
46
+ apple_position , score = collision_with_apple (apple_position , score )
47
+ snake_position .insert (0 , list (snake_head ))
48
+
49
+ else :
50
+ snake_position .insert (0 , list (snake_head ))
51
+ snake_position .pop ()
52
+
53
+ return snake_position , apple_position , score
54
+
55
+
56
+ def display_snake (snake_position ):
57
+ for position in snake_position :
58
+ pygame .draw .rect (display ,red ,pygame .Rect (position [0 ],position [1 ],10 ,10 ))
59
+
60
+ def display_apple (display ,apple_position , apple ):
61
+ display .blit (apple ,(apple_position [0 ], apple_position [1 ]))
62
+
63
+
64
+ def play_game (snake_head , snake_position , apple_position , button_direction , apple , score ):
65
+ crashed = False
66
+ prev_button_direction = 1
67
+ button_direction = 1
68
+ current_direction_vector = np .array (snake_position [0 ]) - np .array (snake_position [1 ])
69
+
70
+ while crashed is not True :
71
+ for event in pygame .event .get ():
72
+
73
+ if event .type == pygame .QUIT :
74
+ crashed = True
75
+ if event .type == pygame .KEYDOWN :
76
+ if event .key == pygame .K_LEFT and prev_button_direction != 1 :
77
+ button_direction = 0
78
+ elif event .key == pygame .K_RIGHT and prev_button_direction != 0 :
79
+ button_direction = 1
80
+ elif event .key == pygame .K_UP and prev_button_direction != 2 :
81
+ button_direction = 3
82
+ elif event .key == pygame .K_DOWN and prev_button_direction != 3 :
83
+ button_direction = 2
84
+ else :
85
+ button_direction = button_direction
86
+
87
+ display .fill (window_color )
88
+ display_apple (display , apple_position , apple )
89
+ display_snake (snake_position )
90
+
91
+ snake_position , apple_position , score = generate_snake (snake_head , snake_position , apple_position ,
92
+ button_direction , score )
93
+ pygame .display .set_caption ("Snake Game" + " " + "SCORE: " + str (score ))
94
+ pygame .display .update ()
95
+ prev_button_direction = button_direction
96
+ if is_direction_blocked (snake_position , current_direction_vector ) == 1 :
97
+ crashed = True
98
+
99
+ clock .tick (2 )
100
+ return score
101
+
102
+
103
+ def display_final_score (display_text , final_score ):
104
+ largeText = pygame .font .Font ('freesansbold.ttf' ,35 )
105
+ TextSurf = largeText .render (display_text , True , black )
106
+ TextRect = TextSurf .get_rect ()
107
+ TextRect .center = ((display_width / 2 ),(display_height / 2 ))
108
+ display .blit (TextSurf , TextRect )
109
+ pygame .display .update ()
110
+ time .sleep (2 )
111
+
112
+
113
+ if __name__ == "__main__" :
114
+ ###### initialize required parameters ########
115
+ display_width = 500
116
+ display_height = 500
117
+ green = (0 , 255 , 0 )
118
+ red = (255 , 0 , 0 )
119
+ black = (0 , 0 , 0 )
120
+ window_color = (200 , 200 , 200 )
121
+ apple_image = pygame .image .load ('apple.jpg' )
122
+ clock = pygame .time .Clock ()
123
+
124
+ snake_head = [250 , 250 ]
125
+ snake_position = [[250 , 250 ], [240 , 250 ], [230 , 250 ]]
126
+ apple_position = [random .randrange (1 , 50 ) * 10 , random .randrange (1 , 50 ) * 10 ]
127
+ score = 0
128
+
129
+ pygame .init () # initialize pygame modules
130
+
131
+ #### display game window #####
132
+
133
+ display = pygame .display .set_mode ((display_width , display_height ))
134
+ display .fill (window_color )
135
+ pygame .display .update ()
136
+
137
+ final_score = play_game (snake_head , snake_position , apple_position , 1 , apple_image , score )
138
+ display = pygame .display .set_mode ((display_width , display_height ))
139
+ display .fill (window_color )
140
+ pygame .display .update ()
141
+
142
+ display_text = 'Your Score is: ' + str (final_score )
143
+ display_final_score (display_text , final_score )
144
+
145
+ pygame .quit ()
0 commit comments