Lapā tiek izmantotas sīkdatnes 

Nepaklausīgā čūska


Problēmas apraksts

Zemāk dots kļūdains kods spēlei "Čūska".

Iepazīsties ar doto kodu un pacenties izlabot kļūdas, sekojot FIXME norādēm, kur aprakstītas problēmas.

Pēc kļūdu izlabošanas testē spēli, izmainot vērtības parametriem (mainīgajiem) koda sākumā.

import pygame
import random

pygame.init()
pygame.display.set_caption("MySnake")
screen = pygame.display.set_mode((300, 400))
surface = pygame.Surface((300, 400))
screen.fill("white")
blue = (0,0,255)
red = (255,0,0)
white = (255,255,255)

# TODO: test game with various lengths
lengthOfSnake = 3
direction = "right"
gameOver = False
snake = []
routeX = []
routeY = []
score = 0
# TODO: test game with various speed settings
speed = 9  # sets speed of snake 0 - 9
myText = ""

# makes initial snake body
for i in range(lengthOfSnake):
    snake.append(pygame.draw.rect(screen,(255-i*5, 255-i*5, 0),[20*lengthOfSnake-i*20,0,20,20]))

# generate food coordinates
def setFoodLocation():
    # FIXME: food sometimes appears outside of the window
    # FIXME: food sometimes overlays body of the snake
    # FIXME: probably some other strange things happen... sometimes...
    global foodX, foodY
    foodX = random.randint(0, 17)*20 # generates number 0 - 280
    foodY = random.randint(0, 19)*20 # generates number 0 - 380
setFoodLocation()

# makes food
def makeFood():
    pygame.draw.rect(screen, white, [foodX, foodY, 20, 20])

# expands snake
def expandSnake():
    # FIXME: snake expands by two blocks instead of one, shouldn't be like that
    snake.append(pygame.draw.rect(screen,(180,180,0),[routeX[len(snake) - 1],routeY[len(snake) - 1],20,20]))

# checks collision with walls
def hitWall():
    # FIXME: snake goes out of field without losing
    if snake[0].x < 0 or snake[0].x >= 300 or snake[0].y < 0 or snake[0].y >= 500:
        gameIsOver()

# checks collision with body
def hitBody():
    for i in range(1, len(snake)):
        if snake[0].x == snake[i].x and snake[0].y == snake[i].y:
            # FIXME: snake doesn't die when hitting itself
            ...

# checks collision with food
def hitFood():
    # FIXME: score goes negative. why..?
    if foodX == snake[0].x and foodY == snake[0].y:
        global score 
        score -= 1
        setFoodLocation()
        expandSnake()
        expandSnake()

# updates score
def updateText(myText):
    pygame.display.set_caption(myText)

# stops snake, quits game
def gameIsOver():
    global gameOver
    gameOver = True
    updateText(myText = "Game Is Over")
    pygame.time.delay(1000)

# ***main loop***
while gameOver == False:
    for event in pygame.event.get():

        # X button closes game
        if event.type==pygame.QUIT:
            gameOver=True

        # defines key actions
        # FIXME: 'up' arrow doesn't work
        # FIXME: snake loses weight when going down.. drastic diet? o.O
        if event.type==pygame.KEYDOWN:
            if event.key==pygame.K_UP and direction != "down":
                direction = "uP"
            elif event.key==pygame.K_DOWN and direction != "up":
                direction = "down"
                snake.pop()
            elif event.key==pygame.K_RIGHT and direction != "left":
                direction = "left"
            elif event.key==pygame.K_LEFT and direction != "right":
                direction = "right"

    # saves route in lists, lets snake to follow itself
    for i in range(len(snake)):
        routeX.insert(i, snake[i].x)
        routeY.insert(i, snake[i].y)

    # moves head according to commands
    # FIXME: background goes crazy, should be solid all the time
    screen.fill(random.choice(["green", "black", "blue"]))
    if direction == "up":
        snake[0] = pygame.draw.rect(screen,(255,255,0),[routeX[0],routeY[0]-20,20,20])

    # FIXME: 'down' direction has bug, snake collapses when going down
    elif direction == "down":
        snake[0] = pygame.draw.rect(screen,(255,255,0),[routeX[0],routeY[0]+30,20,20])
    # FIXME: snake dies when opposite direction activated
    elif direction == "right":
        snake[0] = pygame.draw.rect(screen,(255,255,0),[routeX[0]+20,routeY[0],20,20])
    elif direction == "left":
        snake[0] = pygame.draw.rect(screen,(255,255,0),[routeX[0]-20,routeY[0],20,20])

    # moves body
    for i in range(1, len(snake)):
        snake[i] = pygame.draw.rect(screen, (180, 180, 0),[routeX[i - 1],routeY[i - 1],20,20])

    hitBody()
    hitWall()

    # FIXME: snake doesn't catch food, something is missing here \_(o.O_/

    makeFood()
    updateText(myText = "score " + str(score))

    pygame.display.update()  # rerenders screen
    pygame.time.delay(1000-speed*100)  # movement delay

pygame.quit()