Pygame-事件发生后精灵无法旋转

时间:2018-08-15 10:50:20

标签: python python-3.x pygame

我一直在做足球比赛,其中有2辆车试图将球撞成网状。有三件事使我的汽车的旋转/角度不正确,撞到了墙壁(侧面或顶部和底部),一旦开始游戏,旋转就开始向后旋转,并且当进球得分时,汽车将以任何角度继续行驶尽管重新设置了红色角度,但在进入目标之前它保持的角度。我的代码在下面,希望有人可以修复它。

import pygame
import pygame as pg
from pygame.math import Vector2
import time
pygame.font.init()
pygame.init()
WIDTH = 1150
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Rocket League")
clock = pygame.time.Clock()
start_ticks = pygame.time.get_ticks()
bgimg = pygame.image.load("Football_pitch.png")
REDGOAL = pg.Surface((50, 150), pg.SRCALPHA)
REDGOAL.fill((255, 0, 0))
redgoal_rect = REDGOAL.get_rect(topleft=(0, 340))
redgoal_mask = pg.mask.from_surface(REDGOAL)
BLUEGOAL = pg.Surface((50, 150), pg.SRCALPHA)
BLUEGOAL.fill((0, 0, 255))
bluegoal_rect = REDGOAL.get_rect(topleft=(1100, 340))
bluegoal_mask = pg.mask.from_surface(REDGOAL)
BLUECAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
    BLUECAR_ORIGINAL, (0, 0, 255), [(0, 30), (50, 20), (50,10), (0, 0)])
bluecar = BLUECAR_ORIGINAL
REDCAR_ORIGINAL = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(
    REDCAR_ORIGINAL, (255, 0, 0), [(0, 30), (50, 20), (50, 10), (0, 0)])
redcar = REDCAR_ORIGINAL
redspeed = 5
bluespeed = 5
ball_x = 575
ball_y = 400
dx = 0
dy = 0
x = 800
y = 500
redscore = 0
bluescore = 0
BALL = pygame.Surface((30, 30), pygame.SRCALPHA)
pygame.draw.circle(BALL, [0,0,0], [15, 15], 15)
ball_pos = Vector2(ball_x, ball_y)
ballrect = BALL.get_rect(center=ball_pos)
ball_vel = Vector2(dx, dy)
ball_mask = pg.mask.from_surface(BALL)
pos_red = Vector2(x,y)
vel_red = Vector2(redspeed,0)
redrect = redcar.get_rect(center=pos_red)
redangle = 180
mask_red = pygame.mask.from_surface(redcar)
mask_ball = pygame.mask.from_surface(BALL)
ballrect = BALL.get_rect(center=ball_pos)
vel_red.rotate_ip(-180)

def redgoal():
        ball_vel.x = 0
        ball_vel.y = 0
        ball_pos.x = 575
        ball_pos.y = 400
        vel_red.x = redspeed
        vel_red.y = 0
        pos_red.x = 800
        pos_red.y = 370
        redangle = 180
        vel_red.rotate_ip(-180)   

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

    if y <0:
        y = 10
    if y > 450:
        y = 440
    if x > 480:
        x = 470

    if ballrect.top < 0 and ball_vel.y < 0:
        ball_vel.y *= -1
    elif ballrect.bottom > screen.get_height() and ball_vel.y > 0:
        ball_vel.y *= -1
    if ballrect.left < 0 and ball_vel.x < 0:
        ball_vel.x *= -1
    elif ballrect.right > screen.get_width() and ball_vel.x > 0:
        ball_vel.x *= -1

    if redrect.top < 0 and vel_red.y < 0:
        vel_red.y *= -1
        redangle == 0
    elif redrect.bottom > screen.get_height() and vel_red.y > 0:
        vel_red.y *= -1
        redangle == 0
    if redrect.left < 0 and vel_red.x < 0:
        vel_red.x *= -1
        redangle == 0
    elif redrect.right > screen.get_width() and vel_red.x > 0:
        vel_red.x *= -1
        redangle == 0   

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        redangle += 5
        vel_red.rotate_ip(-5)
        redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
        redrect = redcar.get_rect(center=redrect.center)
        mask_red = pygame.mask.from_surface(redcar)
    elif keys[pygame.K_RIGHT]:
        redangle -= 5
        vel_red.rotate_ip(5)
        redcar = pygame.transform.rotate(REDCAR_ORIGINAL, redangle)
        redrect = redcar.get_rect(center=redrect.center)
        mask_red = pygame.mask.from_surface(redcar)
    elif keys[pygame.K_UP]:
        vel_red.scale_to_length(10)

    pos_red += vel_red
    redrect.center = pos_red
    ball_vel *= .97  
    ball_pos += ball_vel
    ballrect.center = ball_pos

    offset_red = redrect[0] - ballrect[0], redrect[1] - ballrect[1]
    overlap_red = mask_ball.overlap(mask_red, offset_red)

    offset = redgoal_rect[0] - ballrect[0], redgoal_rect[1] - ballrect[1]
    redgoaloverlap = ball_mask.overlap(redgoal_mask, offset)

    if redgoaloverlap:
        redgoal()

    elif overlap_red:  
        ball_vel = Vector2(vel_red) * 1.4

    screen.blit(bgimg,(0,0))
    screen.blit(BALL, ballrect)
    screen.blit(redcar, redrect)
    screen.blit(REDGOAL, redgoal_rect)

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

