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