diff --git a/BlankProjectTemplate/src/Gameplay.py b/BlankProjectTemplate/src/Gameplay.py index 3ef365efa71c43109372d7509cd280e04464898e..440eec0c71766b4f3a087085dcd9ba88889e2a0a 100644 --- a/BlankProjectTemplate/src/Gameplay.py +++ b/BlankProjectTemplate/src/Gameplay.py @@ -6,6 +6,7 @@ from random import randint from Snake import * from Food import * +import ScoreDisplay def game(speed, colour,food_colour, backgroundColour): x = randint(0, grid_length) * size @@ -93,8 +94,9 @@ def game(speed, colour,food_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() @@ -121,8 +123,9 @@ def game(speed, colour,food_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) @@ -157,4 +160,4 @@ def game(speed, colour,food_colour, backgroundColour): pygame.quit() -#game(70, [255,0,0], [255,255,255]) +#game(70, [255,0,0], [255,255,0],[0,0,0]) diff --git a/BlankProjectTemplate/src/ScoreDisplay.py b/BlankProjectTemplate/src/ScoreDisplay.py new file mode 100644 index 0000000000000000000000000000000000000000..aeb64aa943c7db2b3d1ad70556e6cc021024aec5 --- /dev/null +++ b/BlankProjectTemplate/src/ScoreDisplay.py @@ -0,0 +1,50 @@ +## @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) diff --git a/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc index fa2123b29d79ff150dc102688744d0a62d3461e4..966ab0d08c40653f062c3152f4be42939b82b723 100644 Binary files a/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc and b/BlankProjectTemplate/src/__pycache__/Food.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/Gameplay.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Gameplay.cpython-37.pyc index 334480d3b3ab6b61d9176893fdc21bbc64603e10..6cda4b39a3bf05045634d7eaf9610a2fa941fd37 100644 Binary files a/BlankProjectTemplate/src/__pycache__/Gameplay.cpython-37.pyc and b/BlankProjectTemplate/src/__pycache__/Gameplay.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/Interface.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Interface.cpython-37.pyc index b4486b601870dd96aab93d9a5bc948caeb117a04..29a8d08971a5270372d9c8bb392a7e6604ee1439 100644 Binary files a/BlankProjectTemplate/src/__pycache__/Interface.cpython-37.pyc and b/BlankProjectTemplate/src/__pycache__/Interface.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/ScoreDisplay.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/ScoreDisplay.cpython-37.pyc new file mode 100644 index 0000000000000000000000000000000000000000..3aab2cc5d38fec9fe6801830348da821771a13a4 Binary files /dev/null and b/BlankProjectTemplate/src/__pycache__/ScoreDisplay.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc index a45d2c5b189b3f5a551175338cb9f563831f4cff..0a26c773271ffab1a37612048cfdcd740e063b89 100644 Binary files a/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc and b/BlankProjectTemplate/src/__pycache__/Snake.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/highscore.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/highscore.cpython-37.pyc index b54b7599ad8d9e8979d97adfde77edd319d5c62e..74dd900c1b885fdbb7e3e982d307d86571ed5f78 100644 Binary files a/BlankProjectTemplate/src/__pycache__/highscore.cpython-37.pyc and b/BlankProjectTemplate/src/__pycache__/highscore.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/init.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/init.cpython-37.pyc index d460e4e19a6f03caa21bb80935a5042c790416ba..95e1b3bc3a0783dcab621df41863c982f1c91a15 100644 Binary files a/BlankProjectTemplate/src/__pycache__/init.cpython-37.pyc and b/BlankProjectTemplate/src/__pycache__/init.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/__pycache__/theme.cpython-37.pyc b/BlankProjectTemplate/src/__pycache__/theme.cpython-37.pyc index a53346a2afcf54be0ff1142c8e096f69f9f93434..4faee4122ea7ce444a0c460220c164f534932e8b 100644 Binary files a/BlankProjectTemplate/src/__pycache__/theme.cpython-37.pyc and b/BlankProjectTemplate/src/__pycache__/theme.cpython-37.pyc differ diff --git a/BlankProjectTemplate/src/highscore.py b/BlankProjectTemplate/src/highscore.py index cf4e413d78a78401ee81c5a3388b56bda1b1ef4b..3146b88ce425b68074870c6c86f553ead2895ae9 100644 --- a/BlankProjectTemplate/src/highscore.py +++ b/BlankProjectTemplate/src/highscore.py @@ -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) diff --git a/BlankProjectTemplate/src/highscore.txt b/BlankProjectTemplate/src/highscore.txt index 4b3506c45635b6ed5b8d2e06add0f0de9d2a93c5..2edeafb09db0093bae6ff060e2dcd2166f5c9387 100644 --- a/BlankProjectTemplate/src/highscore.txt +++ b/BlankProjectTemplate/src/highscore.txt @@ -1,9 +1 @@ -10 -20 -45 -3 -47 -10 -48 -48 -49 \ No newline at end of file +20 \ No newline at end of file diff --git a/BlankProjectTemplate/src/theme.py b/BlankProjectTemplate/src/theme.py index 1785b717b96f2357dd74c342fd38211e91954288..0e5da1b8b580ff21ea01844f4898240dab458736 100644 --- a/BlankProjectTemplate/src/theme.py +++ b/BlankProjectTemplate/src/theme.py @@ -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()