Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • namy2/se3xa3
  • bokhari/se3xa3
  • kanwalg/se3xa3
  • sunx20/se3xa3
  • hameea1/se3xa3
  • aij/se3xa3
  • hanzy3/se3xa3
  • linl20/se3xa3
  • zhous20/se3xa3
9 results
Show changes
Showing
with 1324 additions and 0 deletions
File added
This diff is collapsed.
#standard set up
import pygame
from random import randint
pygame.init()
#setting width and height of window
screen_x = 500
screen_y = 500
win = pygame.display.set_mode((screen_x,screen_y))
pygame.display.set_caption("My Game")
white = (255,255,255)
black = (0,0,0)
x = randint(40,400)
y = randint(40,400)
width = 20
height = 20
vel = 10
speed = 40
# 0 - (- direction) , 1 - (+ direction)
direction = 0
# 0 - x-axis , 1 - y-axis
axis = 0
run = True
while run:
pygame.time.delay(speed) #create a delay to prevent any unwanted behaviour
#events are anything that happens from the user
#we loop through the list of events that happen by the user's actions
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#Each event type has an integer assigned to it. KEYDOWN has the code 2
if event.type == pygame.KEYDOWN:
if (event.key == pygame.K_LEFT):
axis = 0;direction=-1
if (event.key == pygame.K_RIGHT):
axis = 0;direction=1
if (event.key == pygame.K_UP):
axis = 1;direction=-1
if (event.key == pygame.K_DOWN):
axis = 1;direction=1
#Snake moving depending on axis and direction
if (axis):
y += vel*direction
else:
x += vel*direction
if x < 0:
#x = 0
x = screen_x - height
if y < 0:
#y = 0
y = screen_y - width
if y > screen_y - width:
#y = 500 - width
y = 0
if x > screen_x - height:
#x = 500 - height
x = 0
win.fill(white)
#all colors are defined in RGB with Pygame
pygame.draw.rect(win,(255,0,0), (x,y,width, height))
#we have to update the display to see the drawing of our object. Since it does
#not automatically update
pygame.display.update()
#Quit the game
pygame.quit()
#importing necessary libraries
import pygame, sys
#a function made to execute other files from the system
def runfile(runfilename):
with open(runfilename,"r") as rnf:
exec(rnf.read())
pygame.init()
#while loop required to always refresh the page
while True:
game = pygame.display.set_mode((800, 610))
game.fill([213, 219, 219])
mousepos = pygame.mouse.get_pos() #checking mouse position
mouseclick = pygame.mouse.get_pressed()#checking mouse pressed
pygame.display.set_caption("Lets Play")
title_font = pygame.font.SysFont("monospace", 80)
#Adding the title
gamename = title_font.render('SNAKE GAME', True, (255, 0, 0))
game.blit(gamename,(150,80))
#Adding the play game button
if (270 <= mousepos[0] <= 270+250 and 505 <= mousepos[1] <= 555 ):
#checks if the mouse is hovering over the button
pygame.draw.rect(game,[100,100,100], [270,505,250,50], 0)
#checking if the button is clicked
if mouseclick[0] == 1:
runfile('Snake_2.o_Demo.py')
else:
pygame.draw.rect(game,[180,180,180], [270,505,250,50], 0)
playgame_font = pygame.font.SysFont("comicsansms", 40)
gamebutton = playgame_font.render('GAME TIME', True, (0, 0, 200))
game.blit(gamebutton,(275,500))
#Taking user name
UserName = playgame_font.render('NAME:', True, (0, 250, 0))
game.blit(UserName,(50,200))
#Asking user for theme
ThemeOption = playgame_font.render('THEMES:', True, (0, 200, 0))
game.blit(ThemeOption,(50,300))
#Asking user for speed
SpeedOption = playgame_font.render('SPEED:', True, (0, 150, 0))
game.blit(SpeedOption,(50,400))
#If user wants to quit
pygame.draw.rect(game,[180,180,180], [650,550,130,55], 0)
Quit = playgame_font.render('QUIT', True, (0, 0, 0))
game.blit(Quit,(650,550))
if ( 650 <= mousepos[0] <= 650+130 and 550 <= mousepos[1] <= 550 +55):
#checks if the mouse is hovering over the button
#checking if the button is clicked
if mouseclick[0] == 1:
#print ("quiting")
#pygame.display.quit()
pygame.quit()
sys.exit()
pygame.display.update()
<?xml version="1.0" encoding="UTF-8"?><project name="3XA3: Team Project" company="" webLink="http://" view-date="2018-10-25" view-index="0" gantt-divider-location="300" resource-divider-location="300" version="2.8.9" locale="en_CA">
<description><![CDATA[Developing the clasical Snake game using python and front-end development languages.]]></description>
<view zooming-state="default:2" id="gantt-chart">
<field id="tpd3" name="Name" width="167" order="0"/>
<field id="tpd4" name="Begin date" width="65" order="1"/>
<field id="tpd5" name="End date" width="64" order="2"/>
</view>
<view id="resource-table">
<field id="0" name="Name" width="210" order="0"/>
<field id="1" name="Default role" width="86" order="1"/>
</view>
<!-- -->
<calendars>
<day-types>
<day-type id="0"/>
<day-type id="1"/>
<default-week id="1" name="default" sun="1" mon="0" tue="0" wed="0" thu="0" fri="0" sat="1"/>
<only-show-weekends value="false"/>
<overriden-day-types/>
<days/>
</day-types>
</calendars>
<tasks empty-milestones="true">
<taskproperties>
<taskproperty id="tpd0" name="type" type="default" valuetype="icon"/>
<taskproperty id="tpd1" name="priority" type="default" valuetype="icon"/>
<taskproperty id="tpd2" name="info" type="default" valuetype="icon"/>
<taskproperty id="tpd3" name="name" type="default" valuetype="text"/>
<taskproperty id="tpd4" name="begindate" type="default" valuetype="date"/>
<taskproperty id="tpd5" name="enddate" type="default" valuetype="date"/>
<taskproperty id="tpd6" name="duration" type="default" valuetype="int"/>
<taskproperty id="tpd7" name="completion" type="default" valuetype="int"/>
<taskproperty id="tpd8" name="coordinator" type="default" valuetype="text"/>
<taskproperty id="tpd9" name="predecessorsr" type="default" valuetype="text"/>
</taskproperties>
<task id="0" name="Development Plan" color="#8cb6ce" meeting="false" start="2018-09-25" duration="4" complete="0" thirdDate="2018-09-27" thirdDate-constraint="0" expand="false">
<task id="19" name="Team Meeting Plan" color="#8cb6ce" meeting="false" start="2018-09-25" duration="1" complete="0" thirdDate="2018-09-19" thirdDate-constraint="0" expand="true"/>
<task id="20" name="Team Communication Plan" color="#8cb6ce" meeting="false" start="2018-09-25" duration="1" complete="0" thirdDate="2018-09-13" thirdDate-constraint="0" expand="true"/>
<task id="29" name="Copy_Team Communication Plan" color="#8cb6ce" meeting="false" start="2018-09-25" duration="1" complete="0" thirdDate="2018-09-13" thirdDate-constraint="0" expand="true"/>
<task id="21" name="Team Member Roles" color="#8cb6ce" meeting="false" start="2018-09-25" duration="1" complete="0" thirdDate="2018-09-13" thirdDate-constraint="0" expand="true"/>
<task id="22" name="Git workflow plan" color="#8cb6ce" meeting="false" start="2018-09-25" duration="1" complete="0" thirdDate="2018-09-13" thirdDate-constraint="0" expand="true"/>
<task id="23" name="Proof of Concept" color="#8cb6ce" meeting="false" start="2018-09-26" duration="1" complete="0" thirdDate="2018-09-13" thirdDate-constraint="0" expand="true"/>
<task id="24" name="Technology" color="#8cb6ce" meeting="false" start="2018-09-26" duration="1" complete="0" thirdDate="2018-09-13" thirdDate-constraint="0" expand="true"/>
<task id="25" name="Coding Style" color="#8cb6ce" meeting="false" start="2018-09-27" duration="1" complete="0" thirdDate="2018-09-13" thirdDate-constraint="0" expand="true"/>
<task id="26" name="Project Schedule" color="#8cb6ce" meeting="false" start="2018-09-27" duration="1" complete="0" thirdDate="2018-09-13" thirdDate-constraint="0" expand="true"/>
<task id="27" name="Project review" color="#8cb6ce" meeting="false" start="2018-09-28" duration="1" complete="0" thirdDate="2018-09-13" thirdDate-constraint="0" expand="true"/>
</task>
<task id="8" name="Requirements Document Revision" color="#8cb6ce" meeting="false" start="2018-10-01" duration="5" complete="0" thirdDate="2018-09-27" thirdDate-constraint="0" expand="false">
<task id="31" name="Project Drivers" color="#8cb6ce" meeting="false" start="2018-10-01" duration="2" complete="0" thirdDate="2018-09-17" thirdDate-constraint="0" expand="true"/>
<task id="49" name="Functional Requirements" color="#8cb6ce" meeting="false" start="2018-10-02" duration="2" complete="0" thirdDate="2018-09-12" thirdDate-constraint="0" expand="true"/>
<task id="42" name="Non-Functional Requirements" color="#8cb6ce" meeting="false" start="2018-10-03" duration="2" complete="0" thirdDate="2018-09-12" thirdDate-constraint="0" expand="true"/>
<task id="44" name="Project Issues" color="#8cb6ce" meeting="false" start="2018-10-03" duration="2" complete="0" thirdDate="2018-09-12" thirdDate-constraint="0" expand="true">
<depend id="52" type="2" difference="0" hardness="Strong"/>
</task>
<task id="52" name="Push &amp; Tag Document" color="#8cb6ce" meeting="false" start="2018-10-05" duration="1" complete="0" thirdDate="2018-09-17" thirdDate-constraint="0" expand="true"/>
<task id="55" name="SpellCheck" color="#8cb6ce" meeting="false" start="2018-10-04" duration="1" complete="0" thirdDate="2018-09-18" thirdDate-constraint="0" expand="true">
<depend id="52" type="2" difference="0" hardness="Strong"/>
</task>
</task>
<task id="9" name="Proof of Concept Demonstration" color="#8cb6ce" meeting="false" start="2018-10-10" duration="4" complete="0" thirdDate="2018-09-27" thirdDate-constraint="0" expand="false">
<task id="59" name="Snake Body &amp; Movement" color="#8cb6ce" meeting="false" start="2018-10-10" duration="2" complete="0" thirdDate="2018-09-17" thirdDate-constraint="0" expand="true"/>
<task id="61" name="Home Page GUI" color="#8cb6ce" meeting="false" start="2018-10-11" duration="2" complete="0" thirdDate="2018-09-17" thirdDate-constraint="0" expand="true"/>
<task id="63" name="Border Boundaries" color="#8cb6ce" meeting="false" start="2018-10-15" duration="1" complete="0" thirdDate="2018-09-17" thirdDate-constraint="0" expand="true"/>
</task>
<task id="12" name="Test Plan Revision" color="#8cb6ce" meeting="false" start="2018-10-19" duration="5" complete="0" thirdDate="2018-09-27" thirdDate-constraint="0" expand="false">
<task id="64" name="General Information" color="#8cb6ce" meeting="false" start="2018-10-19" duration="2" complete="0" thirdDate="2018-10-08" thirdDate-constraint="0" expand="true"/>
<task id="65" name="Plan" color="#8cb6ce" meeting="false" start="2018-10-19" duration="2" complete="0" thirdDate="2018-10-08" thirdDate-constraint="0" expand="true"/>
<task id="66" name="System Test Description" color="#8cb6ce" meeting="false" start="2018-10-22" duration="2" complete="0" thirdDate="2018-10-08" thirdDate-constraint="0" expand="true"/>
<task id="67" name="Tests for Proof of Concept" color="#8cb6ce" meeting="false" start="2018-10-23" duration="3" complete="0" thirdDate="2018-10-08" thirdDate-constraint="0" expand="true">
<task id="82" name="Tests for Functional Req." color="#8cb6ce" meeting="false" start="2018-10-23" duration="3" complete="0" thirdDate="2018-10-15" thirdDate-constraint="0" expand="true"/>
<task id="83" name="Tests for Non-Functional Req." color="#8cb6ce" meeting="false" start="2018-10-23" duration="2" complete="0" thirdDate="2018-10-15" thirdDate-constraint="0" expand="true"/>
</task>
<task id="68" name="Comparison to Existing Implementation" color="#8cb6ce" meeting="false" start="2018-10-24" duration="2" complete="0" thirdDate="2018-10-08" thirdDate-constraint="0" expand="true"/>
<task id="69" name="Unit Test Plan" color="#8cb6ce" meeting="false" start="2018-10-24" duration="2" complete="0" thirdDate="2018-10-08" thirdDate-constraint="0" expand="true"/>
</task>
<task id="13" name="Design &amp; Document Revision" color="#8cb6ce" meeting="false" start="2018-10-31" duration="4" complete="89" thirdDate="2018-09-27" thirdDate-constraint="0" expand="false">
<task id="85" name="Anticipated and Unlikely Changes" color="#8cb6ce" meeting="false" start="2018-11-05" duration="1" complete="100" thirdDate="2018-10-18" thirdDate-constraint="0" expand="true"/>
<task id="103" name="Introduction" color="#8cb6ce" meeting="false" start="2018-10-31" duration="1" complete="100" thirdDate="2018-10-31" thirdDate-constraint="0" expand="true"/>
<task id="86" name="Module Hierarchy" color="#8cb6ce" meeting="false" start="2018-11-01" duration="2" complete="100" thirdDate="2018-10-18" thirdDate-constraint="0" expand="true"/>
<task id="91" name="Conncection between Requirements and Design" color="#8cb6ce" meeting="false" start="2018-10-31" duration="3" complete="100" thirdDate="2018-10-18" thirdDate-constraint="0" expand="true"/>
<task id="93" name="Module Decomposition" color="#8cb6ce" meeting="false" start="2018-10-31" duration="4" complete="100" thirdDate="2018-10-18" thirdDate-constraint="0" expand="true" cost-manual-value="33.0" cost-calculated="false"/>
<task id="96" name="Traceability Matrix" color="#8cb6ce" meeting="false" start="2018-10-31" duration="4" complete="100" thirdDate="2018-10-18" thirdDate-constraint="0" expand="true"/>
<task id="98" name="Use Heirarchy between modules" color="#8cb6ce" meeting="false" start="2018-11-02" duration="2" complete="100" thirdDate="2018-10-18" thirdDate-constraint="0" expand="true"/>
<task id="99" name="MIS" meeting="false" start="2018-11-02" duration="2" complete="0" expand="false">
<task id="100" name="Interface - Doxygen" color="#8cb6ce" meeting="false" start="2018-11-02" duration="2" complete="0" thirdDate="2018-10-29" thirdDate-constraint="0" expand="true"/>
<task id="101" name="Food &amp; barriers - Doxygen" color="#8cb6ce" meeting="false" start="2018-11-02" duration="2" complete="0" thirdDate="2018-10-29" thirdDate-constraint="0" expand="true"/>
<task id="102" name="Snake body &amp; movement - doxygen" color="#8cb6ce" meeting="false" start="2018-11-02" duration="2" complete="0" thirdDate="2018-10-29" thirdDate-constraint="0" expand="true"/>
</task>
</task>
<task id="113" name="Implementation" meeting="false" start="2018-11-06" duration="8" complete="0" expand="false">
<task id="114" name="highscore" color="#8cb6ce" meeting="false" start="2018-11-12" duration="2" complete="0" thirdDate="2018-10-22" thirdDate-constraint="0" expand="true"/>
<task id="117" name="Gameplay" color="#8cb6ce" meeting="false" start="2018-11-06" duration="8" complete="0" thirdDate="2018-10-22" thirdDate-constraint="0" expand="true"/>
<task id="119" name="init" color="#8cb6ce" meeting="false" start="2018-11-06" duration="2" complete="0" thirdDate="2018-10-22" thirdDate-constraint="0" expand="true"/>
<task id="121" name="Interface" color="#8cb6ce" meeting="false" start="2018-11-06" duration="8" complete="0" thirdDate="2018-10-22" thirdDate-constraint="0" expand="true"/>
<task id="123" name="Food" color="#8cb6ce" meeting="false" start="2018-11-08" duration="1" complete="0" thirdDate="2018-10-22" thirdDate-constraint="0" expand="true"/>
<task id="125" name="Themes" color="#8cb6ce" meeting="false" start="2018-11-13" duration="3" complete="0" thirdDate="2018-10-22" thirdDate-constraint="0" expand="true"/>
</task>
<task id="309" name="Testing" meeting="false" start="2018-11-13" duration="3" complete="0" expand="false">
<task id="310" name="System/Integration testing" color="#8cb6ce" meeting="false" start="2018-11-13" duration="1" complete="0" thirdDate="2018-10-12" thirdDate-constraint="0" expand="true">
<notes><![CDATA[Will be done throughout the development process to check if everything works together correctly. Closer to the final date, peers will be asked to try the game as part of system testing.]]></notes>
</task>
<task id="355" name="Gameplay - white box testing" color="#8cb6ce" meeting="false" start="2018-11-14" duration="2" complete="0" thirdDate="2018-10-22" thirdDate-constraint="0" expand="true"/>
<task id="357" name="Interface - white box testin" color="#8cb6ce" meeting="false" start="2018-11-14" duration="2" complete="0" thirdDate="2018-10-22" thirdDate-constraint="0" expand="true"/>
<task id="358" name="highscore - whitebox testing" color="#8cb6ce" meeting="false" start="2018-11-14" duration="2" complete="0" thirdDate="2018-10-22" thirdDate-constraint="0" expand="true"/>
<task id="359" name="Themes - white box" color="#8cb6ce" meeting="false" start="2018-11-14" duration="2" complete="0" thirdDate="2018-10-22" thirdDate-constraint="0" expand="true"/>
</task>
<task id="14" name="Revision 0 Demonstration" color="#8cb6ce" meeting="false" start="2018-11-12" duration="2" complete="0" thirdDate="2018-09-27" thirdDate-constraint="0" expand="true"/>
<task id="15" name="Final Demonstration" color="#8cb6ce" meeting="false" start="2018-11-19" duration="7" complete="0" thirdDate="2018-09-27" thirdDate-constraint="0" expand="true"/>
<task id="17" name="Peer Evaluation - Final Demo" color="#8cb6ce" meeting="false" start="2018-11-27" duration="4" complete="0" thirdDate="2018-09-27" thirdDate-constraint="0" expand="true"/>
<task id="18" name="Final Documentation" color="#8cb6ce" meeting="false" start="2018-12-06" duration="1" complete="0" thirdDate="2018-09-27" thirdDate-constraint="0" expand="true">
<notes><![CDATA[Problem Statement
Development Plan
Requirements Document
Design Document
Test Plan
Test Report
Users Guide (optional)
Source Code]]></notes>
</task>
</tasks>
<resources>
<resource id="3" name="Andy" function="4" contacts="hameea1@mcmaster.ca" phone="6479276093"/>
<resource id="4" name="Usman" function="4" contacts="irfanm7@mcmaster.ca" phone="4168787844">
<rate name="standard" value="99"/>
</resource>
<resource id="5" name="Vaibhav" function="8" contacts="chadhav@mcmaster.ca" phone="6474545182">
<rate name="standard" value="100"/>
</resource>
<resource id="6" name="Varun Hooda" function="2" contacts="hoodav@mcmaster.ca" phone=""/>
<resource id="7" name="Dr. Bokhari" function="3" contacts="bokhari@mcmaster.ca" phone=""/>
</resources>
<allocations>
<allocation task-id="31" resource-id="3" function="1" responsible="true" load="100.0"/>
<allocation task-id="8" resource-id="3" function="1" responsible="true" load="33.333332"/>
<allocation task-id="44" resource-id="3" function="1" responsible="true" load="100.0"/>
<allocation task-id="55" resource-id="3" function="1" responsible="true" load="100.0"/>
<allocation task-id="59" resource-id="3" function="1" responsible="true" load="100.0"/>
<allocation task-id="67" resource-id="3" function="1" responsible="true" load="100.0"/>
<allocation task-id="66" resource-id="3" function="1" responsible="true" load="20.0"/>
<allocation task-id="64" resource-id="3" function="1" responsible="true" load="100.0"/>
<allocation task-id="93" resource-id="3" function="1" responsible="true" load="33.0"/>
<allocation task-id="102" resource-id="3" function="1" responsible="true" load="100.0"/>
<allocation task-id="98" resource-id="3" function="1" responsible="true" load="100.0"/>
<allocation task-id="103" resource-id="3" function="1" responsible="true" load="100.0"/>
<allocation task-id="117" resource-id="3" function="1" responsible="true" load="50.0"/>
<allocation task-id="119" resource-id="3" function="1" responsible="true" load="100.0"/>
<allocation task-id="125" resource-id="3" function="1" responsible="false" load="20.0"/>
<allocation task-id="355" resource-id="3" function="1" responsible="false" load="50.0"/>
<allocation task-id="359" resource-id="3" function="1" responsible="true" load="20.0"/>
<allocation task-id="8" resource-id="4" function="4" responsible="false" load="33.333332"/>
<allocation task-id="49" resource-id="4" function="4" responsible="true" load="100.0"/>
<allocation task-id="65" resource-id="4" function="4" responsible="true" load="100.0"/>
<allocation task-id="66" resource-id="4" function="4" responsible="false" load="40.0"/>
<allocation task-id="101" resource-id="4" function="4" responsible="true" load="100.0"/>
<allocation task-id="91" resource-id="4" function="4" responsible="true" load="100.0"/>
<allocation task-id="93" resource-id="4" function="4" responsible="false" load="33.0"/>
<allocation task-id="96" resource-id="4" function="4" responsible="false" load="100.0"/>
<allocation task-id="117" resource-id="4" function="4" responsible="false" load="50.0"/>
<allocation task-id="123" resource-id="4" function="4" responsible="true" load="100.0"/>
<allocation task-id="125" resource-id="4" function="4" responsible="false" load="20.0"/>
<allocation task-id="355" resource-id="4" function="4" responsible="true" load="50.0"/>
<allocation task-id="359" resource-id="4" function="4" responsible="false" load="20.0"/>
<allocation task-id="8" resource-id="5" function="8" responsible="false" load="33.333332"/>
<allocation task-id="42" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="52" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="61" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="68" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="69" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="66" resource-id="5" function="8" responsible="false" load="40.0"/>
<allocation task-id="100" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="85" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="86" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="93" resource-id="5" function="8" responsible="false" load="34.0"/>
<allocation task-id="114" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="121" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="125" resource-id="5" function="8" responsible="true" load="60.0"/>
<allocation task-id="357" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="358" resource-id="5" function="8" responsible="true" load="100.0"/>
<allocation task-id="359" resource-id="5" function="8" responsible="false" load="60.0"/>
</allocations>
<vacations/>
<previous/>
<roles roleset-name="Default"/>
<roles>
<role id="1" name="UI Deisgner | Project Schedule Administrator"/>
<role id="2" name="TA"/>
<role id="3" name="Course Professor"/>
<role id="4" name="Main Programmer | Requirements Documentation"/>
<role id="5" name="Tester"/>
<role id="6" name="Requirements &amp; Documentation Editor"/>
<role id="7" name="Project Schedule Editor"/>
<role id="8" name="Git master | Testing Requirements"/>
</roles>
</project>
File added
## @file Food.py
# @author Usman Irfan
# @brief implements an abstract data type for a snake's food
# @date 11/09/2018
from random import randint
from init import *
## @brief An Abstract Data type which represents a one-unit of food
class Food():
## @brief Food constructor
# @details Initializes the size of the food, this needs to be the same as
# snake's block size
# @param blockSize the width and height of the square block representing the food
def __init__(self, blockSize):
self.size = blockSize
## @brief Draw method uses pygame to draw the food object on the window
# @param location A list which consists the x and y location of the food
def draw_food(self, location):
pygame.draw.rect(win, blue , (location[0],location[1], self.size, self.size))
## @brief redraw_food method redraws the food on the screen randomly
# @param x is the location of snake's x-axis head location
# @param y is the location of snake's y-axis head location
# @param location is a list that gives the location of present food
# @param screenSize is the size of the screen
def redraw_food(self, x, y, location,screenSize):
if( abs(x - location[0]) < 15 and abs(y - location[1]) < 15):
location[0] = randint(0, screenSize - self.size)
location[1] = randint(0, screenSize - self.size)
return location
## @file Snake_2.o.py
# @author Andy Hameed, Usman Irfan
# @brief implements gameplay and connects the different components of the game
# @date 11/09/2018
from random import randint
from Snake import *
from Food import *
# One size for all blocks created for snake
size = 20
#velocity and score
vel = 10
score = 0
#initial x and y coordinates of the snake
x = randint(0,screenSize - size)
y = randint(0, screenSize - size)
speed = 40
# 0 gives (- direction)
# 1 gives (+ direction)
direction = 0
# 0 - x-axis , 1 - y-axis
axis = 0
score = 0
location = []
food_x = randint(0,screenSize - size)
food_y = randint(0,screenSize - size)
location = [food_x, food_y]
##initialize snake
snake = Snake(size, 0, 20, 0)
food = Food(size)
#Loop through the events as long as the game is running
run = True
while run:
#delay controls part of speed
pygame.time.delay(speed)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#Each event type has an integer assigned to it. KEYDOWN has the code 2
if event.type == pygame.KEYDOWN:
if (event.key == pygame.K_LEFT):
snake.axis = 0; snake.direct = -1
if (event.key == pygame.K_RIGHT):
snake.axis = 0; snake.direct = 1
if (event.key == pygame.K_UP):
snake.axis = 1; snake.direct = -1
if (event.key == pygame.K_DOWN):
snake.axis = 1 ; snake.direct = 1
#Snake moving depending on axis and direction
if (snake.axis):
y += vel*snake.direct
else:
x += vel*snake.direct
#Boundary conditions for snake hitting window edge
if x < 0:
#x = 0
x = screenSize - size
if y < 0:
#y = 0
y = screenSize - size
if y > screenSize - size:
#y = 500 - size
y = 0
if x > screenSize - size:
#x = 500 - size
x = 0
#consumption of food block
food.redraw_food(x, y, location, screenSize)
if(abs(x - location[0]) < 15 and abs(y - location[1]) < 15):
score += 1
print(score)
win.fill(white)
#Draw food item
food.draw_food(location)
#food.update_score(x,y,location, score)
#Draw snake
snake.draw(x,y)
#update display
pygame.display.update()
pygame.quit()
## @file interface.py
# @author Vaibhav Chadha
# @brief implements the main interface of this game
# @date 11/09/2018
#importing necessary libraries
import pygame, sys
import highscore
## @brief A Class that will contain useful functions in order for the creation of main interface
class GUI():
## @brief A function for running other files
# @details Executes another python file when this is selected, Given that the file is in same folder.
# @param runfilename The name of the file to be executed
def runfile(runfilename):
with open(runfilename,"r") as rnf:
exec(rnf.read())
## @brief A method to create a button
# @details This method will make a box on the interface
# @param surface The background (surface) the box should be made on
# @param color The color of the button to be made
# @param Rect The coordinate of the button with the length and width
# @param width The width of the sides of button
def button(Surface, color,Rect,width):
pygame.draw.rect(Surface, color,Rect,width)
## @brief A method to display text
# @details This function will print the text on the interface
# @param text The text to be printed
# @param fontStyle The font Style of the text to be displayed
# @param fontSize The size of the text written
# @param color The color of the text
# @param coord The coordinate at which the text should start displaying
# @param surface The background (surface) the text should be printed on
def text(text,fontStyle,fontSize,color,coord,surface):
font = pygame.font.SysFont(fontStyle,fontSize)
text = font.render(text,True,color)
surface.blit(text,coord)
## @brief Makes the main interface of this game
# @details This will output the main page of this game by using the class above
def main():
pygame.init()
gray = [180,180,180]
darkgray = [100,100,100]
red = [255,0,0]
#while loop required to always refresh the page
while True:
game = pygame.display.set_mode((800, 610))
game.fill([213, 219, 219])
mousepos = pygame.mouse.get_pos() #checking mouse position
mouseclick = pygame.mouse.get_pressed()#checking mouse pressed
pygame.display.set_caption("Lets Play")
GUI.text('SNAKE GAME',"monospace", 80,red,(150,80),game)
#Adding the play game button
if (270 <= mousepos[0] <= 270+250 and 450 <= mousepos[1] <= 450+55 ):
#checks if the mouse is hovering over the button
GUI.button(game,darkgray, [270,455,250,50], 0)
#checking if the button is clicked
if mouseclick[0] == 1:
GUI.runfile('Gameplay.py')
else:
GUI.button(game,gray, [270,455,250,50], 0)
GUI.text('GAME TIME',"comicsansms", 40,[0, 0, 200],(275,450),game)
GUI.text('THEMES:',"comicsansms", 40,(0, 200, 0),(50,250),game)
GUI.text('SPEED',"comicsansms", 40,(0, 150, 0),(50,350),game)
#Highest Score
if ( 10 <= mousepos[0] <= 310 and 550 <= mousepos[1] <= 550 +55):
GUI.button(game,[200,0,0],[10,550,300,55], 0)
if mouseclick[0] == 1:
#GUI.runfile('highscore.py')
highscore.main()
else:
GUI.button(game,[180,180,180], [10,550,300,55], 0)
GUI.text('HIGHEST SCORE',"comicsansms", 35,(0, 0, 0),(10,550),game)
#If user wants to quit
if ( 670 <= mousepos[0] <= 670+105 and 550 <= mousepos[1] <= 550 +55):
GUI.button(game,[200,0,0],[670,550,105,55], 0)
if mouseclick[0] == 1:
pygame.quit()
sys.exit()
else:
GUI.button(game,[180,180,180], [670,550,105,55], 0)
GUI.text('QUIT',"comicsansms", 35,(0, 0, 0),(670,550),game)
pygame.display.update()
if __name__ == "__main__":
main()
## @file Snake.py
# @author Andy Hameed
# @brief implements an abstract data type for a snake
# @date 11/09/2018
from init import *
## @brief An Abstract Data type representing a snake character object
class Snake():
## @brief Snake constructor
# @details Initializes a Snake object with its initial attributes
# @param blockSize the width and height of the square block representing the snake
# @param direct The direction of the snake's movement
# @param speed The initial speed of the snake's movement
def __init__(self, blockSize, direct, speed, axis):
self.speed = speed
self.direct = direct
self.size = blockSize
self.axis = axis
## @brief Draw method uses pygame to draw the snake object
# @param x The x-coordinate where the block should be drawn
# @param y The y-coordinate where the block should be drawn
def draw(self,x,y):
pygame.draw.rect(win, red , (x,y, self.size, self.size))
File added
File added
File added
## @file highscore.py
# @author Vaibhav Chadha
# @brief implements the highscore interface
# @date 10/11/2018
import pygame, sys
## @brief A Class that will contain useful functions in order for the creation of highscore page
class HighScore():
## @brief A function for running other files
# @details Executes another python file when this is selected, Given that the file is in same folder.
# @param runfilename The name of the file to be executed
def runfile(runfilename):
with open(runfilename,"r") as rnf:
exec(rnf.read())
## @brief A method to display text
# @details This function will print the text on the interface
# @param text The text to be printed
# @param fontStyle The font Style of the text to be displayed
# @param fontSize The size of the text written
# @param color The color of the text
# @param coord The coordinate at which the text should start displaying
# @param surface The background (surface) the text should be printed on
def text(text,fontStyle,fontSize,color,coord,surface):
font = pygame.font.SysFont(fontStyle,fontSize)
text = font.render(text,True,color)
surface.blit(text,coord)
## @brief A method to create a button
# @details This method will make a box on the interface
# @param surface The background (surface) the box should be made on
# @param color The color of the button to be made
# @param Rect The coordinate of the button with the length and width
# @param width The width of the sides of button
def button(Surface, color,Rect,width):
pygame.draw.rect(Surface, color,Rect,width)
## @brief Finds the highest score from the file
# @details This writes the input from the file in an array and find the max number from it
def findHighscore():
infile = open("highscore.txt","r")
mylist = []
for line in infile:
a = line.strip()
mylist.append(a)
return max(mylist)
## @brief Makes the highscore interface
# @details This will output the final interface using the class above
# which can be seen by executing this function.
def main():
pygame.init()
red = [255,0,0]
while True:
mousepos = pygame.mouse.get_pos() #checking mouse position
mouseclick = pygame.mouse.get_pressed()#checking mouse pressed
highscore = pygame.display.set_mode((300, 150))
#highscore.fill([213, 219, 219])
pygame.display.set_caption("Highscore")
HighScore.text('Highest Score: ' + str(HighScore.findHighscore()),"comicsansms", 30,[0, 0, 200],(10,20),highscore)
HighScore.button(highscore,[0,0,0], [90,70,120,26], 0)
HighScore.text('Main Menu',"times", 25,red,(90,70),highscore)
if (90 <= mousepos[0] <= 90+120 and 70 <= mousepos[1] <= 70+27 ):
if mouseclick[0] == 1:
HighScore.runfile('Interface.py')
#Snake_Game.main()
HighScore.button(highscore,[0,0,0], [125,105,45,27], 0)
HighScore.text('Quit',"times", 25,red,(125,105),highscore)
if (125 <= mousepos[0] <= 125+45 and 105 <= mousepos[1] <= 105+27 ):
if mouseclick[0] == 1:
pygame.quit()
sys.exit()
pygame.display.update()
#main()
10
20
45
3
47
10
48
48
49
\ No newline at end of file
## @file Colors.py
# @author Andy Hameed
# @brief Define color constants
# @date 11/09/2018
import pygame
#initializing PyGame and setting game window dimensions
pygame.init()
screenSize = 500
win = pygame.display.set_mode((screenSize,screenSize))
pygame.display.set_caption("Snake 2.o")
#Define color constants
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
black = (0,0,0)
Copyright © Patrick Gillespie, http://patorjk.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\ No newline at end of file
JavaScript Snake<br/>
By Patrick Gillespie<br/>
License: MIT<br/>
http://patorjk.com/games/snake
This is a DOM-based game of Snake that I wrote in JavaScript a few years back.
Other than the full screen mode demonstrated in the code, it can also be
initialized in div tags within a page. Example:
var mySnakeBoard = new SNAKE.Board( {
boardContainer: "game-area",
fullScreen: false,
width: 580,
height:400
});
The comments are formatted a little strange because at the time I was playing
around with YUI Doc.
/*
JavaScript Snake
By Patrick Gillespie
http://patorjk.com/games/snake
*/
select {
border: black;
color: #3E2E44;
background: black;
}
button {
border: black;
color: #3E2E44;
background: black;
}
body {
margin:0px;
padding:0px;
}
#game-area {
margin:0px;
padding:0px;
}
#high-score {
position: relative;
left: 200px;
bottom: 50px;
}
#game-area:focus { outline: none; }
#mode-wrapper {
font-family: Verdana, arial, helvetica, sans-serif;
font-size: 14px;
color: black;
}
a.snake-link, a.snake-link:link, a.snake-link:visited {
color: black;
}
a.snake-link:hover {
color: #3E2E44;
}
.snake-pause-screen {
font-family: Verdana, arial, helvetica, sans-serif;
font-size: 14px;
position:absolute;
width:300px;
height:80px;
text-align:center;
top:50%;
left:50%;
margin-top:-40px;
margin-left:-150px;
display:none;
background-color:#3E2E44;
color:black;
}
.snake-panel-component {
position: absolute;
font-family: Verdana, arial, helvetica, sans-serif;
font-size: 14px;
color: black;
text-align: center;
background-color: #3E2E44;
padding: 8px;
margin: 0px;
}
.snake-snakebody-block {
margin: 0px;
padding: 0px;
background-color: #3E2E44;
position: absolute;
border: 0px solid black;
background-repeat: no-repeat;
}
.snake-snakebody-alive {
background-image: url('./images/dark-snakeblock.png');
}
.snake-snakebody-dead {
background-image: url('./images/dead-dark-snakeblock.png');
}
.snake-food-block {
margin: 0px;
padding: 0px;
background-color: black;
border: 2px solid #3E2E44;
position: absolute;
}
.snake-playing-field {
margin: 0px;
padding: 0px;
position: absolute;
background-color: #312E44;
border: 3px solid black;
}
.snake-game-container {
margin: 0px;
padding: 0px;
border-width: 0px;
border-style: none;
zoom: 1;
background-color: #3E2E44;
position: relative;
}
.snake-welcome-dialog {
padding: 8px;
margin: 0px;
background-color: black;
color: #3E2E44;
font-family: Verdana, arial, helvetica, sans-serif;
font-size: 14px;
position: absolute;
top: 50%;
left: 50%;
width: 300px;
/*height: 150px;*/
margin-top: -100px;
margin-left: -158px;
text-align: center;
display: block;
}
.snake-try-again-dialog {
padding: 8px;
margin: 0px;
background-color: black;
color: #312E44;
font-family: Verdana, arial, helvetica, sans-serif;
font-size: 14px;
position: absolute;
top: 50%;
left: 50%;
width: 300px;
height: 100px;
margin-top: -75px;
margin-left: -158px;
text-align: center;
display: none;
}
File added