1 个答案:

答案 0 :(得分:2)

如果您想沿边框滑动而不是弹跳,则可以在离开屏幕时将rect的top等属性设置为边框坐标,然后设置pos_red矢量rect.center坐标。速度不会受到影响。

要更改汽车图像的初始旋转,您需要以所需的角度pg.transform.rotate redcar = pg.transform.rotate(REDCAR_ORIGINAL, redangle)

在程序开始vel_red = Vector2(-redspeed, 0)时,vel_red向量的x分量应为负(指向左侧)。

import pygame as pg
from pygame.math import Vector2


pg.init()
screen = pg.display.set_mode((1150, 800))
clock = pg.time.Clock()

REDCAR_ORIGINAL = pg.Surface((50, 30), pg.SRCALPHA)
pg.draw.polygon(
    REDCAR_ORIGINAL, (255, 0, 0), [(0, 30), (50, 20), (50, 10), (0, 0)])
redangle = 180
# Rotate the image/surface before the game starts.
redcar = pg.transform.rotate(REDCAR_ORIGINAL, redangle)
redspeed = 5
pos_red = Vector2(800, 500)
# -redspeed means the vector points to the left.
vel_red = Vector2(-redspeed, 0)
redrect = redcar.get_rect(center=pos_red)
mask_red = pg.mask.from_surface(redcar)

run = True
while run:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            run = False
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_r:
                # Reset the car to the start position.
                redangle = 180
                redcar = pg.transform.rotate(REDCAR_ORIGINAL, redangle)
                pos_red = Vector2(800, 500)
                vel_red = Vector2(-redspeed, 0)
                redrect = redcar.get_rect(center=pos_red)
                mask_red = pg.mask.from_surface(redcar)

    keys = pg.key.get_pressed()
    if keys[pg.K_LEFT]:
        redangle += 5
        vel_red.rotate_ip(-5)
        redcar = pg.transform.rotate(REDCAR_ORIGINAL, redangle)
        redrect = redcar.get_rect(center=redrect.center)
        mask_red = pg.mask.from_surface(redcar)
    elif keys[pg.K_RIGHT]:
        redangle -= 5
        vel_red.rotate_ip(5)
        redcar = pg.transform.rotate(REDCAR_ORIGINAL, redangle)
        redrect = redcar.get_rect(center=redrect.center)
        mask_red = pg.mask.from_surface(redcar)
    elif keys[pg.K_UP]:
        vel_red.scale_to_length(10)

    # Move the car.
    pos_red += vel_red
    redrect.center = pos_red

    # Slide at the borders.
    if redrect.top < 0 and vel_red.y < 0:
        redrect.top = 0
        pos_red = Vector2(redrect.center)
    elif redrect.bottom > screen.get_height() and vel_red.y > 0:
        redrect.bottom = screen.get_height()
        pos_red = Vector2(redrect.center)
    if redrect.left < 0 and vel_red.x < 0:
        redrect.left = 0
        pos_red = Vector2(redrect.center)
    elif redrect.right > screen.get_width() and vel_red.x > 0:
        redrect.right = screen.get_width()
        pos_red = Vector2(redrect.center)

    # Blit everything.
    screen.fill((30, 110, 30))
    screen.blit(redcar, redrect)
    rec = pg.Rect(800, 500, 50, 30)
    rec.center = (800, 500)
    pg.draw.rect(screen, (90, 20, 20), rec, 1)  # Start position.
    pg.display.flip()
    clock.tick(60)

pg.quit()