STAY INFORMED
following content serves as a personal note and may lack complete accuracy or
certainty.
Minimal-Mistakes instruction
Useful vscode Shortcut Keys
Unix Commands
npm Commands
Vim Commands
Git Note
Useful Figma Shortcut Keys
Snake Game Project_2
food class
We also need food class to feed the snake and get the score. If snake eats food, the length of the body is increased and get +1 score. The food appears at the random location.
class Food:
def new_location(self):
global food_x, food_y
food_x = random.randrange(1, width - scale)
food_y = random.randrange(1, height - scale)
def show(self):
pygame.draw.rect(display, food_color, (food_x, food_y, scale, scale))
like I said before, if snake eats food, you will get +1 score. I will create showing score function.
score = 0
def show_score():
global score
# create font(font, font-size)
font = pygame.font.SysFont("Copperplate Gothic Bold", 30)
# render font(text, anti-aliasing enabled, color)
text = font.render("Score: " + str(score), True, (255, 255, 255))
# draw text surface(text, (pos.x, pos.y))
display.blit(text, (10, 10))
Learned New
I realized that I better separate classes and variables since the code file is quite complicated. Variables file contains
import pygame
# screen width, height
width = 500
height = 500
# for collision detection
scale = 10
# this should be global since snake class also need this variable
food_x = 10
food_y = 10
# colors
background = (23, 32, 42)
snake_head = (247, 220, 111)
snake_color = (236, 240, 241)
food_color = (148, 49, 38)
# to handle events, draw graphics, and manage the display within the window.
display = pygame.display.set_mode((width, height))
and to use these variables(variables.py),
from variables import *