Skip to content
Snippets Groups Projects
Commit 9a7ce73c authored by Hameed Andy's avatar Hameed Andy
Browse files
parents b6cbf825 38fbf5da
No related branches found
No related tags found
No related merge requests found
...@@ -26,7 +26,11 @@ class Food(): ...@@ -26,7 +26,11 @@ class Food():
# @param y is the location of snake's y-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 location is a list that gives the location of present food
# @param screenSize is the size of the screen # @param screenSize is the size of the screen
def redraw_food(self, x, y, location,screenSize): def redraw_food(self, x, y, location,screenSize, snake_loc):
if(abs(x - location[0]) < 15 and abs(y - location[1]) < 15): if(abs(x - location[0]) < 15 and abs(y - location[1]) < 15):
location[0] = randint(0, grid_length - 1) * self.size location[0] = randint(0, grid_length - 1) * self.size
location[1] = randint(0, grid_length - 1) * self.size location[1] = randint(0, grid_length - 1) * self.size
if(location[0], location[1] in snake_loc):
location[0] = randint(0, grid_length - 1) * self.size
location[1] = randint(0, grid_length - 1) * self.size
...@@ -7,144 +7,145 @@ from random import randint ...@@ -7,144 +7,145 @@ from random import randint
from Snake import * from Snake import *
from Food import * from Food import *
#defining a list to update snanke's length def Gameplay(speed, colour, backgroundColour):
snake_loc = [] #defining a list to update snanke's length
#variable to increment snake's length, initially it would be 1 snake_loc = []
snake_length = 1 #variable to increment snake's length, initially it would be 1
snake_length = 1
speed = 70
speed = 70
# 0 gives (- direction)
# 1 gives (+ direction)
direction = 1
# 0 - x-axis , 1 - y-axis
axis = 0
score = 0
# parameters for initializing food on the screen
food_location = []
food_x = randint(0, grid_length - 1) * size
food_y = randint(0, grid_length - 1) * size
food_location = [food_x, food_y]
##initialize snake and draw snake body somewhere on the screen
snake = Snake(size, 0, 20, 1)
pygame.draw.rect(win, red , [x,y, size, size])
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):
# if snake is moving up or down, turn left, otherwise don't turn
if (snake.axis): snake.direct = -1
snake.axis = 0
if (event.key == pygame.K_RIGHT):
#if snake is moving up or down turn right, otherwise dont turn
if (snake.axis): snake.direct = 1
snake.axis = 0
if (event.key == pygame.K_UP):
#if snake is moving left or right turn up, otherwise dont turn
if (not snake.axis): snake.direct = -1
snake.axis = 1
if (event.key == pygame.K_DOWN):
#if snake is moving left or right turn down, otherwise dont turn
if (not snake.axis): snake.direct = 1
snake.axis = 1
#Snake moving depending on axis and direction
if (snake.axis):
y += (size)*snake.direct
else:
x += (size)*snake.direct
#Boundary conditions for snake hitting window edge
if (x < 0 or
y < 0 or
y > screenSize - size or
x > screenSize - size):
pygame.quit() #for now, quit the game when snake hits boundary
## snake.die() # 0 gives (- direction)
# 1 gives (+ direction)
direction = 1
# 0 - x-axis , 1 - y-axis
axis = 0
#---------------------FOR WRAPING SNAKE AROUND WINDOW--------------------- score = 0
## 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
#------------------------------------------------- # parameters for initializing food on the screen
food_location = []
food_x = randint(0, grid_length - 1) * size
food_y = randint(0, grid_length - 1) * size
food_location = [food_x, food_y]
if(abs(x - food_location[0]) < 15 and abs(y - food_location[1]) < 15): ##initialize snake and draw snake body somewhere on the screen
score += 10
#increment the length by 3 unit every time snake = Snake(size, 0, 20, 1)
snake_length += 3 pygame.draw.rect(win, colour , [x,y, size, size])
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):
# if snake is moving up or down, turn left, otherwise don't turn
if (snake.axis): snake.direct = -1
snake.axis = 0
if (event.key == pygame.K_RIGHT):
#if snake is moving up or down turn right, otherwise dont turn
if (snake.axis): snake.direct = 1
snake.axis = 0
if (event.key == pygame.K_UP):
#if snake is moving left or right turn up, otherwise dont turn
if (not snake.axis): snake.direct = -1
snake.axis = 1
if (event.key == pygame.K_DOWN):
#if snake is moving left or right turn down, otherwise dont turn
if (not snake.axis): snake.direct = 1
snake.axis = 1
#Snake moving depending on axis and direction
if (snake.axis):
y += (size)*snake.direct
else:
x += (size)*snake.direct
#Boundary conditions for snake hitting window edge
if (x < 0 or
y < 0 or
y > screenSize - size or
x > screenSize - size):
pygame.quit() #for now, quit the game when snake hits boundary
## snake.die()
#---------------------FOR WRAPING SNAKE AROUND WINDOW---------------------
win.fill(white) ## if x < 0:
## #x = 0
font = pygame.font.SysFont("times",30) ## x = screenSize - size
text = font.render("Score = " + str(score),True,[0,0,0]) ## if y < 0:
win.blit(text,(0,0)) ## #y = 0
#function to print ## y = screenSize - size
#consumption of food block ## if y > screenSize - size:
food.redraw_food(x, y, food_location, screenSize) ## #y = 500 - size
## y = 0
if ([x,y] in snake_loc) and snake_length > 1: ## if x > screenSize - size:
pygame.time.delay(1000) ## #x = 500 - size
pygame.quit() #quit for now, but should return to main menu ## x = 0
snake_head = [] #-------------------------------------------------
snake_head.append(x)
snake_head.append(y) if(abs(x - food_location[0]) < 15 and abs(y - food_location[1]) < 15):
score += 10
snake_loc.append(snake_head)
#increment the length by 3 unit every time
snake_length += 3
snake_blocks = len(snake_loc)
#Draw food item win.fill(backgroundColour)
food.draw_food(food_location)
font = pygame.font.SysFont("times",30)
text = font.render("Score = " + str(score),True,[0,0,0])
if snake_blocks > snake_length: win.blit(text,(0,0))
#keep updating the new block
del snake_loc[0] if ([x,y] in snake_loc) and snake_length > 1:
''' pygame.time.delay(1000)
Logic for updating the length is taken from: pygame.quit() #quit for now, but should return to main menu
CodeWithHarry, CodeWithHarry. “Snakes Game: Length Increment Logic - Python Game Development Using Pygame In Hindi #17.”
YouTube, YouTube, 2 Oct. 2018, snake_head = []
www.youtube.com/watch?v=mkGJb0W03DM&index=17&list=PLu0W_9lII9ailUQcxEPZrWgDoL36BtPYb. snake_head.append(x)
''' snake_head.append(y)
#Draw snake snake_loc.append(snake_head)
snake.draw(snake_loc)
#update display #function to print
pygame.display.update() #consumption of food block
food.redraw_food(x, y, food_location, screenSize, snake_loc)
pygame.quit()
snake_blocks = len(snake_loc)
#Draw food item
food.draw_food(food_location)
if snake_blocks > snake_length:
#keep updating the new block
del snake_loc[0]
'''
Logic for updating the length is taken from:
CodeWithHarry, CodeWithHarry. “Snakes Game: Length Increment Logic - Python Game Development Using Pygame In Hindi #17.”
YouTube, YouTube, 2 Oct. 2018,
www.youtube.com/watch?v=mkGJb0W03DM&index=17&list=PLu0W_9lII9ailUQcxEPZrWgDoL36BtPYb.
'''
#Draw snake
snake.draw(snake_loc)
#update display
pygame.display.update()
pygame.quit()
...@@ -8,13 +8,6 @@ import highscore, theme ...@@ -8,13 +8,6 @@ import highscore, theme
## @brief A Class that will contain useful functions in order for the creation of main interface ## @brief A Class that will contain useful functions in order for the creation of main interface
class GUI(): 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 ## @brief A method to create a button
# @details This method will make a box on the interface # @details This method will make a box on the interface
...@@ -41,70 +34,60 @@ class GUI(): ...@@ -41,70 +34,60 @@ class GUI():
# @details This will output the main page of this game by using the class above # @details This will output the main page of this game by using the class above
def main(): def main():
pygame.init() pygame.init()
lightgray = [200,200,200] lightBlue = [200,200,200]
gray = [180,180,180]
darkgray = [100,100,100] darkgray = [100,100,100]
red = [255,0,0] image1 = pygame.image.load("Snake_Game_Logo_background.png")
image2 = pygame.image.load("snake_image.png")
#while loop required to always refresh the page #while loop required to always refresh the page
run = True run = True
while run: while run:
game = pygame.display.set_mode((800, 610)) game = pygame.display.set_mode((800, 610))
game.fill([213, 219, 219]) game.blit(image1,(0,0))
game.blit(image2,(550,0))
mousepos = pygame.mouse.get_pos() #checking mouse position mousepos = pygame.mouse.get_pos() #checking mouse position
mouseclick = pygame.mouse.get_pressed()#checking mouse pressed mouseclick = pygame.mouse.get_pressed()#checking mouse pressed
pygame.display.set_caption("Lets Play") pygame.display.set_caption("Lets Play")
GUI.text('SNAKE GAME',"monospace", 100,red,(100,80),game)
#Adding the play game button #Adding the play game button
if (130 <= mousepos[0] <= 130+250 and 250 <= mousepos[1] <= 250+100 ): if (400 <= mousepos[0] <= 400+170 and 250 <= mousepos[1] <= 300+50 ):
#checks if the mouse is hovering over the button #checks if the mouse is hovering over the button
GUI.button(game,darkgray, [130,250,250,100], 0) GUI.button(game,darkgray, [400,250,170,50], 0)
#checking if the button is clicked #checking if the button is clicked
if mouseclick[0] == 1: if mouseclick[0] == 1:
theme.Themes.themes() theme.Themes.themes()
else: else:
GUI.button(game,lightgray, [130,250,250,100], 0) GUI.button(game,lightBlue, [400,250,170,50], 0)
GUI.text('Beginner',"comicsansms", 40,[10, 200, 10],(170,270),game) GUI.text('Beginner',"comicsansms", 35,[10, 200, 10],(410,250),game)
if (430 <= mousepos[0] <= 430+250 and 250 <= mousepos[1] <= 250+100 ): if (430 <= mousepos[0] <= 430+220 and 350 <= mousepos[1] <= 350+50 ):
#checks if the mouse is hovering over the button #checks if the mouse is hovering over the button
GUI.button(game,darkgray, [430,250,250,100], 0) GUI.button(game,darkgray, [430,350,220,50], 0)
#checking if the button is clicked #checking if the button is clicked
if mouseclick[0] == 1: if mouseclick[0] == 1:
theme.Themes.themes() theme.Themes.themes()
else: else:
GUI.button(game,lightgray, [430,250,250,100], 0) GUI.button(game,lightBlue, [430,350,220,50], 0)
GUI.text('Intermediate',"comicsansms", 40,[250, 250, 10],(431,270),game) GUI.text('Intermediate',"comicsansms", 35,[250, 250, 10],(430,350),game)
if (270 <= mousepos[0] <= 270+250 and 400 <= mousepos[1] <= 400+100 ): if (400 <= mousepos[0] <= 400+180 and 450 <= mousepos[1] <= 450+50 ):
#checks if the mouse is hovering over the button #checks if the mouse is hovering over the button
GUI.button(game,darkgray, [270,400,250,100], 0) GUI.button(game,darkgray, [400,450,180,50], 0)
#checking if the button is clicked #checking if the button is clicked
if mouseclick[0] == 1: if mouseclick[0] == 1:
theme.Themes.themes() theme.Themes.themes()
else: else:
GUI.button(game,lightgray, [270,400,250,100], 0) GUI.button(game,lightBlue, [400,450,180,50], 0)
GUI.text('Advanced',"comicsansms", 40,[200, 10, 30],(300,420),game) GUI.text('Advanced',"comicsansms", 35,[200, 10, 30],(410,450),game)
#Highest Score if (15 <= mousepos[0] <= 15+115 and 565 <= mousepos[1] <= 565+35 ):
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: if mouseclick[0] == 1:
highscore.main() highscore.main()
else:
GUI.button(game,[180,180,180], [10,550,300,55], 0) if (725 <= mousepos[0] <= 725+50 and 565 <= mousepos[1] <= 565+35 ):
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: if mouseclick[0] == 1:
pygame.quit() pygame.quit()
sys.exit() 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() pygame.display.update()
......
BlankProjectTemplate/src/Snake_Game_Logo_background.png

23.3 KiB

No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
BlankProjectTemplate/src/snake_image.png

36.9 KiB

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