Skip to content
Snippets Groups Projects
Commit 997eb9c1 authored by Hameed Andy's avatar Hameed Andy
Browse files
parents 8eda1ee4 23bd47ef
No related branches found
No related tags found
No related merge requests found
Showing
with 89 additions and 37 deletions
......@@ -18,8 +18,8 @@ class Food():
## @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, black1 , (location[0],location[1], self.size, self.size))
def draw_food(self, food_colour, location):
pygame.draw.rect(win, food_colour , (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
......@@ -31,6 +31,6 @@ class Food():
if(abs(x - location[0]) < 15 and abs(y - location[1]) < 15):
location[0] = randint(0, grid_length - 1) * self.size
location[1] = randint(0, grid_length - 1) * self.size
if(location[0], location[1] in snake_loc):
if(location in snake_loc):
location[0] = randint(0, grid_length - 1) * self.size
location[1] = randint(0, grid_length - 1) * self.size
......@@ -6,8 +6,9 @@
from random import randint
from Snake import *
from Food import *
import ScoreDisplay
def game(speed, colour, backgroundColour):
def game(speed, colour,food_colour, backgroundColour):
x = randint(0, grid_length) * size
y = randint(0, grid_length) * size
#defining a list to update snanke's length
......@@ -95,8 +96,9 @@ def game(speed, colour, backgroundColour):
y < 0 or
y > screenSize - size or
x > screenSize - size):
pygame.quit() #for now, quit the game when snake hits boundary
ScoreDisplay.display(score)
#pygame.quit() #for now, quit the game when snake hits boundary
## snake.die()
......@@ -123,8 +125,9 @@ def game(speed, colour, backgroundColour):
win.blit(text,(0,0))
if ([x,y] in snake_loc) and snake_length > 1:
pygame.time.delay(1000)
pygame.quit() #quit for now, but should return to main menu
pygame.time.delay(1000)
# pygame.quit() #quit for now, but should return to main menu
ScoreDisplay.display(score)
snake_head = []
snake_head.append(x)
......@@ -139,7 +142,7 @@ def game(speed, colour, backgroundColour):
snake_blocks = len(snake_loc)
#Draw food item
food.draw_food(food_location)
food.draw_food(food_colour, food_location)
if snake_blocks > snake_length:
......@@ -153,10 +156,10 @@ def game(speed, colour, backgroundColour):
'''
#Draw snake
snake.draw(snake_loc)
snake.draw(colour,snake_loc)
#update display
pygame.display.update()
pygame.quit()
#game(70, [255,0,0], [255,255,255])
#game(70, [255,0,0], [255,255,0],[0,0,0])
......@@ -50,7 +50,7 @@ def main():
pygame.display.set_caption("Lets Play")
#Adding the play game button
if (400 <= mousepos[0] <= 400+170 and 250 <= mousepos[1] <= 300+50 ):
if (400 <= mousepos[0] <= 400+170 and 250 <= mousepos[1] <= 250+50 ):
#checks if the mouse is hovering over the button
GUI.button(game,darkgray, [400,250,170,50], 0)
#checking if the button is clicked
......
## @file ScoreDisplay.py
# @author Vaibhav Chadha
# @brief implements the interface after the snake dies
# @date 11/22/2018
import pygame, sys
import highscore, Interface
def storeScore(score):
prevScore = int(highscore.HighScore.findHighScore())
#print(prevScore)
if(score > prevScore):
infile = open("highscore.txt", "w")
infile.write(str(score))
else: return
def display(score):
pygame.init()
storeScore(score)
run = True
while run:
mousepos = pygame.mouse.get_pos() #checking mouse position
mouseclick = pygame.mouse.get_pressed()#checking mouse pressed
lastPage = pygame.display.set_mode((400, 400))
lastPage.fill([200,150,100])
highscore.HighScore.text('WOAHH Your Score is ' + str(score),"comicsansms", 30,[0, 200, 200],(10,50),lastPage)
if (100 <= mousepos[0] <= 100+200 and 150 <= mousepos[1] <= 150+60 ):
highscore.HighScore.button(lastPage,[0,255,0], [100,150,200,60], 0)
if mouseclick[0] == 1:
Interface.main()
else:
highscore.HighScore.button(lastPage,[0,170,0], [100,150,200,60], 0)
highscore.HighScore.text('PLAY MORE!',"comicsansms", 30,[0,0,0],(110,160),lastPage)
if (100 <= mousepos[0] <= 100+200 and 260 <= mousepos[1] <= 260+60 ):
highscore.HighScore.button(lastPage,[250,0,0], [100,260,200,60], 0)
if mouseclick[0] == 1:
pygame.quit()
sys.exit()
else:
highscore.HighScore.button(lastPage,[170,0,0], [100,260,200,60], 0)
highscore.HighScore.text('CAN\'T PLAY',"comicsansms", 30,[0,0,0],(110,270),lastPage)
pygame.display.update()
#display(99)
......@@ -23,9 +23,9 @@ class Snake():
## @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,snake_loc):
def draw(self,colour, snake_loc):
for x,y in snake_loc:
pygame.draw.rect(win, lightB , [x,y, self.size, self.size])
pygame.draw.rect(win, colour , [x,y, self.size, self.size])
## pygame.draw.circle(win, lightB , [x,y], int(self.size/2))
def die():
......
No preview for this file type
No preview for this file type
No preview for this file type
File added
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -2,7 +2,7 @@
# @author Vaibhav Chadha
# @brief implements the highscore interface
# @date 11/09/2018
import pygame, sys, Interface
import pygame, sys, Interface, pathlib
## @brief A Class that will contain useful functions in order for the creation of highscore page
class HighScore():
......@@ -31,13 +31,20 @@ class HighScore():
## @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)
def findHighScore():
file = pathlib.Path("highscore.txt")
if file.exists ():
infile = open("highscore.txt","r")
mylist = []
for line in infile:
a = line.strip()
mylist.append(a)
if (len(mylist) == 0):
return 0
else: return max(mylist)
else:
makingfile = open("highscore.txt","w+")
return 0
## @brief Makes the highscore interface
# @details This will output the final interface using the class above
# which can be seen by executing this function.
......@@ -51,7 +58,7 @@ def main():
#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.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)
......
10
20
45
3
47
10
48
48
49
\ No newline at end of file
20
\ No newline at end of file
......@@ -10,7 +10,7 @@ from random import randint
pygame.init()
screenSize = 500
win = pygame.display.set_mode((screenSize,screenSize))
pygame.display.set_caption("Snake 2.o")
pygame.display.set_caption("Snake")
#Define color constants
white = (255,255,255)
......
......@@ -55,13 +55,13 @@ class Themes():
Themes.text('Regular',"maiandragd", 40,white_green,(80,170),theme)
if (75 <= mousepos[0] <= 75+150 and 150 <= mousepos[1] <= 150+100 ):
if mouseclick[0] == 1:
Gameplay.game(speed,[255,0,0],[255,255,255])
Gameplay.game(speed,[255,0,0],[0,255,0],[255,255,255])
Themes.button(theme,black1, [280,150,150,100], 0)
Themes.text('Dark',"maiandragd", 40,white,(305,170),theme)
if (280 <= mousepos[0] <= 280+150 and 150 <= mousepos[1] <= 150+100 ):
if mouseclick[0] == 1:
Gameplay.game(speed,white,[10,10,10])
Gameplay.game(speed,white,[255,255,0],[10,10,10])
Themes.button(theme,lightBlue, [175,300,80,50], 0)
Themes.button(theme,[200,200,200], [175,350,80,50], 0)
......@@ -71,8 +71,8 @@ class Themes():
if (180 <= mousepos[0] <= 180+160 and 315 <= mousepos[1] <= 315+100 ):
if mouseclick[0] == 1:
x = randint(0, 1)
if(x == 1): Gameplay.game(speed,[255,0,0],[255,255,255])
else: Gameplay.game(speed,[255,255,255],[10,10,10])
if(x == 1): Gameplay.game(speed,[255,0,0],[0,255,0],[255,255,255])
else: Gameplay.game(speed,white,[255,255,0],[10,10,10])
pygame.display.update()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment