gpt4 book ai didi

python - Pygame 在事件发生后移动 Sprite 坐标

转载 作者:行者123 更新时间:2023-12-01 09:09:06 25 4
gpt4 key购买 nike

我一直在尝试用 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()

最佳答案

问题是您正在设置 ball_xball_y,但球并未在这些位置绘制。球实际上是在名为 ball_pos 的向量中的坐标处绘制的。

要解决此问题:

  1. 正如您可能猜到的,有一个非常简单的解决方案:只需重置 ball_pos 即可。
  2. 此外,我重置了矢量 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()

关于python - Pygame 在事件发生后移动 Sprite 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51810316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com