GUVI
Back

Fixed a bug of snake game have a look ->

Created 3 years ago
55 Views
2 Comments
Teknathkrishnajha
@Teknathkrishnajha
Teknathkrishnajha
@TeknathkrishnajhaProfile is locked. Login

in apple block if snake could reach to left most x-axis or top most y-axis then it didn't ate apple but it should ate i fixed this bug

also modified gui hope it gives you a very strong and clean experience

# MOdified by Teknath krishna jha

# Basic window setup

# part 1

import pygame

import time

import random

# initializing pygame

pygame.init()

# colors

black = (0,0,0)

white = (255,255,255)

red = (255,0,0)

green = (0,255,0)

blue = (0,0,255)

# window surface size

window_width = 800

window_height = 600

gameDisplay = pygame.display.setmode((windowwidth , window_height))

# window name

pygame.display.set_caption('$n@ke g@me +ekn@+h Jh@')

# text font

font = pygame.font.SysFont(None , 25 , bold=True)

# on quit

def myquit():

pygame.quit()

sys.exit()

clock = pygame.time.Clock()

FPS = 5

blockSize = 20

noPixel = 0

'''

sizeGrd = window_width // blockSize

row = 0

col = 0

for nextline in range(sizeGrd):

'''

# part 2

# function to each block of draw snake

def snake(blockSize, snakelist):

#x = 250 - (segmentwidth + segmentmargin) * i

i=1

for size in snakelist:

if i==1:

i=0

pygame.draw.rect(gameDisplay, blue ,[size[0]+5,size[1],blockSize,blockSize],100)

else:

i=1

pygame.draw.rect(gameDisplay, black ,[size[0]+5,size[1],blockSize,blockSize])

def messagetoscreen(msg, color):

# render bold text background is cyan

screen_text = font.render(msg, True, 'red' ,'cyan')

# position of text on surface

gameDisplay.blit(screentext, [(windowwidth/2)-150, (window_height/2) -50])

# part 3

def gameLoop():

# game status

gameExit = False

gameOver = False

leadx = windowwidth/2

leady = windowheight/2

changepixelsof_x = 0

changepixelsof_y = 0

# Initial snake dimension

snakelist = []

snakeLength = 1

# food position

# round for round off values for integer

randomAppleX = round(random.randrange(0, window_width-blockSize)/10.0)*10.0

randomAppleY = round(random.randrange(0, window_height-blockSize)/10.0)*10.0

while not gameExit:

# Execution of below while loop when game will over

while gameOver == True:

gameDisplay.fill(white)

messagetoscreen("Game over, press c to play again or Q to quit", red)

pygame.display.update()

for event in pygame.event.get():

if event.type == pygame.QUIT:

gameOver = False

gameExit = True

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_q:

gameExit = True

gameOver = False

if event.key == pygame.K_c:

gameLoop()

for event in pygame.event.get():

if event.type == pygame.QUIT:

gameExit = True

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_ESCAPE:

myquit()

leftArrow = event.key == pygame.K_LEFT

rightArrow = event.key == pygame.K_RIGHT

upArrow = event.key == pygame.K_UP

downArrow = event.key == pygame.K_DOWN

# Androids

# leftArrow = event.key == pygame.K_4

# rightArrow = event.key ==pygame.K_6

# upArrow = event.key == pygame.K_2

# downArrow = event.key == pygame.K_8

#Reduce and exceed from opposite ends as per instructions as left ,right, up , down

if leftArrow:

changepixelsof_x = -blockSize

changepixelsof_y = noPixel

elif rightArrow:

changepixelsof_x = blockSize

changepixelsof_y = noPixel

elif upArrow:

changepixelsof_y = -blockSize

changepixelsof_x = noPixel

elif downArrow:

changepixelsof_y = blockSize

changepixelsof_x = noPixel

if leadx >= windowwidth or leadx < 0 or leady >= windowheight or leady < 0:

gameOver = True

leadx += changepixelsofx

leady += changepixelsofy

gameDisplay.fill('cyan')

AppleThickness = 20

# About food position and size in terminal in back-end

print([int(randomAppleX),int(randomAppleY),AppleThickness,AppleThickness])

pygame.draw.rect(gameDisplay, red, [randomAppleX,randomAppleY,AppleThickness,AppleThickness])

allspriteslist = []

allspriteslist.append(lead_x)

allspriteslist.append(lead_y)

snakelist.append(allspriteslist)

if len(snakelist) > snakeLength:

del snakelist[0]

for eachSegment in snakelist [:-1]:

if eachSegment == allspriteslist:

gameOver = True

snake(blockSize, snakelist)

pygame.display.update()

# Comparision of food and snake mouth position

if leadx >= (randomAppleX - AppleThickness) and leadx <= (randomAppleX + AppleThickness-2):

if leady >= (randomAppleY

- AppleThickness) and leady <= (randomAppleY + AppleThickness-2):

randomAppleX = round(random.randrange(0, window_width-blockSize)/10.0)*10.0

randomAppleY = round(random.randrange(0, window_height-blockSize)/10.0)*10.0

snakeLength += 1

# Frames per second here it is 5fps

clock.tick(FPS)

pygame.quit()

quit()

# Calling

gameLoop()

Comments (2)
Please login to comment.
 
Powered by habitate.io Habitate