Having trouble getting code to allow the cars to go off screen then come back on the opposite side of the screen! for top to bottom and side to side!
-The code i was trying was

if self.position > rect.top
                self.position == rect.bottom

not sure if this is gonna work im new soo need some help thanks!!

-Heres the game code

# Initialization
import pygame, math, sys, random
from pygame.locals import *
screen = pygame.display.set_mode((1020,680))
clock = pygame.time.Clock()

class CarSprite(pygame.sprite.Sprite):
    MAX_FORWARD_SPEED = 10
    MAX_REVERSE_SPEED = 10
    ACCELERATION = 2
    TURN_SPEED = 5

    def __init__(self, image, position):
        pygame.sprite.Sprite.__init__(self)
        self.src_image = pygame.image.load(image)
        self.position = position
        self.speed = self.direction = 0
        self.k_left = self.k_right = self.k_down = self.k_up = 0

    def update(self,  deltat):
        # SIMULATION
        self.speed += (self.k_up + self.k_down)
        if self.speed > self.MAX_FORWARD_SPEED:
            self.speed = self.MAX_FORWARD_SPEED
        if self.speed < -self.MAX_REVERSE_SPEED:
            self.speed = -self.MAX_REVERSE_SPEED
        self.direction += (self.k_right + self.k_left)
        x, y = self.position
        rad = self.direction * math.pi / 180
        x += -self.speed*math.sin(rad)
        y += -self.speed*math.cos(rad)
        self.position = (x, y)
        self.image = pygame.transform.rotate(self.src_image, self.direction)
        self.rect = self.image.get_rect()
        self.rect.center = self.position

class PadSprite(pygame.sprite.Sprite):
    normal = pygame.image.load('bumper.png')
    hit = pygame.image.load('bumperHit.png')
    def __init__(self, number, position):
        pygame.sprite.Sprite.__init__(self)
        self.number = number
        self.rect = pygame.Rect(self.normal.get_rect())
        self.rect.center = position
        self.image = self.normal
    
# CREATE A CAR AND RUN

rect = screen.get_rect()
background = pygame.image.load('ground.png')
screen.blit(background, (0,0))
car = CarSprite('car.png', rect.center)
car_group = pygame.sprite.RenderPlain(car)
car2 = CarSprite('car2.png', rect.center)
car2_group = pygame.sprite.RenderPlain(car2)
pads = [
    PadSprite(1, (random.randint(5,999), random.randint(10,675))),#add numbers back in
    PadSprite(2, (random.randint(5,999), random.randint(10,675))),
    PadSprite(3, (random.randint(5,999), random.randint(10,675))),
]
pads2 = [
    PadSprite(1, (random.randint(5,999), random.randint(10,675))),
    PadSprite(2, (random.randint(5,999), random.randint(10,675))),
    PadSprite(3, (random.randint(5,999), random.randint(10,675))),
]
pads3 = [
    PadSprite(1, (random.randint(5,999), random.randint(10,675))),
    PadSprite(2, (random.randint(5,999), random.randint(10,675))),
    PadSprite(3, (random.randint(5,999), random.randint(10,675))),
]
current_pad_number = 0
current_pad2_number = 0
current_pad3_number = 0
pad_group = pygame.sprite.RenderPlain(*pads)
pad2_group = pygame.sprite.RenderPlain(*pads2)
pad3_group = pygame.sprite.RenderPlain(*pads3)
screen.blit(background, (0,0))
screen.blit(background, (0,0))
screen.blit(background, (0,0))


