当精灵移出屏幕时将精灵移到另一侧 Pygame [英] Moving a sprite to the other side when a sprite moves off screen Pygame

查看:69
本文介绍了当精灵移出屏幕时将精灵移到另一侧 Pygame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个可以四处移动的精灵(见下文),屏幕上的箭头键.(上下前后移动,左右转弯.)我想知道当它熄灭时是否可以移动到屏幕的另一侧?但是无论角度如何它都可以工作,所以如果它被驱动一半进入边缘,一半会出现在一侧.(有点像蛇)有没有办法做到这一点?

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?

这是我目前的代码:

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)

推荐答案

我对 pygame 不太熟悉,但我认为 此游戏(md5:92f9f508cbe2d015b18376fb083e0064 文件:spacegame.zip)具有您正在寻找的功能.感谢作者 DR0ID_ 在他/她的网站

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

为了在你的汽车游戏中实现环绕"功能,我使用了game.py"中的这个函数:

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)

这篇关于当精灵移出屏幕时将精灵移到另一侧 Pygame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