Python 皮亚杰将球传给球门
我正在尝试使用pygame在球类游戏中传球,但一旦我第一次通过在所需方向单击鼠标传球,球将不会在击中队友2后再次单击鼠标重定向。我正在努力使球能够在队友之间来回传递,但是传球方面给了我困难。有什么建议可以让我用鼠标点击来传球吗Python 皮亚杰将球传给球门,python,python-3.x,pygame,Python,Python 3.x,Pygame,我正在尝试使用pygame在球类游戏中传球,但一旦我第一次通过在所需方向单击鼠标传球,球将不会在击中队友2后再次单击鼠标重定向。我正在努力使球能够在队友之间来回传递,但是传球方面给了我困难。有什么建议可以让我用鼠标点击来传球吗 import sys import pygame import characters import math width = 600 height = 730 win = pygame.display.set_mode((width, height)) clock =
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()
导入系统
导入pygame
导入字符
输入数学
宽度=600
高度=730
win=pygame.display.set_模式((宽度、高度))
clock=pygame.time.clock()
pygame.font.init()
bg=pygame.image.load(“backgroundCY300.jpg”)
pygame.display.set_标题(“游戏”)
温·布利特(背景,(-150,-140))
游戏结束=错误
鲍尔斯里夫特=3
命中=错误
是真的吗
运行=真
单击=False
click2=错误
得分=错误
font=pygame.font.SysFont('Comic Sans',50)
def重新绘制GameWin():
win.blit(文本,(300100))
温·布利特(背景,(-150,-140))
球。平局(胜利)
队友1.平局(获胜)
反射器1.平局(赢)
2.平局(获胜)
守门员。平局(获胜)
防守。平局(获胜)
边界1.平局(赢)
pygame.display.update()
def通过(目的地x、目的地y):
如果单击==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
角度=数学atan2(y_diff,x_diff)
ball.change_x=数学cos(角度)*ball.vel_x
ball.change_y=math.sin(角度)*ball.vel_y
ball.floating_point_y+=ball.change_y
ball.floating_point_x+=ball.change_x
ball.y=int(ball.floating\u point\u y)
ball.x=int(ball.floating\u point\u x)
ball=字符。ball(50,50,12)
队友1=角色。队友(50、150、20)
队友2=角色。队友(390、500、20)
守门员=角色。防守(300、650、400、20、20)
防御=角色。防御(1003004002020)
bound1=字符。边界(350、400、400、20、10)
反射器1=字符。反射器(50、500、200、10、12)
#主回路
当run==True时:
pygame.时间延迟(100)
对于pygame.event.get()中的事件:
如果event.type==pygame.MOUSEBUTTONDOWN:
鼠标_x=事件位置[0]
鼠标_y=event.pos[1]
单击=真
如果单击==True:
传球(鼠标x,鼠标y)
如果click2==True:
球.y+=15
角色,防守,移动(守门员,胜利)
角色。防御。移动(防御,胜利)
“死亡边界”
如果ball.y=高度和ball.x<200或ball.y>=高度和ball.x>400:
命中=正确
完成=错误
“弹性边界”
如果ball.x>=600或ball.x反射器1.hitbox[1]:
如果ball.x>reflector 1.hitbox[0]和ball.x