I am making a basketball game where a ball is shot and when it hits a moving hoop it scores a point. I have to do it using the object oriented programming style, but I am having trouble creating a collision statement to detect when the ball is touching the hoop. The collision statement I am trying to create is in the Ball class under the hoop_collisions function. hit_hoop = pygame.sprite.spritecollide(self, hoops, False)
As far as I am aware, the sprite collide thing doesn't work unless it has a sprite group as the second parameter. The problem I am having is that my hoops (basketball hoop) sprite group is defined in the main file. hoops = pygame.sprite.Group() hoop = Hoop(250, 10) hoops.add(hoop)
I can't call the sprite group because the Ball class as well as the rest of the class files are being imported into the main file, and I can't import it back because it creates an error. I have tried moving the collision statement to other files but I always end up with things not being able to be defined in that file, and end up in a loop. I am fairly new to python and coding in general so any help would be appreciated.
import os
import math
import pygame
class Hoop(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
image_location = os.path.join("assets", "unnamed (1).png")
self.image = pygame.image.load(image_location).convert_alpha()
self.size = self.image.get_size()
self.image = pygame.transform.scale(self.image, (240, 200))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.x_speed = 2
def move(self, x_change):
self.rect.x += x_change
def wall_bounce(self):
if self.rect.right >= 700 or self.rect.left <= -50:
self.x_speed *= -1
def update(self):
self.move(self.x_speed)
self.wall_bounce()
import os
import math
import pygame
from boundary import Hoop
class Ball(pygame.sprite.Sprite):
def __init__(self, x, y, x_speed, y_speed):
super().__init__()
image_location = os.path.join("assets", "unnamed.png")
self.image = pygame.image.load(image_location).convert_alpha()
self.size = self.image.get_size()
self.image = pygame.transform.scale(self.image, (75, 60))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.x_speed = y_speed
self.y_speed = x_speed
score_count = 0
def move(self, x_change, y_change):
self.rect.x += x_change
self.rect.y += y_change
def wall_collide(self):
if self.rect.right >= 650 or self.rect.left <= 0:
# 650 is the screen width
self.x_speed *= -1
elif self.rect.bottom >= 1000:
# 1000 is the screen height
self.kill()
def gravity(self):
gravity = 0.95
self.y_speed += gravity
def hoop_collisions(self):
hit_hoop = pygame.sprite.spritecollide(self, hoops, False)
if hit_hoop:
score_count += 1
self.kill()
def update(self):
self.move(self.x_speed, self.y_speed)
self.gravity()
self.wall_collide()
self.hoop_collisions()
import os
import math
import pygame
import time
from ball import Ball
from boundary import Hoop
class Cannon(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
image_location = os.path.join("assets", "unnamed (2).png")
self.image = pygame.image.load(image_location).convert_alpha()
self.image = pygame.transform.scale(self.image, (200, 200))
self.original_image = self.image
self.rect = self.image.get_rect()
self.position = (325, 900)
self.balls = pygame.sprite.Group()
self.rect.x = x
self.rect.y = y
def rotate(self):
self.mouse_x, self.mouse_y = pygame.mouse.get_pos()
self.rel_x, self.rel_y = self.mouse_x - self.rect.centerx, self.mouse_y - self.rect.centery
self.angle = math.atan2(-self.rel_y, self.rel_x) + 1.57
self.angle2 = (180 / math.pi) * -math.atan2(self.rel_y, self.rel_x) - 90
self.image = pygame.transform.rotate(self.original_image, int(self.angle2))
self.rect = self.image.get_rect(center = self.position)
def create_new_ball(self):
new_ball = Ball(self.rect.centerx, self.rect.centery, (40 * math.cos(self.angle)), (40 * math.sin(self.angle)))
self.balls.add(new_ball)
time.sleep(0.1)
def update(self):
self.rotate()
self.balls.update()
import sys
import os
import pygame
from ball import Ball
from boundary import Hoop
from level import Level
from cannon import Cannon
"""
SETUP section - preparing everything before the main loop runs
"""
pygame.init()
# Global constants
SCREEN_WIDTH = 650
SCREEN_HEIGHT = 1000
FRAME_RATE = 30
# Useful colors
BLACK = (0, 0, 0)
GREEN = (40, 147, 43)
WHITE = (255, 255, 255)
# Creating the screen and the clock
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen.set_alpha(0) # Make alpha bits transparent
clock = pygame.time.Clock()
# Hoops sprite group
hoops = pygame.sprite.Group()
hoop = Hoop(250, 10)
hoops.add(hoop)
# cannon sprite group and position
cannons = pygame.sprite.Group()
cannon = Cannon(230, 795)
cannons.add(cannon)
run = True
while run:
"""
EVENTS section
"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
sys.exit()
# Keyboard events
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_UP] or keys_pressed[pygame.K_w]:
pass
if keys_pressed[pygame.K_LEFT] or keys_pressed[pygame.K_a]:
pass
if keys_pressed[pygame.K_RIGHT] or keys_pressed[pygame.K_d]:
pass
if keys_pressed[pygame.K_DOWN] or keys_pressed[pygame.K_s]:
pass
if keys_pressed[pygame.K_SPACE]:
cannon.create_new_ball()
"""
UPDATE section
"""
cannon.balls.update()
hoops.update()
cannons.update()
"""
DRAW section
"""
screen.fill(GREEN)
cannon.balls.draw(screen)
hoops.draw(screen)
cannons.draw(screen)
pygame.display.flip(
)
clock.tick(
FRAME_RATE
)