Jan-18-2020, 05:15 AM
(This post was last modified: Jan-18-2020, 05:15 AM by ElevenDecember.)
I"m currently working on a small project for a computer class, in which we need to make a basic game. I'm using Thonny as well as the PyGame module.
The problem I'm facing is that the program starts fine, however when I click the "Play" button on the start menu (not in editor), instead of entering into the main game loop, the program closes instead.
Feedback would be appreciated, as even the teacher had no solution.
Thank you!
Code:
#-------------------------
#Snake Game Final Project
#Steven Bailey
#January 8th 2020
#-------------------------
The problem I'm facing is that the program starts fine, however when I click the "Play" button on the start menu (not in editor), instead of entering into the main game loop, the program closes instead.
Feedback would be appreciated, as even the teacher had no solution.
Thank you!
Code:
#-------------------------
#Snake Game Final Project
#Steven Bailey
#January 8th 2020
#-------------------------
#-------------------------
#Snake Game Final Project
#Steven Bailey
#January 8th 2020
#-------------------------
import pygame
import time
import random
#game initilization
pygame.init()
pygame.font.init()
clock = pygame.time.Clock()
def text_objects(text, font,):
textSurface = font.render(text, True, white)
return textSurface, textSurface.get_rect()
# Screen size
size = width, height = 500,500
win = pygame.display.set_mode((size))
pygame.display.set_caption("Snake by Steven Bailey")
#how to make the colours I want to use
red = pygame.Color(255, 0, 0)
bright_green = pygame.Color(0, 255, 0)
white = pygame.Color(255, 255, 255)
black = pygame.Color(0, 0, 0)
green = pygame.Color(0, 200, 0)
brown = pygame.Color(165, 42, 42)
#how to make a start menu
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
win.fill(black)
largeText = pygame.font.Font('freesansbold.ttf',115)
textSurf, textRect = text_objects("Snake", largeText)
win.blit(textSurf, textRect)
button("Start",150,250,100,50,green,bright_green,"play")
pygame.display.update()
clock.tick(15)
#how to make a start button
def button(msg, x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
print(click)
if x+w > mouse[0] > x and x and y+h > mouse[1] > y:
pygame.draw.rect(win, ac, (x,y,w,h))
if click[0] == 1 and action != None:
if action == "play":
game_loop()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(win, ac, (x,y,w,h))
else:
pygame.draw.rect(win, ic, (x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x + (w/2)), y+(h/2) )
win.blit(textSurf, textRect)
#game over font and gameover function
def game_over():
gameoverText = pygame.font.Font('freesansbold.ttf',75)
textSurf, textRect = text_objects("GAME OVER", gameoverText,)
win.blit(textSurf, textRect)
time.sleep(4)
pygame.quit()
exit()
#how to show the score
def show_score(choice=1):
pygame.font.init()
SFont = pygame.font.SysFont('freesansbold.ttf', 32)
Ssurf = SFont.render("Score : {0}".format(score), True, black)
Srect = Ssurf.get_rect()
if choice == 1:
Srect.midtop = (80, 10)
else:
Srect.midtop = (320, 100)
win.blit(Ssurf, Srect)
# FPS controller, controls the frames per second of the game
fpsController = pygame.time.Clock()
# Game settings
delta = 10
snakePos = [100, 50]
snakeBody = [[100, 50], [90, 50], [80, 50]]
foodPos = [400, 50]
foodSpawn = True
change_to = ''
direction = 'RIGHT'
score = 0
def game_loop():
win.fill(black)
while True:
pygame.init()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.KEYDOWN:
changeto = ''
if event.key == pygame.K_RIGHT or event.key == pygame.K_d:
change_to = 'RIGHT'
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
change_to = 'LEFT'
if event.key == pygame.K_UP or event.key == pygame.K_w:
change_to = 'UP'
if event.key == pygame.K_DOWN or event.key == pygame.K_s:
change_to = 'DOWN'
if event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT))
# Validate direction
change_to = ''
direction = 'RIGHT'
if change_to == 'RIGHT' and direction != 'LEFT':
direction = changeto
if change_to == 'LEFT' and direction != 'RIGHT':
direction = changeto
if change_to == 'UP' and direction != 'DOWN':
direction = changeto
if change_to == 'DOWN' and direction != 'UP':
direction = change_to
# Update snake position
delta = 10
snakePos = [100, 50]
if direction == 'RIGHT':
snakePos[0] += delta
if direction == 'LEFT':
snakePos[0] -= delta
if direction == 'DOWN':
snakePos[1] += delta
if direction == 'UP':
snakePos[1] -= delta
# Snake body mechanism
snakeBody = [[100, 50], [90, 50], [80, 50]]
snakePos = [100, 50]
foodSpawn = True
foodPos = [400, 50]
snakeBody.insert(0, list(snakePos))
if snakePos == foodPos:
foodSpawn = False
score += 1
else:
snakeBody.pop()
if foodSpawn == False:
foodPos = [random.randrange(1, width // 10) * delta, random.randrange(1, height // 10) * delta]
foodSpawn = True
for pos in snakeBody:
pygame.draw.rect(win, green, pygame.Rect(pos[0], pos[1], delta, delta))
pygame.draw.rect(win, brown, pygame.Rect(foodPos[0], foodPos[1], delta, delta))
show_score
# Bounds
if snakePos[0] >= width or snakePos[0] < 0:
game_over()
if snakePos[1] >= height or snakePos[1] < 0:
game_over()
# Self hit
for block in snakeBody[1:]:
if snakePos == block:
game_over()
fpsController.tick(20)
game_intro()
game_loop()
