Question:
I asked Ghostwriter to make a snake game, but it fails to load an output. (I made it a Team Repl, not a personal one, so that’s why the link is under @giraffegang, not @AMDryzen5600X) (Also this is on an open network, nothing blocked)
Repl link:
https://replit.com/@giraffegang/Snake
import pygame
import sys
import random
import time
check_errors = pygame.init()
if check_errors[1] > 0:
print("(!) Had {0} initializing errors, exiting...".format(check_errors[1]))
sys.exit(-1)
else:
print("(+) PyGame successfully initialized!")
# Play surface
playSurface = pygame.display.set_mode((720, 460))
pygame.display.set_caption('Snake game!')
# Colors
red = pygame.Color(255, 0, 0) # gameover
green = pygame.Color(0, 255, 0) # snake
black = pygame.Color(0, 0, 0) # score
white = pygame.Color(255, 255, 255) # background
brown = pygame.Color(165, 42, 42) # food
# FPS controller
fpsController = pygame.time.Clock()
# Important variables
snakePos = [100, 50]
snakeBody = [[100, 50], [90, 50], [80, 50]]
foodPos = [random.randrange(1, 72) * 10, random.randrange(1, 46) * 10]
foodSpawn = True
direction = 'RIGHT'
changeto = direction
score = 0
# Game over function
def gameOver():
myFont = pygame.font.SysFont('monaco', 72)
GOsurf = myFont.render('Game over!', True, red)
GOrect = GOsurf.get_rect()
GOrect.midtop = (360, 15)
playSurface.blit(GOsurf, GOrect)
showScore(0)
pygame.display.flip()
time.sleep(4)
pygame.quit() # pygame exit
sys.exit() # console exit
def showScore(choice=1):
sFont = pygame.font.SysFont('monaco', 24)
Ssurf = sFont.render('Score : {0}'.format(score), True, black)
Srect = Ssurf.get_rect()
if choice == 1:
Srect.midtop = (80, 10)
else:
Srect.midtop = (360, 120)
playSurface.blit(Ssurf, Srect)
# Main Logic of the game
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT or event.key == ord('d'):
changeto = 'RIGHT'
if event.key == pygame.K_LEFT or event.key == ord('a'):
changeto = 'LEFT'
if event.key == pygame.K_UP or event.key == ord('w'):
changeto = 'UP'
if event.key == pygame.K_DOWN or event.key == ord('s'):
changeto = 'DOWN'