Jan 15, 2019 08:09 AM
(This post was last modified: Jan 15, 2019 08:32 AM by CrazyLinuxHacker.)
Hello everyone. I'm making the snake game with python but i have 2 major problems:
Firstly, snake length does not change even i eat the apple(actually i have to pass over the apple many times to increase length) and secondly, snake crosses the boundaries without game over.(I've programmed the boundaries). Also how can i make sprite borders? Thank you.
ps: I apologise for my english. I am greek. 🤗
Firstly, snake length does not change even i eat the apple(actually i have to pass over the apple many times to increase length) and secondly, snake crosses the boundaries without game over.(I've programmed the boundaries). Also how can i make sprite borders? Thank you.
Code
#libraries
import pygame
import random
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Snake(alpha edition)")
block_size = 10
FPS = 30
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 25)
def snake(block_size, snakelist):
for xny in snakelist:
pygame.draw.rect(gameDisplay, green, [xny[0], xny[1], block_size, block_size])
def gameover_msg(msg,color):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [display_width/2, display_height/2])
def gameLoop():
gameExit = False
gameOver = False
pos_x = display_width/2
pos_y = display_height/2
pos_x_change = 0
pos_y_change = 0
randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
snakelist = []
snakelength = 1
while not gameExit:
while gameOver == True:
gameDisplay.fill(white)
gameover_msg("Game over, press R to play again or Q to quit", red)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameExit = True
gameOver = False
if event.key == pygame.K_r:
gameLoop()
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
pos_x_change = -block_size
pos_y_change = 0
elif event.key == pygame.K_RIGHT:
pos_x_change = block_size
pos_y_change = 0
elif event.key == pygame.K_UP:
pos_y_change = -block_size
pos_x_change = 0
elif event.key == pygame.K_DOWN:
pos_y_change = block_size
pos_x_change = 0
if pos_x >= display_width or pos_x < 0 or pos_y >= display_height or pos_y < 0:
gameOver = True
if pos_x == randAppleX and pos_y == randAppleY:
randAppleX = round(random.randrange(0, display_width-block_size)/10.0)*10.0
randAppleY = round(random.randrange(0, display_height-block_size)/10.0)*10.0
snakelength += 1
pos_x += pos_x_change
pos_y += pos_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, red, [randAppleX, randAppleY, block_size, block_size])
snakehead = []
snakehead.append(pos_x)
snakehead.append(pos_y)
snakelist.append(snakehead)
if len(snakelist) > snakelength:
del snakelist[0]
if snakehead in snakelist[:-1]:
if not snakehead == snakelist[0]:
continue
gameOver = True
snake(block_size, snakelist)
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
gameLoop()
