Traceback (most recent call last):
File "c:\Users\Name\Google Drive\Downloads\Jasosc\python.py", line 254, in <module>
show_score(1, white, 'Times New Roman', 20)
File "c:\Users\Name\Google Drive\Downloads\Jasosc\python.py", line 137, in show_score
score_font = pygame.font.SysFont(font, size)
File "C:\Users\Name\AppData\Local\Programs\Python\Python310\lib\site-packages\pygame\sysfont.py", line 474, in SysFont
return constructor(fontname, size, set_bold, set_italic)
File "C:\Users\Name\AppData\Local\Programs\Python\Python310\lib\site-packages\pygame\sysfont.py", line 391, in font_constructor
font = Font(fontpath, size)
pygame.error: font not initialized
We are going to need more information
complete guess based on the last line “pygame.error: font not initialized”
maybe you are missing:
pygame.init()
?
I have pygame.init ()
code
import time
import random
import pygame
import keyboard
ask_for_speed = int(input("Choose a speed (program will crash if you do not put in a number or if you put 0 or a nehgitive it will also crash): "))
snake_speed = ask_for_speed
print("Game running in sepreate window...")
print('''
CONTROALS
ARROW KEYS THATS IT
HOW TO PLAY
- THE GREEN THING IS THE SNAKE
- THE RED THING IS THE FOOD
- YOUR SCORE IS ON THE TOP LEFT CORNER OF THE SCREEN
- GAME ENDS WHEN YOU BUMP INTO THE WALL OR YOURSELF
- THE SIZE OF THE WINDOW INCREASES WITH THE SPEED
- YOU HAVE TO CLICK ON THE SCREEN OF THE SNAKE GAME BEFORE YOU CAN PRESS ANY KEYS
''')
time.sleep(5)
# Window size
if snake_speed <= 15:
window_x = 700
window_y = 600
elif snake_speed >= 16:
window_x = 900
window_y = 800
elif snake_speed >= 30:
window_x = 1500
window_y = 1400
else:
window_x = 2000
window_y = 2100
# defining colors
black = pygame.Color(0, 0, 0)
white = pygame.Color(255, 255, 255)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
blue = pygame.Color(0, 0, 255)
# Initialising pygame
pygame.init()
# Initialise game window
pygame.display.set_caption('JasoPC Snake Game')
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('Arial', 30)
# creating a text surface on which text
# will be drawn
game_over_surface = my_font.render(
'GAME OVER ): Your Score is : ' + str(score), 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 3 seconds we will quit the program
time.sleep(3)
# deactivating pygame library
pygame.quit()
# quit the program
pygame.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 += 1
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, green,
pygame.Rect(pos[0], pos[1], 10, 10))
pygame.draw.rect(game_window, red, 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, white, 'Times New Roman', 20)
# Refresh game screen
pygame.display.update()
# Frame Per Second /Refresh Rate
fps.tick(snake_speed)
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.