python - pygame 传球到球门
问题描述
我正在尝试使用 pygame 进行球类游戏的传球,但是一旦我第一次通过在所需方向上单击鼠标来传球,在击中队友 2 后再次单击鼠标,球将不会重定向。我正在努力让球可以在队友之间来回传球,但传球方面给我带来了困难。对我可以通过鼠标点击传球的另一种方式有什么建议吗?
import sys
import pygame
import characters
import math
width = 600
height = 730
win = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
pygame.font.init()
bg = pygame.image.load("backgroundCY300.jpg")
pygame.display.set_caption("Game")
win.blit(bg, (-150, -140))
game_over = False
ballsleft = 3
hit = False
isFalling = True
run = True
click = False
click2 = False
scored = False
font = pygame.font.SysFont('Comic Sans', 50)
def redrawGameWin():
win.blit(text, (300, 100))
win.blit(bg, (-150, -140))
ball.draw(win)
teammate1.draw(win)
reflector1.draw(win)
teammate2.draw(win)
goalie.draw(win)
defense.draw(win)
bound1.draw(win)
pygame.display.update()
def passing(dest_x, dest_y):
if click == True:
ball.floating_point_x = ball.x
ball.floating_point_y = ball.y
dest_x += ball.x
dest_y += ball.y
x_diff = dest_x - ball.x
y_diff = dest_y - ball.y
angle = math.atan2(y_diff, x_diff)
ball.change_x = math.cos(angle) * ball.vel_x
ball.change_y = math.sin(angle) * ball.vel_y
ball.floating_point_y += ball.change_y
ball.floating_point_x += ball.change_x
ball.y = int(ball.floating_point_y)
ball.x = int(ball.floating_point_x)
ball = characters.ball(50, 50, 12)
teammate1 = characters.teammate(50, 150, 20)
teammate2 = characters.teammate(390, 500, 20)
goalie = characters.defense(300, 650, 400, 20, 20)
defense = characters.defense(100, 300, 400, 20, 20)
bound1 = characters.boundry(350, 400, 400, 20, 10)
reflector1 = characters.reflector(50, 500, 200, 10, 12)
#mainloop
while run == True:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x = event.pos[0]
mouse_y = event.pos[1]
click = True
if click == True:
passing(mouse_x, mouse_y)
if click2 == True:
ball.y += 15
characters.defense.mover_t_l(goalie, win)
characters.defense.mover_t_l(defense, win)
"""DEATH BOUNDRIES"""
if ball.y <= 0:
hit = True
if ball.y >= height and ball.x < 200 or ball.y >= height and ball.x > 400:
hit = True
done = False
"""BOUNCY BOUNDRIES"""
if ball.x >= 600 or ball.x <= 0:
ball.vel_x = -ball.vel_x
if ball.y < reflector1.hitbox[1] + reflector1.hitbox[3] and ball.y > reflector1.hitbox[1]:
if ball.x > reflector1.hitbox[0] and ball.x < reflector1.hitbox[0] + reflector1.hitbox[2]:
ball.vel_y = -ball.vel_y
"""DEFENSE"""
if ball.y < goalie.hitbox[1] + goalie.hitbox[3] and ball.y > goalie.hitbox[1]:
if ball.x > goalie.hitbox[0] and ball.x < goalie.hitbox[0] + goalie.hitbox[2]:
hit = True
done = False
if ball.y < defense.hitbox[1] + defense.hitbox[3] and ball.y > defense.hitbox[1]:
if ball.x > defense.hitbox[0] and ball.x < defense.hitbox[0] + defense.hitbox[2]:
hit = True
done = False
if ball.y < bound1.hitbox[1] + bound1.hitbox[3] and ball.y > bound1.hitbox[1]:
if ball.x > bound1.hitbox[0] and ball.x < bound1.hitbox[0] + bound1.hitbox[2]:
hit = True
done = False
"""TEAMMATES"""
*if ball.y < teammate1.hitbox[1] + teammate1.hitbox[3] and ball.y > teammate1.hitbox[1]:
if ball.x > teammate1.hitbox[0] and ball.x < teammate1.hitbox[0] + teammate1.hitbox[2]:
isFalling = False
if ball.y < teammate2.hitbox[1] + teammate2.hitbox[3] and ball.y > teammate2.hitbox[1]:
if ball.x > teammate2.hitbox[0] and ball.x < teammate2.hitbox[0] + teammate2.hitbox[2]:
click = False
isFalling = False
click2 = True*
if ball.y >= height and 200 <= ball.x <= 400:
scored = True
if scored == True:
run = False
done = False
if hit == True:
run = False
game_over = True
done = False
if isFalling == True:
ball.y += 15
if ballsleft > 0:
text = font.render("Balls: " + str(ballsleft), 1, (0, 0, 0))
redrawGameWin()
解决方案
推荐阅读
- javascript - 将数据从原生 android 应用程序发送到 Ionic 本地索引文件
- javascript - 来自 JSON 对象列表的 Charts.js 中的堆积条形图
- ffmpeg - Discord.NET 1.0.2 将语音发送到语音通道不起作用
- azure - 如何为 Azure CLI 使用 Powershell splatting
- amazon-web-services - AWS S3 跨区域复制删除操作行为的状态
- r - 是否有可能一次关闭 R 脚本中的所有代码中断?
- c# - .NET Framework 4.5 中 Entity Framework 的包含方法与 .NET Standard 2.0 不兼容
- swift - SceneKit/ARKit地板反射反射CategoryBitMask
- angular - 使用 Angular 应用数天后 Chrome 变慢了
- vespa - TimerTask 在 vespa 的 Searcher 中不起作用