I am fairly new to python and the module pygame. I also recently started work on a small project where the purpose of the game is to if a circle touches the "player" (the square) then the game will end or present a message saying you loose (still trying to solve the hitbox issue so still thinking of what to do after). The problem I am having is I am having trouble creating a way to detect the collision of the hitboxes and every time I try to make a way to test if the hitboxes I have collided it just won't work, so if someone can tell me how to make the classes and hitbox, another method, or even how to fix my code so it won't crash with a player class. Thank you, for any help, I may receive.
Here is the code (apologies if it is horrible on the eyes I kept on deleting and adding stuff while trying to find a solution and also sorry if I did something improper this is one of my first questions).
import pygame
import random
pygame.init()
width, height = 800, 800
hbox, vbox = 15, 15
rect = pygame.Rect(500, 600, hbox, vbox)
velocity = (0, 0)
frames = 40
ball_size = 12
white = (255, 255, 255)
black = (0, 0, 0)
hp = 100
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
is_blue = True
clock = pygame.time.Clock()
class Ball:
def __init__(self):
self.x = 0
self.y = 0
self.change_x = 0
self.change_y = 0
self.hitbox = (self.x + 1, self.y + 2, 31, 57)
def make_ball():
ball = Ball()
Ball.hitbox = ball.hitbox
ball.x = random.randrange(ball_size, width - ball_size)
ball.y = random.randrange(ball_size, height - ball_size)
ball.change_x = random.randrange(-2, 3)
ball.change_y = random.randrange(-2, 3)
return ball
ball_list = []
ball = make_ball()
ball_list.append(ball)
class player(object):
def __init__(self, ):
self.x = rect.x
self.y = rect.y
self.box_width = hbox
self.box_hieght = vbox
self.speed = move
player.hitbox = (self.x + 1, self.y + 11, 29, 52)
def draw(self, is_blue):
if is_blue:
color = (0, 128, 255)
else:
color = (255, 100, 0)
if event.type == pygame.KEYDOWN and event.key == pygame.K_c:
is_blue = not is_blue
Player = player
Player = pygame.draw.rect(screen, color, rect)
def move(self):
surface = pygame.Surface((100, 100))
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]:
move = 8
else:
move = 4
if keys[pygame.K_w]:
rect.y -= move
if rect.y < 0:
rect.y = 0
if keys[pygame.K_s]:
rect.y += move
if rect.y > height - hbox:
rect.y = height - vbox
if keys[pygame.K_a]:
rect.x -= move
if rect.x < 0:
rect.x = 0
if keys[pygame.K_d]:
rect.x += move
if rect.x > width - hbox:
rect.x = width - hbox
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
# Space bar! Spawn a new ball.
if event.key == pygame.K_SPACE:
ball = make_ball()
ball_list.append(ball)
if rect.x == ball.x and rect.y == ball.y:
hp -100
if hp == 0:
pygame.quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_c:
is_blue = not is_blue
surface = pygame.Surface((100, 100))
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]:
move = 8
else:
move = 4
if keys[pygame.K_w]:
rect.y -= move
if rect.y < 0:
rect.y = 0
if keys[pygame.K_s]:
rect.y += move
if rect.y > height - hbox:
rect.y = height - vbox
if keys[pygame.K_a]:
rect.x -= move
if rect.x < 0:
rect.x = 0
if keys[pygame.K_d]:
rect.x += move
if rect.x > width - hbox:
rect.x = width - hbox
screen.fill((0, 0, 0))
if is_blue:
color = (0, 128, 255)
else:
color = (255, 100, 0)
color_2 = (255, 0, 0)
for ball in ball_list:
ball.x += ball.change_x
ball.y += ball.change_y
if ball.y > height - ball_size or ball.y < ball_size:
ball.change_y *= -1
if ball.x > width - ball_size or ball.x < ball_size:
ball.change_x *= -1
screen.fill(black)
for ball in ball_list:
pygame.draw.circle(screen, white, [ball.x, ball.y], ball_size)
Rectangle = pygame.draw.rect(screen, color, rect)
pygame.display.flip()
clock.tick(30)
`