我一直在做一个pygame项目,其中有2个sprites围着一个足球踢,试图打入相应颜色的球网。我正在尝试增加一个方面,即汽车可以按下特定的按钮,然后以增加的速度向前行驶,但只能以特定的速度(例如2秒)前进。我的尝试是在177行,但是速度没有增加到10号。没有错误消息。下面是我的代码,请有人可以修复它以使其成功吗?
import pygame
import pygame as pg
from pygame.math import Vector2
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()
# Images.
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, 0), (50, 10), (50, 20), (0, 30)])
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 variables.
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)
# Car variables.
pos_red = Vector2(x,y)
vel_red = Vector2(redspeed,0)
redrect = redcar.get_rect(center=pos_red)
redangle = 180
pos_blue = Vector2(275,300)
vel_blue = Vector2(bluespeed,0)
bluerect = bluecar.get_rect(center=pos_red)
blueangle = 0
# Masks.
mask_blue = pygame.mask.from_surface(bluecar)
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 printredscore():
print(bluescore, "-", redscore)
def printbluescore():
print(bluescore, "-", redscore)
def redtextscore():
font = pygame.font.Font(None, 72)
redscore1 = str(redscore)
redtext = font.render(redscore1, False, (255,255,255))
screen.blit(redtext, (650,15))
pygame.display.flip()
def bluetextscore():
font1 = pygame.font.Font(None, 72)
bluescore1 = str(bluescore)
bluetext = font1.render(bluescore1, False, (255,255,255))
screen.blit(bluetext, (500,15))
pygame.display.flip()
def dashtext():
font2 = pygame.font.Font(None, 72)
dash = font2.render("-", False, (255,255,255))
screen.blit(dash, (575,15))
pygame.display.flip()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
redtextscore()
bluetextscore()
dashtext()
if y <0:
y = 10
if y > 450:
y = 440
if x > 480:
x = 470
def redgoal():
ball_vel.x = 0
ball_vel.y = 0
ball_pos.x = 575
ball_pos.y = 400
global redscore
redscore += 1
def bluegoal():
ball_vel.x = 0
ball_vel.y = 0
ball_pos.x = 575
ball_pos.y = 400
global bluescore
bluescore += 1
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 redrect.y < 0:
redrect.y *= +10
elif redrect.bottom > screen.get_height() and redrect.y > 0:
redrect.y *= -10
if redrect.left < 0 and redrect.x < 0:
redrect.x *= -10
elif redrect.right > screen.get_width() and redrect.x > 0:
redrect.x *= -10
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)
# We need a new mask after the rotation.
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]:
redspeed == 10
if keys[pygame.K_a]:
blueangle += 5
vel_blue.rotate_ip(-5)
bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
bluerect = bluecar.get_rect(center=bluerect.center)
mask_blue = pygame.mask.from_surface(bluecar)
elif keys[pygame.K_d]:
blueangle -= 5
vel_blue.rotate_ip(5)
bluecar = pygame.transform.rotate(BLUECAR_ORIGINAL, blueangle)
bluerect = bluecar.get_rect(center=bluerect.center)
mask_blue = pygame.mask.from_surface(bluecar)
# Move the cars.
pos_red += vel_red
redrect.center = pos_red
pos_blue += vel_blue
bluerect.center = pos_blue
# Move the ball.
ball_vel *= .97 # Friction.
ball_pos += ball_vel
ballrect.center = ball_pos
# Red car collision.
# We need the offset between the redrect and the ballrect.
offset_red = redrect[0] - ballrect[0], redrect[1] - ballrect[1]
# Pass the offset to the `overlap` method. If the masks collide,
# overlap will return a single point, otherwise `None`.
overlap_red = mask_ball.overlap(mask_red, offset_red)
# Blue car collision.
offset_blue = bluerect[0] - ballrect[0], bluerect[1] - ballrect[1]
overlap_blue = mask_ball.overlap(mask_blue, offset_blue)
offset = redgoal_rect[0] - ballrect[0], redgoal_rect[1] - ballrect[1]
redgoaloverlap = ball_mask.overlap(redgoal_mask, offset)
offset = bluegoal_rect[0] - ballrect[0], bluegoal_rect[1] - ballrect[1]
bluegoaloverlap = ball_mask.overlap(bluegoal_mask, offset)
if redgoaloverlap:
redgoal()
printredscore()
if bluegoaloverlap:
bluegoal()
printbluescore()
if overlap_red and overlap_blue: # Both collide with the ball.
# Not sure what should happen here.
ball_vel = vel_red + vel_blue * 1.4
elif overlap_red: # Red collides with the ball.
ball_vel = Vector2(vel_red) * 1.4
elif overlap_blue: # Blue collides with the ball.
ball_vel = Vector2(vel_blue) * 1.4
# Drawing.
screen.blit(bgImg, (0, 0))
screen.blit(BALL, ballrect)
screen.blit(redcar, redrect)
screen.blit(bluecar, bluerect)
screen.blit(REDGOAL, redgoal_rect)
screen.blit(BLUEGOAL, bluegoal_rect)
pygame.display.flip()
pygame.display.update()
clock.tick(60)
pygame.quit()
答案 0 :(得分:1)
如果要增加速度,可以缩放vel_red
向量。
elif keys[pygame.K_UP]:
vel_red.scale_to_length(10)
更改redspeed
无效,因为vel_red
是红色汽车的速度。另外,您不应该在这里使用==
(等于),而应该在=
中使用redspeed == 10
(分配)。
要使效果基于时间,可以使用其中一种timers,并在效果应结束时将矢量缩放到原始长度。