while 1:
    # USER INPUT
    deltat = clock.tick(30)
    for event in pygame.event.get():
        if not hasattr(event, 'key'): continue
        down = event.type == KEYDOWN
        if event.key == K_RIGHT: car.k_right = down * -5
        elif event.key == K_LEFT: car.k_left = down * 5
        elif event.key == K_UP: car.k_up = down * 2
        elif event.key == K_DOWN: car.k_down = down * -2
        elif event.key == K_SPACE: car.position = (550,300)
        elif event.key == K_d: car2.k_right = down * -5
        elif event.key == K_a: car2.k_left = down * 5
        elif event.key == K_w: car2.k_up = down *2
        elif event.key == K_s: car2.k_down = down * -2
        elif event.key == K_b: car2.position = (550,300)
        elif event.key == K_ESCAPE: sys.exit(0)

    # RENDERING
    #screen.fill((0,0,0))
    pad_group.clear(screen, background)
    pad2_group.clear(screen, background)
    pad3_group.clear(screen, background)
    car_group.clear(screen,background)
    car2_group.clear(screen,background)
    car2_group.update(deltat)
    car_group.update(deltat)
    collisions = pygame.sprite.spritecollide(car, pad_group, False)
    if collisions:
        pad = collisions[0]
        if pad.number == current_pad_number + 1:
            pad.image = pad.hit
            current_pad_number += 1
    elif current_pad_number ==3:
        for pad in pad_group: pad.image = pad.normal
        current_pad_number = 0
    collisions2 = pygame.sprite.spritecollide(car2, pad2_group, False)
    if collisions2:
        pad2 = collisions2[0]
        if pad2.number == current_pad2_number + 1:
            pad2.image = pad2.hit
            current_pad2_number += 1
    elif current_pad2_number ==3:
        for pad2 in pad2_group: pad2.image = pad.normal
        current_pad2_number = 0
    #collisions3 = pygame.sprite.spritecollide(car_group, pad3_group, False)
    #if collisions3:
     #   pad3 = collisions3[0]
      #  if pad2.number == current_pad2_number + 1:
       #     pad2.image = pad2.hit
        #    current_pad2_number += 1
    #elif current_pad2_number ==3:
     #   for pad2 in pad2_group: pad2.image = pad.normal
      #  current_pad2_number = 0
    
    pad_group.draw(screen)
    pad2_group.draw(screen)
    pad3_group.draw(screen)
    car_group.draw(screen)
    car2_group.draw(screen)
    pygame.display.flip()

For moving something you have to do something like

self.rect = self.rect.move(self.whereyouwantittogo)

As you can see that is just an example, its not using any of the proper variables that are seen in your program. But im sure you get the gist :)

try:

if car_pos.x >= the_screens_length:
    car_pos.x = 0

I just need the "PacMan" effect like if you go off the screen it comes back on the opposite side

i tried both codes but nothing is working still

Member Avatar for masterofpuppets

hi
I don't know if this will help you but I did something similar to what you want for a Snake game and I used something like the following piece of code:

from Tkinter import *
import time

root = Tk()
c = Canvas( root, width = 400, height = 200 ); c.pack()

x, y = 150, 100
xInc = 5; yInc = 0
c.create_text( 200, 80, text = "control the ball using arrow keys", font = "Arial" )

def keyboard( event ):
    global x, y, xInc, yInc
    key = event.keysym
    if key == "Right":
        xInc = 5; yInc = 0
    if key == "Left":
        xInc = -5; yInc = 0
    if key == "Up":
        yInc = -5; xInc = 0
    if key == "Down":
        yInc = 5; xInc = 0

def main():
    global x, y, xInc, yInc
    ball = c.create_oval( x, y, x + 10, y + 10, fill = 'black' )

    while True:
        time.sleep( 0.05 )
        x += xInc; y += yInc
        c.delete( ball )
        ball = c.create_oval( x, y, x + 10, y + 10, fill = 'black' )
        if x >= 400:
            x = 0
        elif x <= 0:
            x = 400
        if y <= 0:
            y = 200
        elif y >= 200:
            y = 0
        c.update()

root.bind( "<Any-KeyPress>", keyboard )       
main()
mainloop()

P.S. I'm not using pygame, so don't know the drawing stuff there at all :) but I guess it'll be something similar. Hope this helps :)

still havent got it working

Got it working
put

if x >= 1020:
            x = 0
        elif x <= 0:
            x = 1020
        if y >= 680:
            y = 0
        elif y <= 0:
            y = 680

in the car update method

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.