forked from YesIndeed/SnakesForPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.py
More file actions
106 lines (80 loc) · 3.48 KB
/
Helpers.py
File metadata and controls
106 lines (80 loc) · 3.48 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import pygame, sys
from random import randint
from pygame.locals import *
from Constants import *
# Python Lesssson #1: Introduction to Python
# Snake is one of the earliest video games in video game history.
# You control a snake with the directional arrows (left, right, up, down) and grow as you eat.
# If you bump into a wall or part of yourself, you lose! (Or at least, you should!
# You can learn more about this game here: https://en.wikipedia.org/wiki/Snake_(video_game)
Blocksize= .5
# Comments are preceded by a pound sign (#).
# There will be commented blocks with questions throughout the code.
# These questions come in two flavors:
# Questions starting with Q X.Y: expect answers. Answer them in comments below them.
# Questions unmarked may be covered in discussion. Discuss with your partner and be prepared to share
# your answers with the class.
# You are not expected to read this code from the top-down. Jump around the code. Try to make sense of it.
# Try to change code. Be prepared to share your findings.
# Code preceded by "def" are called functions. They are very similar to methods in Java.
# Q 1.1: How is Java different than Python (try to get at least 5 differences)?
# Q 1.2: In Java, what is a method?
# Q 1.3: How does a Python function look different than a Java method?
walls = []
def initWalls(screen):
for i in range(0, xBound):
for j in range(0, yBound):
walls.append( Rect(i * blockSize, 0, blockSize, blockSize) )
walls.append( Rect(0, j * blockSize, blockSize, blockSize) )
walls.append( Rect(i * blockSize, (yBound - 1) * blockSize, blockSize, blockSize) )
walls.append( Rect((xBound - 1) * blockSize, j * blockSize, blockSize, blockSize) )
# Draw the walls
for wall in walls:
pygame.draw.rect(screen, (255, 255, 255), wall)
# Useful debug method. Text is a string, rect is a Rect.
def printRect(text, rect):
print text + " Top-left: (" + str(rect.left) + ", " + str(oldPiece.top) + ")"
# Code for if snake runs into itself or game exits.
def quitGame():
print "Game over!"
# How can we measure the player's score?
print "Your score is: " + Score
sys.exit(0)
# Code to move the head.
def moveHead(headRect, dir):
if dir == 1:
deltaX = blockSize
elif dir == 3:
deltaX = -blockSize
else:
deltaX = 0
if dir == 0:
deltaY = blockSize
elif dir == 2:
deltaY = -blockSize
else:
deltaY = 0
newRect= headRect.move(deltaX, deltaY)
return newRect
# Q 1.4 What do you think return means?
# Accepts two Rects as input.
# Move from to to's location.
def moveBody(toRect, fromRect):
deltaX = toRect.left - fromRect.left
deltaY = toRect.top - fromRect.top
return fromRect.move(deltaX, deltaY)
# Creates a random rectangle.
def randomRect():
return Rect(randint(1, xBound - 2) * blockSize, randint(1, yBound - 2) * blockSize, blockSize, blockSize)
# Draw the screen depending on what happens.
def draw(oldPiece, head, body, appleRect, hasEaten, screen):
# Draw the head.
pygame.draw.rect(screen, (255, 255, 255), head)
# Draw the body.
for block in body:
pygame.draw.rect(screen, (255, 255, 255), block) or pygame.draw.rect(screen, (0, 0, 255), block)
# If we have not eaten, we need to clear the old rectangle.
if (not hasEaten):
pygame.draw.rect(screen, (0, 0, 0), oldPiece)
# Draw the apple.
pygame.draw.rect(screen, (255, 255, 255), appleRect)