图像旋转无法正常工作

时间:2018-07-28 14:52:14

标签: python pygame

我是python / pygame的新手,希望获得一些帮助。我试图使两辆汽车自动前进(我已经成功完成了),但是当按下某个键时,我无法设法使汽车左/右转。我希望有人可以帮助我。就像我说的那样,我是新手,所以如果有人愿意帮助您,请您可以编辑我的代码以使它成功以及对出现问题的解释,在此先非常感谢。 https://i.stack.imgur.com/zrlUT.png          https://i.stack.imgur.com/q6Hb9.png          https://i.stack.imgur.com/MGR3N.jpg

P.S。我的代码没有错误消息,因为其中没有错误,旋转功能根本无法正常工作

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


/* POST handler. */
router.post('/', function (req, res, next) {
  res.send('POST request to homepage');
  console.log('post received')
});

module.exports = router;

3 个答案:

答案 0 :(得分:1)

如果要在游戏中旋转汽车并沿其面向的方向移动汽车,则需要了解三角函数或vectors(在以下示例中,我将使用矢量)。位置和速度应为矢量,并且每帧必须将vel添加到pos矢量中以使汽车向前移动。您还需要一个pygame.Rect用作blit位置(矩形可以使汽车更容易居中并可以用于碰撞检测)。

当用户按下←或→时,您必须旋转anglevel矢量(vel也是方向),然后使用角度旋转图像/ pygame.Surface。至于图像旋转,应保留对原始图像的引用,以保持图像的质量。将其与汽车的当前角度一起传递到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()

答案 1 :(得分:0)

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

答案 2 :(得分:0)

也调用了您使用的函数

functionCall()

不是

functionCall = True

(从其他答案看来,您已经解决了它) 同样,您在while循环中的每个循环中都初始化redcar,这意味着您将每个循环中的redcar初始化为0度,绘制它,然后检查旋转,但是即使有旋转,在下一个循环中,您也将再次初始化为0度您可以做很多事情来解决此问题。最简单:移动

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

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