Snake Game keep adding food and removing
Question:
i created a snake game and i have one problem in function food() it keep adding food on screen and removing it i don’t know how to fix this i tried with food_statement like = "wait" when there’s a food in screen and draw when it’s not food can you help me code is working properly until hit food function?
import pygame
import time
import random
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('Snake Game by Joelinton')
blue=(0,0,255)
x_change = 0.2
y_change = 0.2
x = 400
y = 250
def creatingsnake():
pygame.draw.rect(screen,blue,[x,y,20,20])
def gameover():
font = pygame.font.SysFont('freesansbold.ttf', 100)
text = font.render('Game Over', True,(255,255,255))
screen.blit(text, (250, 250))
def food():
foodx = random.randint(0,750)
foody = random.randint(0,550)
pygame.draw.rect(screen,blue,[foodx,foody,20,20])
running = True
while running:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= x_change
if keys[pygame.K_RIGHT]:
x += x_change
if keys[pygame.K_UP]:
y -= y_change
if keys[pygame.K_DOWN]:
y += y_change
if x < 0 or x > 780 or y < 0 or y > 580:
gameover()
running = False
time.sleep(1)
food()
creatingsnake()
pygame.display.update()
Answers:
food
is called in each frame. Thus, when the coordinates are generated in the function ‘food’, new random coordinates are generated in each frame. You must set the coordinates of the food once before the application loop:
foodx = random.randint(0,750)
foody = random.randint(0,550)
def food():
pygame.draw.rect(screen,blue,[foodx,foody,20,20])
running = True
while running:
# [...]
food()
creatingsnake()
pygame.display.update()
i created a snake game and i have one problem in function food() it keep adding food on screen and removing it i don’t know how to fix this i tried with food_statement like = "wait" when there’s a food in screen and draw when it’s not food can you help me code is working properly until hit food function?
import pygame
import time
import random
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('Snake Game by Joelinton')
blue=(0,0,255)
x_change = 0.2
y_change = 0.2
x = 400
y = 250
def creatingsnake():
pygame.draw.rect(screen,blue,[x,y,20,20])
def gameover():
font = pygame.font.SysFont('freesansbold.ttf', 100)
text = font.render('Game Over', True,(255,255,255))
screen.blit(text, (250, 250))
def food():
foodx = random.randint(0,750)
foody = random.randint(0,550)
pygame.draw.rect(screen,blue,[foodx,foody,20,20])
running = True
while running:
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= x_change
if keys[pygame.K_RIGHT]:
x += x_change
if keys[pygame.K_UP]:
y -= y_change
if keys[pygame.K_DOWN]:
y += y_change
if x < 0 or x > 780 or y < 0 or y > 580:
gameover()
running = False
time.sleep(1)
food()
creatingsnake()
pygame.display.update()
food
is called in each frame. Thus, when the coordinates are generated in the function ‘food’, new random coordinates are generated in each frame. You must set the coordinates of the food once before the application loop:
foodx = random.randint(0,750)
foody = random.randint(0,550)
def food():
pygame.draw.rect(screen,blue,[foodx,foody,20,20])
running = True
while running:
# [...]
food()
creatingsnake()
pygame.display.update()