I have basic knowledge about python. I am looking for help with this code: how is the snake moving? How does it length increases on colliding with the object? And, how does it detect when it touches itself or when it collides with the wall?

It also has semicolon in it which, when I remove it and add proper indentation, the game works but the object keeps on moving.

import pygame, random, sys
from pygame.locals import *
def collide(x1, x2, y1, y2, w1, w2, h1, h2):
    if x1+w1>x2 and x1<x2+w2 and y1+h1>y2 and y1<y2+h2:return True
    else:return False
def die(screen, score):
    f=pygame.font.SysFont('Arial', 30);t=f.render('Your score was: '+str(score), True, (0, 0, 0));screen.blit(t, (10, 270));pygame.display.update();pygame.time.wait(2000);sys.exit(0)
xs = [290, 290, 290, 290, 290];ys = [290, 270, 250, 230, 210];dirs = 0;score = 0;applepos = (random.randint(0, 590), random.randint(0, 590));pygame.init();s=pygame.display.set_mode((600, 600));pygame.display.set_caption('Snake');appleimage = pygame.Surface((10, 10));appleimage.fill((0, 255, 0));img = pygame.Surface((20, 20));img.fill((255, 0, 0));f = pygame.font.SysFont('Arial', 20);clock = pygame.time.Clock()
while True:
    clock.tick(10)
    for e in pygame.event.get():
        if e.type == QUIT:
            sys.exit(0)
        elif e.type == KEYDOWN:
            if e.key == K_UP and dirs != 0:dirs = 2
            elif e.key == K_DOWN and dirs != 2:dirs = 0
            elif e.key == K_LEFT and dirs != 1:dirs = 3
            elif e.key == K_RIGHT and dirs != 3:dirs = 1
    i = len(xs)-1
    while i >= 2:
        if collide(xs[0], xs[i], ys[0], ys[i], 20, 20, 20, 20):die(s, score)
        i-= 1
    if collide(xs[0], applepos[0], ys[0], applepos[1], 20, 10, 20, 10):score+=1;xs.append(700);ys.append(700);applepos=(random.randint(0,590),random.randint(0,590))
    if xs[0] < 0 or xs[0] > 580 or ys[0] < 0 or ys[0] > 580: die(s, score)
    i = len(xs)-1
    while i >= 1:
        xs[i] = xs[i-1];ys[i] = ys[i-1];i -= 1
    if dirs==0:ys[0] += 20
    elif dirs==1:xs[0] += 20
    elif dirs==2:ys[0] -= 20
    elif dirs==3:xs[0] -= 20    
    s.fill((255, 255, 255)) 
    for i in range(0, len(xs)):
        s.blit(img, (xs[i], ys[i]))
    s.blit(appleimage, applepos);t=f.render(str(score), True, (0, 0, 0));s.blit(t, (10, 10));pygame.display.update()

Recommended Answers

All 2 Replies

This bit of code makes the snake move with the arroy keys

elif e.type == KEYDOWN:
            if e.key == K_UP and dirs != 0:dirs = 2
            elif e.key == K_DOWN and dirs != 2:dirs = 0
            elif e.key == K_LEFT and dirs != 1:dirs = 3
            elif e.key == K_RIGHT and dirs != 3:dirs = 1

and this code makes the snake grow bigger, specifically the score+=1 statement (from the looks of it, whatever your score is, is what length you are)

 if collide(xs[0], applepos[0], ys[0], applepos[1], 20, 10, 20, 10):score+=1;xs.append(700);ys.append(700);applepos=(random.randint(0,590),random.randint(0,590))

This code is how you die, basically the "i" variable is set to 1, and if it is ever set to 2 then you die, the only way it can be set to 2 is if it runs into itself or a wall.

while i >= 2:
    if collide(xs[0], xs[i], ys[0], ys[i], 20, 20, 20, 20):die(s, score)

also look at collide() fof how/why this works

now about the semi colons, ( I really don't want to explain this wrong, so anyone feel free to edit if they have a better explanation) in Python they are used to basically separate commands, but still have them in the same line (it's a space saver, to make your code cleaner), like in the english language a comma is used to separate items in a list(ie. "Coding in Python, MatLab, and Java is fun. As opposed to Coding in Python is fun. Coding in MatLab is fun. Coding in Java is fun.) The semicolon is used to do the same but for code, like here

 s.blit(appleimage, applepos);t=f.render(str(score), True, (0, 0, 0));s.blit(t, (10, 10));pygame.display.update()

this one line calls the s.blit(appleimage, applepos) function, then it sets t to f.render(str(score), True, (0, 0, 0)), then calls s.blit(t, (10, 10)), and then it updates the display to show the end scores.

I think whenever you changed the code to have "proper" indenting and what not, it denied the program's intentions of having all the commands on the same line be executed at the same time.

commented: Good stuff. +1

I had the same problem as you when I eliminated the semicolons--The object kept moving wildly around.

Turns out that one must be judicious in removing the semicolons on line 23, as the result may not--mine didn't--indent properly.

Once I preserved the proper indentation, all was well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.