-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.py
More file actions
79 lines (69 loc) · 2.21 KB
/
Game.py
File metadata and controls
79 lines (69 loc) · 2.21 KB
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
from random import randint
from sys import exit
import pygame
import time
import Snake
import Food
# Setup Game Parameters
winWidth = 800
winHeight = 800
boxWidth = winWidth / 30
running = True
player = Snake.Snake(4, boxWidth)
food = Food.Food(randint(1, round(boxWidth)) * boxWidth, randint(1, round(boxWidth)) * boxWidth)
# Initialize window
pygame.init()
screen = pygame.display.set_mode((winWidth, winHeight))
pygame.display.set_caption("Pygame Snake")
# Start Game Loop
while True:
# Event Loop
pygame.event.pump()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
pygame.quit()
exit()
if keys[pygame.K_a] or keys[pygame.K_LEFT]:
player.move('L')
if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
player.move('R')
if keys[pygame.K_w] or keys[pygame.K_UP]:
player.move('U')
if keys[pygame.K_s] or keys[pygame.K_DOWN]:
player.move('D')
if keys[pygame.K_p]: # DEBUG
food.x = randint(1, round(boxWidth)) * boxWidth
food.y = randint(1, round(boxWidth)) * boxWidth
player.feed()
# Update our snakes position
player.update()
# Update Display
screen.fill((80, 80, 80))
fruitRec = pygame.Rect(round(food.x), round(food.y), round(boxWidth), round(boxWidth))
pygame.draw.rect(screen, (255, 0, 0, 1), fruitRec)
for i in range(player.length-1, 0, -1):
rectangle = pygame.Rect(round(player.x[i]), round(player.y[i]), round(boxWidth), round(boxWidth))
pygame.draw.rect(screen, (255, 255, 255, 1), rectangle)
pygame.display.update()
# Check for Collision
# Snake to Snake
for i in range(1, player.length):
if round(player.x[0]) == round(player.x[i]) and round(player.y[0]) == round(player.y[i]):
pygame.quit()
exit()
# Snake to food
if round(player.x[0]) == round(food.x) and round(player.y[0]) == round(food.y):
food.x = randint(1, round(boxWidth)) * boxWidth
food.y = randint(1, round(boxWidth)) * boxWidth
player.feed()
# Snake to Wall
if round(player.x[0]) < 0 or round(player.x[0]) > winWidth or round(player.y[0]) < 0 or round(player.y[0]) > winHeight:
pygame.quit()
exit()
# Make the Game Wait
time.sleep (50.0 / 1000.0)