Ask her out with python Game
A couple of days ago, I was talking with one of my friends about the girl that he likes to ask her out. Let's call them Mr.Potato and Ms.Tomato. Mr.Poatato is one of my best friends, and I know him since high school. He is so shy and it's kinda hard for him to ask Ms.tomato out directly.
He is a game developer and recently, he came up with the idea of asking her out by making a game, and ask her out at the end of the game.
We discussed too much about what is the best game, but in the end, the classy, famous, snake game was the winner.
I decided to share this blog because I personally found it interesting.
You can also find the source code in my Github.
Required skills for this project
Snake game is a basic project on python that does not require too many skill.
- Things you have to know are:
- python
- pygame library
Don't worry if you are not fammiliar with pygame, It's not a big a deal.
Installation
The pygame library is an open-source module for the Python programming language specifically intended to help you make games and other multimedia applications. Built on top of the highly portable SDL (Simple DirectMedia Layer) development library, pygame can run across many platforms and operating systems.
pip install pygame
Step 1
import necessary libaries, in this project we need 3 libaries. Two of which are python built in libaries.
# importing libraries
import pygame
import time
import random
After that we define snake speed, width and height of the windows in which the game will be played.
snake_speed = 15
# Window size
window_x = 720
window_y = 480
After all we define the colors:
# defining colors
black = pygame.Color(0, 0, 0)
pink = pygame.Color(255, 192, 203)
red = pygame.Color(255, 0, 0)
purple = pygame.Color(226, 69, 239)
blue = pygame.Color(0, 0, 255)
Step 2
After we import the pygame, we have to initialized Pygame using pygame.init() method.
- Create a game window using the width and height defined in the previous step.
- Here pygame.time.Clock() will be used further in the main logic of the game to change the speed of the snake.
# Initialising pygame
pygame.init()
# Initialise game window
pygame.display.set_caption('CopyTheCodes Snakes')
game_window = pygame.display.set_mode((window_x, window_y))
# FPS (frames per second) controller
fps = pygame.time.Clock()
Step 3
we have to initialized snake position and it's size.
Let's define the snake defualt postion first.
# defining snake default position
snake_position = [100, 50]
Then we make the body of the snake. start with 4 block for body sized sounds good.
# defining first 4 blocks of snake
# body
snake_body = [ [100, 50],
[90, 50],
[80, 50],
[70, 50]
]
After that, we have to define the position of food, fruit or whatever that snake eats.
# fruit position
fruit_position = [random.randrange(1, (window_x//10)) * 10,
random.randrange(1, (window_y//10)) * 10]
fruit_spawn = True
Now we have to set the snake default direction.
# setting default snake direction
# towards right
direction = 'RIGHT'
change_to = direction
Step 4
We need to display the score of the player. we can build a function that do this for us.
- In this function, we first set the font size and font color.
- Then we are using render to create a background surface that we are going to change whenever our score updates.
- Create a rectangular object for the text surface object (where text will be refreshed)
- Then, we are displaying our score using blit. blit takes two argument screen.blit(background,(x,y))
# initial score
score = 0
# displaying Score function
def show_score(choice, color, font, size):
# creating font object score_font
score_font = pygame.font.SysFont(font, size)
# create the display surface object
# score_surface
score_surface = score_font.render('Score : ' + str(score), True, color)
# create a rectangular object for the
# text surface object
score_rect = score_surface.get_rect()
# displaying text
game_window.blit(score_surface, score_rect)
Step 5
Now create a game over function that will represent the score after the snake is hit by a wall or itself.
- We are constructing a font object to display scores in the first line.
- Then, text surfaces are developed to represent scores.
- The position of the text is then set to be in the center of the playable area.
- Display the scores using blit and updating the score by updating the surface using flip().
- Sleep(5) is used to delay the window's closure by five seconds before using quit ().
# game over function
def game_over():
# creating font object my_font
my_font = pygame.font.SysFont('times new roman', 30)
# creating a text surface on which text
# will be drawn
game_over_surface = my_font.render('If I ask you out, will that ruin our pen pal thing?', True, red)
# create a rectangular object for the text
# surface object
game_over_rect = game_over_surface.get_rect()
# setting position of the text
game_over_rect.midtop = (window_x/2, window_y/4)
# blit will draw the text on screen
game_window.blit(game_over_surface, game_over_rect)
pygame.display.flip()
# after 5 seconds we will quit the
# program
time.sleep(5)
# deactivating pygame library
pygame.quit()
# quit the program
quit()
Step 6
We are currently developing our primary function, which will accomplish the following tasks:
- The keys that control the snake's movement will be verified, and after that, a particular restriction will be put in place to prevent the snake from immediately moving in the other way.
- Following that, if a snake eats the fruit, the score will be increased by 10 and a new fruit will be produced.
- After that, we check that the snake hit the wall or hit itself or not. If so, we call the game over function.
- And in the end we displayed the message which Mr.Potato wants to tell to Ms.Tomato.
# Main Function
while True:
# handling key events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change_to = 'UP'
if event.key == pygame.K_DOWN:
change_to = 'DOWN'
if event.key == pygame.K_LEFT:
change_to = 'LEFT'
if event.key == pygame.K_RIGHT:
change_to = 'RIGHT'
# If two keys pressed simultaneously
# we don't want snake to move into two directions
# simultaneously
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
if change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
# Moving the snake
if direction == 'UP':
snake_position[1] -= 10
if direction == 'DOWN':
snake_position[1] += 10
if direction == 'LEFT':
snake_position[0] -= 10
if direction == 'RIGHT':
snake_position[0] += 10
# Snake body growing mechanism
# if fruits and snakes collide then scores will be
# incremented by 10
snake_body.insert(0, list(snake_position))
if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:
score += 10
fruit_spawn = False
else:
snake_body.pop()
if not fruit_spawn:
fruit_position = [random.randrange(1, (window_x//10)) * 10,
random.randrange(1, (window_y//10)) * 10]
fruit_spawn = True
game_window.fill(black)
for pos in snake_body:
pygame.draw.rect(game_window, purple, pygame.Rect(
pos[0], pos[1], 10, 10))
pygame.draw.rect(game_window, pink, pygame.Rect(
fruit_position[0], fruit_position[1], 10, 10))
# Game Over conditions
if snake_position[0] < 0 or snake_position[0] > window_x-10:
game_over()
if snake_position[1] < 0 or snake_position[1] > window_y-10:
game_over()
# Touching the snake body
for block in snake_body[1:]:
if snake_position[0] == block[0] and snake_position[1] == block[1]:
game_over()
# displaying score countinuously
show_score(1, pink, 'times new roman', 20)
# Refresh game screen
pygame.display.update()
# Frame Per Second /Refresh Rate
fps.tick(snake_speed)
Here is the full code:
# importing libraries
import pygame
import time
import random
snake_speed = 15
# Window size
window_x = 720
window_y = 480
# defining colors
black = pygame.Color(0, 0, 0)
pink = pygame.Color(255, 192, 203)
red = pygame.Color(255, 0, 0)
purple = pygame.Color(226, 69, 239)
blue = pygame.Color(0, 0, 255)
# Initialising pygame
pygame.init()
# Initialise game window
pygame.display.set_caption('CopyTheCodes Snakes')
game_window = pygame.display.set_mode((window_x, window_y))
# FPS (frames per second) controller
fps = pygame.time.Clock()
# defining snake default position
snake_position = [100, 50]
# defining first 4 blocks of snake body
snake_body = [[100, 50],
[90, 50],
[80, 50],
[70, 50]
]
# fruit position
fruit_position = [random.randrange(1, (window_x//10)) * 10,
random.randrange(1, (window_y//10)) * 10]
fruit_spawn = True
# setting default snake direction towards
# right
direction = 'RIGHT'
change_to = direction
# initial score
score = 0
# displaying Score function
def show_score(choice, color, font, size):
# creating font object score_font
score_font = pygame.font.SysFont(font, size)
# create the display surface object
# score_surface
score_surface = score_font.render('Score : ' + str(score), True, color)
# create a rectangular object for the text
# surface object
score_rect = score_surface.get_rect()
# displaying text
game_window.blit(score_surface, score_rect)
# game over function
def game_over():
# creating font object my_font
my_font = pygame.font.SysFont('times new roman', 30)
# creating a text surface on which text
# will be drawn
game_over_surface = my_font.render('If I ask you out, will that ruin our pen pal thing?', True, red)
# create a rectangular object for the text
# surface object
game_over_rect = game_over_surface.get_rect()
# setting position of the text
game_over_rect.midtop = (window_x/2, window_y/4)
# blit will draw the text on screen
game_window.blit(game_over_surface, game_over_rect)
pygame.display.flip()
# after 5 seconds we will quit the program
time.sleep(5)
# deactivating pygame library
pygame.quit()
# quit the program
quit()
# Main Function
while True:
# handling key events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
change_to = 'UP'
if event.key == pygame.K_DOWN:
change_to = 'DOWN'
if event.key == pygame.K_LEFT:
change_to = 'LEFT'
if event.key == pygame.K_RIGHT:
change_to = 'RIGHT'
# If two keys pressed simultaneously
# we don't want snake to move into two
# directions simultaneously
if change_to == 'UP' and direction != 'DOWN':
direction = 'UP'
if change_to == 'DOWN' and direction != 'UP':
direction = 'DOWN'
if change_to == 'LEFT' and direction != 'RIGHT':
direction = 'LEFT'
if change_to == 'RIGHT' and direction != 'LEFT':
direction = 'RIGHT'
# Moving the snake
if direction == 'UP':
snake_position[1] -= 10
if direction == 'DOWN':
snake_position[1] += 10
if direction == 'LEFT':
snake_position[0] -= 10
if direction == 'RIGHT':
snake_position[0] += 10
# Snake body growing mechanism
# if fruits and snakes collide then scores
# will be incremented by 10
snake_body.insert(0, list(snake_position))
if snake_position[0] == fruit_position[0] and snake_position[1] == fruit_position[1]:
score += 10
fruit_spawn = False
else:
snake_body.pop()
if not fruit_spawn:
fruit_position = [random.randrange(1, (window_x//10)) * 10,
random.randrange(1, (window_y//10)) * 10]
fruit_spawn = True
game_window.fill(black)
for pos in snake_body:
pygame.draw.rect(game_window, purple,
pygame.Rect(pos[0], pos[1], 10, 10))
pygame.draw.rect(game_window, pink, pygame.Rect(
fruit_position[0], fruit_position[1], 10, 10))
# Game Over conditions
if snake_position[0] < 0 or snake_position[0] > window_x-10:
game_over()
if snake_position[1] < 0 or snake_position[1] > window_y-10:
game_over()
# Touching the snake body
for block in snake_body[1:]:
if snake_position[0] == block[0] and snake_position[1] == block[1]:
game_over()
# displaying score countinuously
show_score(1, pink, 'times new roman', 20)
# Refresh game screen
pygame.display.update()
# Frame Per Second /Refresh Rate
fps.tick(snake_speed)