图像旋转未按预期工作

2024-04-26 09:20:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python/pygame非常陌生,需要一些帮助。我试图使两辆车自动向前移动(我已经成功地完成了),但我不能设法使汽车左转/右转时,某一关键是按下。我希望有人能帮我。正如我所说,我是新来的,所以如果有人愿意帮助,请您编辑我的代码,使它是成功的,以及解释什么是错的,非常感谢您提前。https://i.stack.imgur.com/zrlUT.pnghttps://i.stack.imgur.com/q6Hb9.pnghttps://i.stack.imgur.com/MGR3N.jpg

另外,我的代码没有错误信息,因为没有错误,rotate函数根本不起作用

import pygame
import sys
pygame.init()

screen = pygame.display.set_mode((1150, 800))



pygame.display.set_caption("Car Football")

degrees = 180

x = 70
y = 70

x1 = 1000
y1 = 400

width = 70
height = 70


vel = 15
vel1 = 15

def rotatedleft():
    pygame.transform.rotate(redcar, (15))

def rotatedright():
     pygame.transform.rotate(redcar, (-15))



run = True
while run:
pygame.time.delay(100)

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        run = False

screen.fill((0, 0, 0))
bgImg = pygame.image.load("Football_pitch.png").convert()
screen.blit(bgImg, [0,0])
redcar = pygame.image.load("redcar.png")
screen.blit(redcar, (x, y))


keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
    rotatedleft = True
if run:
    x += vel

if keys[pygame.K_RIGHT]:
    rotatedright = True



pygame.display.flip()
pygame.display.update()

for event in pygame.event.get():
     if event.type == pygame.QUIT:
         run = False
keys = pygame.key.get_pressed()
if run:
     x1 -= vel1
if keys[pygame.K_d] and x1 < 1150:
     x1 += vel1
if keys[pygame.K_w]:
     y1 -= vel1
if keys[pygame.K_s]:
    y1 += vel1


bluecar = pygame.image.load("Bluecarss.png")
screen.blit(bluecar, (x1, y1))
circleball = pygame.draw.circle(screen, (255, 255, 255), (300, 140), 50, 0)
pygame.display.update()





pygame.display.flip()
pygame.quit()
sys.exit()

Tags: runhttpseventgetifpngstackdisplay
3条回答

如果你想在游戏中旋转一辆车并将它朝着它所面对的方向移动(我在下面的例子中使用向量),你需要知道三角学或者vectors。位置和速度应该是矢量,你必须每一帧把vel加到pos矢量上才能使汽车前进。您还需要一个^{}作为blit位置(rect使汽车更容易居中,并可用于碰撞检测)。你知道吗

当用户按←或→时,必须旋转anglevel向量(vel也是方向),然后使用角度旋转图像/pygame.表面. 至于图像旋转,为了保持图像的质量,应该保留对原始图像的引用。把它和汽车的当前角度一起传递给pygame.transform.rotate,你会得到一个旋转的曲面。此曲面将具有不同的维度,因此需要创建一个新的rect(使用pygame.Surface.get_rect)来更新blit位置(左上角坐标)。你知道吗

import pygame
from pygame.math import Vector2


pygame.init()
screen = pygame.display.set_mode((1150, 800))
clock = pygame.time.Clock()  # A clock to limit the frame rate.
# Load your images once at the beginning of the program.
REDCAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
    REDCAR_ORIGINAL, (200, 0, 0), [(0, 0), (50, 10), (50, 20), (0, 30)])
redcar = REDCAR_ORIGINAL

# Use vectors for the position and the velocity.
pos = Vector2(70, 70)  # Center position of the car.
vel = Vector2(2, 0)
# This rect serves as the blit position of the redcar image.
rect = redcar.get_rect(center=pos)
angle = 0  # Current angle of the car.

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        # When a key gets pressed, increment the angle, rotate
        # the vector and create a new rotated car image.
        # The angle is rotated in the opposite direction because
        # pygame's y-axis is flipped.
        angle += 3
        vel.rotate_ip(-3)
        redcar = pygame.transform.rotate(REDCAR_ORIGINAL, angle)
        # Also, get a new rect because the image size was changed.
        # Pass the pos vector as the `center` argument to keep the
        # image centered.
        rect = redcar.get_rect(center=pos)
    elif keys[pygame.K_RIGHT]:
        angle -= 3
        vel.rotate_ip(3)
        redcar = pygame.transform.rotate(REDCAR_ORIGINAL, angle)
        rect = redcar.get_rect(center=pos)

    # Move the car by adding the velocity to the position.
    pos += vel
    rect.center = pos  # Update the rect as well.

    screen.fill((30, 30, 30))
    screen.blit(redcar, rect)  # Blit the car at the `rect.topleft` coords.

    pygame.display.flip()
    clock.tick(60)  # Limit the frame rate to 60 FPS.

pygame.quit()
if keys[pygame.K_LEFT]:
    rotatedleft = True

如果要将True赋值给rotatedleft(您以前将其定义为函数),请尝试调用该函数,如下所示:

if keys[pygame.K_LEFT]:
    rotatedleft()

现在有点拐弯了,我很确定这还不是你想要的,但是嘿,宝贝。祝你比赛好运。你知道吗

import pygame
import sys
pygame.init()

screen = pygame.display.set_mode((1150, 800))



pygame.display.set_caption("Car Football")

degrees = 180

x = 70
y = 70

x1 = 1000
y1 = 400

width = 70
height = 70


vel = 15
vel1 = 15


run = True
while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    screen.fill((0, 0, 0))
    bgImg = pygame.image.load("Football_pitch.png").convert()
    screen.blit(bgImg, [0,0])
    redcar = pygame.image.load("redcar.png")
    screen.blit(redcar, (x, y))


    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        redcar = pygame.transform.rotate(redcar, 15)
        screen.blit(redcar, (x, y))

    if run:
        x += vel

    if keys[pygame.K_RIGHT]:
        redcar = pygame.transform.rotate(redcar, -15)
        screen.blit(redcar, (x, y))



    pygame.display.flip()
    pygame.display.update()

    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             run = False
    keys = pygame.key.get_pressed()
    if run:
         x1 -= vel1
    if keys[pygame.K_d] and x1 < 1150:
         x1 += vel1
    if keys[pygame.K_w]:
         y1 -= vel1
    if keys[pygame.K_s]:
        y1 += vel1


    bluecar = pygame.image.load("Bluecarss.png")
    screen.blit(bluecar, (x1, y1))
    circleball = pygame.draw.circle(screen, (255, 255, 255), (300, 140), 50, 0)
    pygame.display.update()





pygame.display.flip()
pygame.quit()
sys.exit()

太调用你使用的函数了

functionCall()

不是

functionCall = True

(从另一个答案看,好像是你修好的) 另外,在while循环中初始化redcar的每个循环,这意味着您将redcar初始化为0度每个循环,绘制它,然后检查旋转,但是即使有旋转,在下一个循环中您也将再次初始化为0学位。你呢可以做很多事情来解决这个问题。最简单的方法:移动

 screen.blit(redcar, (x, y))

检查两次旋转后 然后将redcar初始化到while循环之前,它应该可以工作。你知道吗

相关问题 更多 >