我一直在尝试使用pygame制作足球比赛,并且几乎成功完成了比赛,但是,我遇到了一些问题。我进球后试图移动球,我曾尝试将球移动到ball_x和ball_y坐标上,但不起作用。我的代码在下面,我真的希望有人可以编辑我的代码以使其起作用。
import pygame
import pygame as pg
from pygame.math import Vector2
pygame.init()
WIDTH = 1150
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
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
score = 0
redspeed = 7
bluespeed = 7
ball_x = 575
ball_y = 400
dx = 0
dy = 0
x = 800
y = 500
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 redgoal():
print("Red Goal!!!")
ball_x = 300
ball_y = 300
def bluegoal():
print("Blue Goal!!!")
boostedspeedred = 10
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 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 *= .99 # 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()
if bluegoaloverlap:
bluegoal()
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)
问题是您正在设置ball_x
和ball_y
,但是这些球并未在这些位置绘制。实际上是在称为ball_pos
的向量的坐标上绘制球。
要解决此问题:
ball_pos
。ball_vel
,所以球再次移到中心后就不会移动。您可能还想添加代码来重置汽车的位置。另外,只有在有“红色进球”的情况下,球才会重置。
这是固定代码:
import pygame
import pygame as pg
from pygame.math import Vector2
pygame.init()
WIDTH = 1150
HEIGHT = 800
screen = pygame.display.set_mode((WIDTH, HEIGHT))
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
score = 0
redspeed = 7
bluespeed = 7
ball_x = 575
ball_y = 400
dx = 0
dy = 0
x = 800
y = 500
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 redgoal():
print("Red Goal!!!")
ball_vel.x = 0
ball_vel.y = 0
ball_pos.x = 575
ball_pos.y = 400
def bluegoal():
print("Blue Goal!!!")
boostedspeedred = 10
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 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 *= .99 # 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()
if bluegoaloverlap:
bluegoal()
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()