I am trying to make a pass a ball game using pygame but once I pass the ball the first time by clicking the mouse in desired direction, the ball will not redirect with a second mouse click after hitting teammate2. I am trying to make it so that the ball can be passed back and forth among teammates, but the passing aspect is giving me difficulty. Any recommendations for another way I could pass the ball around with a mouse click?
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()