Built simple rendition of Snake and Apples in python using pygame game development module for purposes of coursework in AP Computer Science Principles class (April 2017):

Source code and explanations:

This program was developed for the purposes of the AP CSP 2016-2017 course. It's intended function is that of a game in python (employing pygame module) involving a snake collecting apples.

from pygame import * #Importing the modules required to develop the game; pygame (game development platform for python) and random (for the intended purposes of generating a random integer value corresponding to the location of elements of the program). from random import randint

init() #Initializing the pygame and random modules for use in the program

N,M=20,20 #This block of code is responsible for determining the dimensions of the grid (i.e.- width and height), as well as the display size on the computer screen. Scale=35 w,h=Scale*N,Scale*M screen=display.set_mode((w,h))

Snake=[(5,15),(5,16)]#List determining initial coordinate position of snake on the grid in the program. Apple=[(8,17),(2,7),(5,11),(19,3),(15,4),(1,20),(12,6),(9,14),(18,10),(11,13)] #List determining iniital coordinate locations of apples on the grid in the program.

FIELD=Surface((w,h)) FIELD.fill((0,255,150))#Colour of grid in the program for i in range(0,w,Scale): #Conditional loop (procedural abstraction) entailing drawling of lines (for the snake's movement) in the grid within the program. draw.line(FIELD,(0,0,0),(i,0),(i,h)) draw.line(FIELD,(0,0,0),(0,i),(w,i))

def NewApple(): #This chunk of code is involved in the randomized distribution/creation of apples at a variety of coordinates following the obtaining of a given apple by the snake in the program. if Snake[0] in Apple: i=Apple.index(Snake[0]) Apple[i]=(randint(0,N),randint(0,M)) Snake.append(Snake[-1])

def AppleDraw(): #Function determining the shape and colour of the apples in the program given size constraints and RGB value. for i in Apple: rect=(i[0]*Scale,i[1]*Scale,Scale-1,Scale-1) draw.rect(screen,(255,50,0),rect)

(R,L,U,D)=range(4) #This block of the code body indicates the number of possible directions (up,down,left,right; 4), initial starting direction (d=R), the number of units moved (per turn), and the rate at which the snake moves across the grid. d=R def tick(): if d==R: x=1;y=0; if d==L: x=-1;y=0; if d==U: x=0;y=-1; if d==D: x=0;y=1; t=Snake[0] t=(t[0]+x,t[1]+y) Snake.insert(0,t) time.wait(50) del Snake[-1] NewApple() if Snake[0] in Snake[1:]: del Snake[2:]

def btn_press(btn): #This block within the code body is responsible for associating the defined variables for directions in the above function with specific keys on the keyboard of the user. global d if btn==K_UP: d=U if btn==K_DOWN: d=D if btn==K_RIGHT: d=R if btn==K_LEFT: d=L

def SnakeDraw(): #This block of code in the body is a function which involves the drawing of the snake; specifically its shape, length, and colour. for i in Snake: rect=(i[0]*Scale,i[1]*Scale,Scale-1,Scale-1) draw.rect(screen,(0,0,255),rect)

k=2 while k: #This code block is from an external source, and thus is accompanied by a citation. It calls the previously define functions given the circumstances that the value of "k" (which represents the length of the snake at a given time). screen.blit(FIELD,(0,0)) k+=1 if k%5==0: tick() SnakeDraw() AppleDraw() display.update()

for e in event.get(): #This final chunk of code in the body text enables the user to exit the game be pressing the escape button on their keyboard. if e.type == KEYDOWN: btn_press(e.key) if e.key == K_ESCAPE: k=0

Built With

Share this project:

Updates