python snake code copy and paste

Solutions on MaxInterview for python snake code copy and paste by the best coders in the world

showing results for - "python snake code copy and paste"
Yoan
31 Jan 2019
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
pygame snake gamesnake pygame projectsnake in python copy and pastepygame code snakesnake game in python pygame modulesnake code in pygamehow to make a snake game using pygamehow code pygame snakepython snake game without pygamepygame how to make a simple snakesnake python copy and pastesnake pygame code pythonsnake game python code copy and pastehow to make a snakey in pythonpygame how to make simple snakehow to make snake with pygamesnake game in pygamepygame snake example codesnake in pygamesimple snake game with pygamepython snake game code copy and pastesnake game python copy pastesnake with pygamehow to def snake in pygamehow to create a snake game using pygamepygame snake multiplayerpygame snake game tutorialsnake game using pygame pygame snake game codesnake game python pygame codesnake game with pygamesnake game code pygamemake multiplayer snake game in pygamepygame simple snake gamesimple snake game in pygamepython snake game without pygame librarysnake code pygamesnake game simple pygamehow to make a snake game in pygamesnake python code copy and pastesnake game tutorial python pygamesnake pygame codesnake mechanism pygamesnake game pygame tutorialsnakegame in pythonhow to make snake game in pygamepython code for snake copy and pastesnake pygamehow to code snake using pygamesnake w pythonie pygamesnake game easy pygamesnake game pygamesnake using pygamesnake pygame assetspygame snake tutorialpygame snake codemultiplayer snake game in pygamesnake in python short scripthow to make snake in python pygamehow to play your python snake game in vscmaking simple snake game pygamesnake code python pygamepython snake game no pygamepython snake code copy and pastepygame example snakeimplement snake game in python without pygamesnake pygame pythonpygame snake examplespygame snakecreate a snake game in pygamepygame python snake game codepython pygame snakequick play snake python code not using pygamesnake python game pygamesnake game in python using pygamepygame snake game source codehow to make snake in pygamesnake game pythonsnake easy pygamesnake game python copy and pastebuild snake game in pythonpython snake code copy and paste