-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygame_5.py
82 lines (73 loc) · 2.56 KB
/
pygame_5.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
import pygame
import random
#Initialize pygame
pygame.init()
# Set the screen size of our game
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
#Main Event loop
done = False
#Game loop
game_over = False
# Variable to store all the snake rectangles
green=(76, 235, 52)
red=(255, 0, 0)
#List of all snake rectangles
snake_segments = [pygame.Rect(40, 40, 20, 20)]
#Generate the Apple rectangle random
apple = pygame.Rect(random.randint(0, screen_width/20) *20,
random.randint(0, screen_height/20) *20, 20, 20)
#generate start version of the snake
for x in range(9):
#Copy the last item and add the y-axis/top value +20
y = snake_segments[-1].y
y += 20
snake_segments.append(pygame.Rect(40, y, 20, 20))
#Start Speed
x_change = 20
y_change = 0
#pygame clock to control the refresh rate of our game
clock = pygame.time.Clock()
while not done:
#Set the backgroudn color to black
screen.fill((0, 0, 0))
#Close the game if someone clicks the X top right
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if not game_over:
#Keyboard input arrow keys
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]:
x_change = 0
y_change = -20
if pressed[pygame.K_DOWN]:
x_change = 0
y_change = 20
if pressed[pygame.K_LEFT]:
x_change = -20
y_change = 0
if pressed[pygame.K_RIGHT]:
x_change = 20
y_change = 0
x = snake_segments[0].x + x_change
y = snake_segments[0].y + y_change
#Check if the snake is not out of the screen if so Game Over.
if(x > screen_width or x < 0 or y > screen_height or y < 0):
game_over = True
#Check if the snake runs into istelf if so = Game Over
if snake_segments[0].collidelistall(snake_segments) != [0]:
game_over = True
#Remove the last element so the snake will move and remains the same lenght
snake_segments.pop()
#Append the moved part a the new position
snake_segments.insert(0, pygame.Rect(x, y, 20, 20))
#Draw all parts of the snake
for segment in snake_segments:
pygame.draw.rect(screen, green, segment)
# draw the apple
pygame.draw.rect(screen, red, apple)
pygame.display.flip()
#Define the frame rate = how many times per second the screen schould refresh and the snake move
clock.tick(5)