im working on a tron game with pygame and i cant figure out how to make my tail that the player's car makes disappear after somany pixels. i want the tail to start shortening itself after like 500 pixels. if you can, can you post the full code edited (im kinda a noob at pygame)

thanks

import sys, random, time, pygame
from pygame.locals import *

def print_text(font, x, y, text, color=(255,255,255)):
    imgText = font.render(text, True, color)
    screen.blit(imgText, (x,y))
    

#main program begins
pygame.init()
w = 500
h = 500
screen = pygame.display.set_mode((w,h))
pygame.display.set_caption("Tron")
font1 = pygame.font.Font(None, 24)
pygame.mouse.set_visible(False)
white = 255,255,255
red = 220, 50, 50
yellow = 230,230,50
black = 0,0,0



pos_x = 300
pos_y = 460
p1 = [[300],[460]]
poss = pos_y,pos_x
print p1
screen.fill(black)

vel_y = 0.7
path = 8 # 8=up 6=right 4=left 2=down
#repeating loop
while True:
    for event in pygame.event.get():
        #Keydown
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key == K_UP and path != 2:
                path = 8
            if event.key == K_RIGHT and path != 4:
                path = 6                
            if event.key == K_LEFT and path != 6:
                path = 4
            if event.key == K_DOWN and path != 8:
                path = 2
    
    if path == 8:
        pos_y -= 1
    elif path == 6:
        pos_x += 1
    elif path == 4:
        pos_x -= 1
    elif path == 2:
        pos_y += 1
    p1[0].append(pos_x)
    p1[1].append(pos_y)
    if pos_y <= 0 or pos_y >= h or pos_x <= 0 or pos_x >= w:
        pygame.quit()
        sys.exit()

    if len(p1[0]) == 10:
        
    
    pygame.time.delay(10) 
    pos = pos_x,pos_y,10,10
    #print pos
    
    pygame.draw.rect(screen, white, pos, 0)
    
    
    pygame.display.update()

What is if block after line 64, your code never will run.

Edited 4 Years Ago by pyTony: n/a

What is if block after line 64, your code never will run.

i put that there because i was trying to solve my problem and i forgot to delete it

Edited 4 Years Ago by natehome: n/a

I coded version of your code, but I do not want to give ready answer for home works.

OK, here it is:

import random, time, pygame
from pygame.locals import *

def print_text(font, x, y, text, color=(255,255,255)):
    imgText = font.render(text, True, color)
    screen.blit(imgText, (x,y))
    

pygame.init()
w = h = 500
screen = pygame.display.set_mode((w,h))
pygame.display.set_caption("Tron")
font1 = pygame.font.Font(None, 24)
pygame.mouse.set_visible(False)
white = 255, 255, 255
red = 220, 50, 50
yellow = 230, 230, 50
black = 0, 0, 0
screen.fill(black)

pos_x, pos_y = 300, 460
tron_path = []

#vel_y = 0.7 # not used

limit = 100
key = K_UP
change = {K_UP:(0,-1), K_RIGHT:(1,0), K_LEFT:(-1,0), K_DOWN:(0,1)}
opposite = {K_UP: K_DOWN, K_LEFT: K_RIGHT}
opposite.update((b,a) for a,b in opposite.items())

speed = 4
counter = 0
grow, growth_rate = 400, 10

while True:
    pygame.time.delay(speed)
    counter += 1
    # growing by time tail (snake game style)
    if counter > grow:
        counter = 0
        limit += growth_rate
        print 'limit', limit
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                raise SystemExit('Escaped')
            if event.key in change and key != opposite[event.key]:
                key = event.key
    offset_x, offset_y = change[key]
    pos_x += offset_x
    pos_y += offset_y           
    
    #print pos_x, pos_y
    tron_path.append((pos_x, pos_y))

    if len(tron_path) > limit:
        pygame.draw.rect(screen, black, tron_path.pop(0) + (10, 10), 0)

    if pos_y <= 0 or pos_y >= h or pos_x <= 0 or pos_x >= w:
        pygame.quit()
        raise SystemExit('Out of board exit')

    pos = pos_x, pos_y, 10, 10
    pygame.draw.rect(screen, white, pos, 0)
    pygame.display.update()
This article has been dead for over six months. Start a new discussion instead.