Skip to content
Advertisement

Image following line, or how to remove an image without refilling the screen

So, I’m trying my hand at remaking TRON, the snake like cycle arcade game. I am having an issue with my lighcycle images. So in TRON after the line is drawn it stays there, so when trying to place an image that needs to move in front of it, the image gets dragged along because i cant refill the screen otherwise the line made disappears.

My solution for this was to make the line almost as thick as the cycle so the line would cover the cycle image.

My issue is with the turning, it turns but then a cycle image gets left behind, and i tried delaying the bike waiting for the line to cover then turn, but then that would make the bike completely disappear and appear after turning. i was wondering if anyone could help me turn my cycle along with my line smoothly

here is my code

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

pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 400))
pygame.display.set_caption('TRON')
fpsClock = pygame.time.Clock()
FPS = 20

BLACK = (0, 0, 0)
GRID = (0, 1 ,40)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

WHITE = (0, 1, 70)

m = 3

X = 0
x = 10
y = 400/2
Y = 400/2
direction = 'right'
direction2 = 'right'


b= 0
player1B = 'noBoost'

cycle1 = pygame.image.load('./images/lightCycle1.png')
cycle2 = pygame.image.load('./images/lightCycle2.png')
cycle3 = pygame.image.load('./images/lightCycle3.png')
cycle4 = pygame.image.load('./images/lightCycle4.png')

counter = 0
counter2 = 0


def drawGrid():
   blockSize = 33
   for x in range(0, 400, blockSize):
       for y in range(0, 400, blockSize):
           rect = pygame.Rect(x, y, blockSize, blockSize)
           pygame.draw.rect(DISPLAYSURF, WHITE, rect, 1)
           
DISPLAYSURF.fill(GRID)
drawGrid()
while True:
   pygame.draw.rect(DISPLAYSURF, BLUE, (X, Y, 3, 3))


   if direction == 'right':
           X += m
   if direction2 == 'right':
       counter2 += 1
       if counter2 == 10:
           x = X
           counter2 -= 1
           previousDirection = direction                               
           cycle = pygame.transform.scale(cycle1, (16, 10))
           DISPLAYSURF.blit(cycle, (x, y-4))


               
   if direction == 'left':
           X -= m
   if direction2 == 'left':
       counter2 += 1
       if counter2 == 5:
           x = X
           counter2 -= 1
           previousDirection = direction
           cycle = pygame.transform.scale(cycle4, (16, 10))
           DISPLAYSURF.blit(cycle, (x-13, y-4))

                   

   if direction == 'down':
           Y += m
   if direction2 == 'down':
       counter2 += 1
       if counter2 == 5:
           y = Y
           counter2 -= 1
           previousDirection = direction
           cycle = pygame.transform.scale(cycle3, (10, 16))
           DISPLAYSURF.blit(cycle, (x-4, y))


               
   if direction == 'up':
           Y -= m      
   if direction2 == 'up':
       counter2 += 1
       if counter2 == 5:
           previousDirection = direction
           counter2 -= 1
           y = Y
           cycle = pygame.transform.scale(cycle2, (10, 16))
           DISPLAYSURF.blit(cycle, (x-4, y-13))



           
   if player1B == 'boost':
       b += 1
       m = 9
  

   for event in pygame.event.get():
       if event.type == QUIT:
           pygame.quit()
           sys.exit()
       elif event.type == KEYDOWN:
           if (event.key == K_LEFT ):
               direction = 'left'
               direction2 = 'left'
               counter = 0
               counter2 = 0
           elif (event.key == K_RIGHT ):
               direction = 'right'
               direction2 = 'right'
               counter = 0
               counter2 = 0
           elif (event.key == K_UP ):
               direction = 'up'
               direction2 = 'up'
               counter = 0
               counter2 = 0
           elif (event.key == K_DOWN ):
               direction2 = 'down'
               counter = 0
               counter2 = 0
               time.sleep(1)
               direction = 'down'
               
               
           elif event.key == pygame.K_RSHIFT:
               player1 = 'boost'
       elif event.type == KEYUP:
           if event.key == pygame.K_RSHIFT:
               player1B = 'noBoost'
               m = 3

   pygame.display.update()
   fpsClock.tick(FPS)

Advertisement

Answer

Here is a simple example of how that might work:

import pygame


pygame.init()
screen = pygame.display.set_mode((900, 600))

clock = pygame.time.Clock()

player_x = 100
player_y = 100

line_coords = []

run = True
while run:
    clock.tick(60)

    screen.fill((0, 0, 0))
    pygame.draw.rect(screen, (255, 0, 0), (player_x - 10, player_y - 10, 20, 20))

    keys = pygame.key.get_pressed()

    if keys[pygame.K_UP]:
        player_y -= 5
    elif keys[pygame.K_DOWN]:
        player_y += 5
    elif keys[pygame.K_LEFT]:
        player_x -= 5
    elif keys[pygame.K_RIGHT]:
        player_x += 5

    line_coords.append((player_x, player_y))
    if len(line_coords) > 1:
        pygame.draw.lines(screen, (255, 255, 255), False, line_coords, width=3)

    pygame.display.update()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

pygame.quit()

Obviously some modifications should be done for example not appending to the list while no keys are pressed (otherwise standing in one place will add a bunch of coords) or for example figuring out the coords of corners so that the rest can be deleted and only the corners left and probably other things too but this is the idea

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement