If I have a sprite (See below) that I can move around, the screen with the arrow keys. (Up and Down moves forwards and backwards, left and right turn.)
I was wondering if it would be possible to move to the other side of the screen when it goes off? But so it works whatever the angle is, and so that if it's driven half into the edge, half appears on one side. (Kind of like snake) Is there a way of doing this?
Here's my code so far:
import sys, pygame, math
from pygame.locals import *
pygame.init()
SCREEN = pygame.display.set_mode((800, 600))
car = pygame.transform.scale(pygame.image.load('Car.png').convert_alpha(), (64, 64))
pygame.display.set_caption('Car Game')
pygame.display.set_icon(car)
FPS = pygame.time.Clock()
carX = 400
carY = 100
angle = 90
speed = 0
while True:
if angle == 360: angle = 0
if angle == -1: angle = 359
SCREEN.fill((0,0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_a] or keys[K_LEFT]:
angle += speed
elif keys[K_d] or keys[K_RIGHT]:
angle -= speed
if keys[K_w] or keys[K_UP]:
speed += 1
elif keys[K_s] or keys[K_DOWN]:
speed -= 0.5
carX += speed*math.cos(math.radians(angle))
carY -= speed*math.sin(math.radians(angle))
speed *= 0.95
rotcar = pygame.transform.rotate(car, angle)
position = rotcar.get_rect(center = (carX,carY))
SCREEN.blit(rotcar, position)
pygame.display.update()
FPS.tick(24)
I'm not too familiar with pygame but I think this game (md5: 92f9f508cbe2d015b18376fb083e0064 file: spacegame.zip) has the feature you're looking for. Thanks to the author, DR0ID_ , for making it publicly available on his/her website
To implement the "wrap around" feature in your car game I used this function from "game.py":
def draw(self, surface):
wrap = 0
for s in self.sprites():
r = s.rect
if r.left<0:
r.move_ip(800, 0)
wrap = 1
elif r.right > 800:
r.move_ip(-800, 0)
wrap = 1
if r.top < 0:
r.move_ip(0, 600)
wrap = 1
elif r.bottom > 600:
r.move_ip(0, -600)
wrap = 1
if wrap:
surface_blit(s.image, r)
wrap = 0
After a few adjustments I ended up with this working example:
import sys, pygame, math
from pygame.locals import *
pygame.init()
# Added HEIGHT and WIDTH as fixed variables
WIDTH = 800
HEIGHT = 600
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
car = pygame.transform.scale(pygame.image.load('Car.png').convert_alpha(), (64, 64))
pygame.display.set_caption('Car Game')
pygame.display.set_icon(car)
FPS = pygame.time.Clock()
carX = 400
carY = 100
angle = 90
speed = 0
while True:
if angle == 360: angle = 0
if angle == -1: angle = 359
SCREEN.fill((0,0,0))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_a] or keys[K_LEFT]:
angle += speed
elif keys[K_d] or keys[K_RIGHT]:
angle -= speed
if keys[K_w] or keys[K_UP]:
speed += 1
elif keys[K_s] or keys[K_DOWN]:
speed -= 0.5
carX += speed*math.cos(math.radians(angle))
carY -= speed*math.sin(math.radians(angle))
speed *= 0.95
rotcar = pygame.transform.rotate(car, angle)
position = rotcar.get_rect(center = (carX,carY))
# Basically a copy/paste of the "draw"-function from the spacegame.zip
wrap_around = False
if position[0] < 0 :
# off screen left
position.move_ip(WIDTH, 0)
wrap_around = True
if position[0] + rotcar.get_width() > WIDTH:
# off screen right
position.move_ip(-WIDTH, 0)
wrap_around = True
if position[1] < 0:
# off screen top
position.move_ip(0, HEIGHT)
wrap_around = True
if position[1] + rotcar.get_height() > HEIGHT:
# off screen bottom
position.move_ip(0, -HEIGHT)
wrap_around = True
if wrap_around:
SCREEN.blit(rotcar, position)
position[0] %= WIDTH
position[1] %= HEIGHT
carX %= WIDTH
carY %= HEIGHT
SCREEN.blit(rotcar, position)
pygame.display.update()
FPS.tick(24)