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()