当精灵移出屏幕Pygame时,将精灵移动到另一侧

时间:2016-03-01 16:29:02

标签: python python-3.x pygame

如果我有一个精灵(见下文)我可以四处移动,屏幕上有箭头键。 (向上和向下移动向前和向后,向左和向右转动。) 我想知道是否有可能在屏幕关闭时移动到屏幕的另一侧?但无论角度如何,它都可以工作,因此如果它被驱动到边缘的一半,则一半出现在一侧。 (有点像蛇)有没有办法做到这一点?

Car

到目前为止,这是我的代码:

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)

2 个答案:

答案 0 :(得分:2)

我对pygame不太熟悉,但我认为this game(md5:92f9f508cbe2d015b18376fb083e0064 file:spacegame.zip)具有您正在寻找的功能。感谢作者DR0ID_,以便在his/her website

上公布

为了在你的汽车游戏中实现“环绕”功能,我使用了“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

经过一些调整后,我最终得到了这个有效的例子:

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)

答案 1 :(得分:0)

您可以只为x和y坐标分别设置坐标“模”(%)屏幕宽度/屏幕高度,而不必使用“ elif”语句。