pygame snake

Solutions on MaxInterview for pygame snake by the best coders in the world

showing results for - "pygame snake"
Catalina
16 Oct 2016
1import pygame
2import time
3import random
4 
5pygame.init()
6 
7white = (255, 255, 255)
8yellow = (255, 255, 102)
9black = (0, 0, 0)
10red = (213, 50, 80)
11green = (0, 255, 0)
12blue = (50, 153, 213)
13 
14dis_width = 600
15dis_height = 400
16 
17dis = pygame.display.set_mode((dis_width, dis_height))
18pygame.display.set_caption('Snake Game by Edureka')
19 
20clock = pygame.time.Clock()
21 
22snake_block = 10
23snake_speed = 15
24 
25font_style = pygame.font.SysFont("bahnschrift", 25)
26score_font = pygame.font.SysFont("comicsansms", 35)
27 
28 
29def Your_score(score):
30    value = score_font.render("Your Score: " + str(score), True, yellow)
31    dis.blit(value, [0, 0])
32 
33 
34 
35def 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 
40def message(msg, color):
41    mesg = font_style.render(msg, True, color)
42    dis.blit(mesg, [dis_width / 6, dis_height / 3])
43 
44 
45def 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-Play Again or Q-Quit", 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 
127gameLoop()
128
queries leading to this page
make multiplayer snake game in pygamequick play snake python code not using pygamehow to make snake game in pygamesnake in pygamesnake python code copy and pastesnake game with pygamesnake pygame pythonsnake code in pygamepygame snake examplespython snake game no pygamesnake using pygamesnake mechanism pygamesnake pygame code pythonsnake game in python using pygamecreate a snake game in pygamepygame simple snake gamesimple snake game in pygamepygame snake game tutorialsnake game code pygamesnake game easy pygamesnake game pygamebuild snake game in pythonpython snake game without pygame librarypygame snake tutorialhow to make snake in python pygamesnake in python short scriptsnakegame in pythonsimple snake game with pygamesnake game python pygame codehow to create a snake game using pygamehow to code snake using pygamehow to make snake in pygamesnake game in python pygame modulesnake pygamehow code pygame snakepygame python snake game codehow to make a snakey in pythonsnake game pythonpython snake code copy and pastepygame example snakepython snake game without pygamepygame snake multiplayersnake pygame assetssnake game tutorial python pygamesnake code python pygamepygame snake game codepygame snake gamepygame snakehow to make a snake game using pygamesnake game in pygamesnake game pygame tutorialsnake with pygamesnake code pygamehow to play your python snake game in vscsnake game using pygame snake w pythonie pygamesnake game simple pygamepygame snake game source codepython pygame snakemaking simple snake game pygamesnake game python copy and pastehow to def snake in pygamesnake python copy and pastehow to make a snake game in pygamepygame snake example codepygame snake codesnake pygame codehow to make snake with pygamemultiplayer snake game in pygamepygame code snakesnake easy pygamepygame snake