Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dbfbaed

Browse files
Merge pull request avinashkranjan#306 from CharvyJain/snake_game
Snake game using Python
2 parents b43c38e + 3ecf55c commit dbfbaed

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

‎Snake Game (GUI)/README.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Snake Game (GUI)
2+
3+
Snake game is an Arcade Maze Game which has been developed by Gremlin Industries. The player’s objective in the game is to achieve maximum points as possible by collecting food or fruits. The player loses once the snake hits the wall or hits itself.
4+
5+
## Setup instructions
6+
7+
In order to run this script, You just need the following 3 modules -
8+
9+
- **Pygame:** It is a set of Python modules designed for writing video games.
10+
- **Time:** This function is used to count the number of seconds elapsed since the epoch.
11+
- **Random:** This function is used to generate random numbers in Python by using random module. **Pygame, Time and Random**
12+
13+
## Output
14+
15+
![Snake Game](game.png)
16+
17+
## Author
18+
19+
[Charvy Jain](https://github.com/CharvyJain)

‎Snake Game (GUI)/game.png‎

5.45 KB
Loading[フレーム]

‎Snake Game (GUI)/game.py‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import pygame
2+
import time
3+
import random
4+
5+
pygame.init()
6+
7+
white = (255, 255, 255)
8+
yellow = (255, 255, 102)
9+
black = (0, 0, 0)
10+
red = (213, 50, 80)
11+
green = (0, 255, 0)
12+
blue = (50, 153, 213)
13+
14+
dis_width = 600
15+
dis_height = 400
16+
17+
dis = pygame.display.set_mode((dis_width, dis_height))
18+
pygame.display.set_caption('Snake Game In Python')
19+
20+
clock = pygame.time.Clock()
21+
22+
snake_block = 10
23+
snake_speed = 15
24+
25+
font_style = pygame.font.SysFont("bahnschrift", 25)
26+
score_font = pygame.font.SysFont("comicsansms", 35)
27+
28+
29+
def Your_score(score):
30+
value = score_font.render("Your Score: " + str(score), True, yellow)
31+
dis.blit(value, [0, 0])
32+
33+
34+
35+
def our_snake(snake_block, snake_list):
36+
for x in snake_list:
37+
pygame.draw.rect(dis, black, [x[0], x[1], snake_block, snake_block])
38+
39+
40+
def message(msg, color):
41+
mesg = font_style.render(msg, True, color)
42+
dis.blit(mesg, [dis_width / 6, dis_height / 3])
43+
44+
45+
def gameLoop():
46+
game_over = False
47+
game_close = False
48+
49+
x1 = dis_width / 2
50+
y1 = dis_height / 2
51+
52+
x1_change = 0
53+
y1_change = 0
54+
55+
snake_List = []
56+
Length_of_snake = 1
57+
58+
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
59+
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
60+
61+
while not game_over:
62+
63+
while game_close == True:
64+
dis.fill(blue)
65+
message("You Lost! Press 'C' to Play Again or 'Q' To Quit The Game", red)
66+
Your_score(Length_of_snake - 1)
67+
pygame.display.update()
68+
69+
for event in pygame.event.get():
70+
if event.type == pygame.KEYDOWN:
71+
if event.key == pygame.K_q:
72+
game_over = True
73+
game_close = False
74+
if event.key == pygame.K_c:
75+
gameLoop()
76+
77+
for event in pygame.event.get():
78+
if event.type == pygame.QUIT:
79+
game_over = True
80+
if event.type == pygame.KEYDOWN:
81+
if event.key == pygame.K_LEFT:
82+
x1_change = -snake_block
83+
y1_change = 0
84+
elif event.key == pygame.K_RIGHT:
85+
x1_change = snake_block
86+
y1_change = 0
87+
elif event.key == pygame.K_UP:
88+
y1_change = -snake_block
89+
x1_change = 0
90+
elif event.key == pygame.K_DOWN:
91+
y1_change = snake_block
92+
x1_change = 0
93+
94+
if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0:
95+
game_close = True
96+
x1 += x1_change
97+
y1 += y1_change
98+
dis.fill(blue)
99+
pygame.draw.rect(dis, green, [foodx, foody, snake_block, snake_block])
100+
snake_Head = []
101+
snake_Head.append(x1)
102+
snake_Head.append(y1)
103+
snake_List.append(snake_Head)
104+
if len(snake_List) > Length_of_snake:
105+
del snake_List[0]
106+
107+
for x in snake_List[:-1]:
108+
if x == snake_Head:
109+
game_close = True
110+
111+
our_snake(snake_block, snake_List)
112+
Your_score(Length_of_snake - 1)
113+
114+
pygame.display.update()
115+
116+
if x1 == foodx and y1 == foody:
117+
foodx = round(random.randrange(0, dis_width - snake_block) / 10.0) * 10.0
118+
foody = round(random.randrange(0, dis_height - snake_block) / 10.0) * 10.0
119+
Length_of_snake += 1
120+
121+
clock.tick(snake_speed)
122+
123+
pygame.quit()
124+
quit()
125+
126+
gameLoop()
127+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /