I am working on a game and I needed to make collision detection. I decided to use the function pygame.Rect.colliderect()
. However, it is giving me the error:
pygame 2.0.0.dev6 (SDL 2.0.10, python 3.7.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:/Users/admin/PycharmProjects/ProgrammingLearn/SpaceInvaders.py", line 185, in <module>
main()
File "C:/Users/admin/PycharmProjects/ProgrammingLearn/SpaceInvaders.py", line 112, in main
if pygame.Rect.colliderect(blue_enemy_rect):
TypeError: Argument must be rect style object
I will "Bookmark" the place where I have used pygame.Rect.colliderect()
with #Bookmark
# importing packages
import pygame
import random
import time
import sys
# Initializing Pygame
pygame.init()
# Setting a display width and height and then creating it
display_width = 700
display_height = 500
display_size = [display_width, display_height]
game_display = pygame.display.set_mode(display_size)
intro_display = pygame.display.set_mode(display_size)
# Setting a display caption
pygame.display.set_caption("Space Bugs")
spaceship = pygame.image.load("spaceship2.png")
blue_enemy = pygame.image.load("blue_enemy.png")
green_enemy = pygame.image.load("green_enemy.png")
orange_enemy = pygame.image.load("orange_enemy.png")
pink_enemy = pygame.image.load("pink_enemy.png")
yellow_enemy = pygame.image.load("yellow_enemy.png")
# Creating a font
pygame.font.init()
font = pygame.font.SysFont("consolas", 30)
large_font = pygame.font.SysFont("consolas", 60)
small_font = pygame.font.SysFont("consolas", 20)
# Creating a way to add text to the screen
def message(sentence, color, x, y, font_type, display):
sentence = font_type.render(str.encode(sentence), True, color)
display.blit(sentence, [x, y])
def main():
global white
global black
global clock
# Spaceship coordinates
spaceship_x = 300
spaceship_y = 375
spaceship_x_change = 0
blue_enemy_health = 5
green_enemy_health = 5
orange_enemy_health = 5
pink_enemy_health = 5
yellow_enemy_health = 5
blue_enemy_rect = pygame.Rect(30, 35, 80, 70)
# Initializing pygame
pygame.init()
# Creating colors
red = (255, 0, 0)
blue = (0, 0, 255)
# clock stuff
clock = pygame.time.Clock()
time_elapsed_since_last_action = 0
time_elapsed_since_last_action2 = 0
# Creating a loop to keep the program running
while True:
# --- Event Processing and controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
spaceship_x_change = 10
elif event.key == pygame.K_LEFT:
spaceship_x_change = -10
elif event.type == pygame.KEYUP:
spaceship_x_change = 0
spaceship_x += spaceship_x_change
# Preventing the ship from going off the screen
if spaceship_x > display_width - 140:
spaceship_x -= 10
if spaceship_x < 1:
spaceship_x += 10
laser_coords = [70, 209, 348, 505, 630]
random_x_coord = random.choice(laser_coords)
dt = clock.tick()
time_elapsed_since_last_action += dt
if time_elapsed_since_last_action > 500:
pygame.draw.rect(game_display, blue, [random_x_coord, 85, 6, 305])
time_elapsed_since_last_action = 0
time_elapsed_since_last_action2 += dt
# Setting Display color
game_display.fill(black)
message(str(blue_enemy_health), white, 65, 10, font, game_display)
game_display.blit(blue_enemy, (20, 25))
blue_hit_box = pygame.draw.rect(game_display, white, blue_enemy_rect, 1)
# Bookmark
if pygame.Rect.colliderect(blue_enemy_rect):
blue_enemy_health = blue_enemy_health - 1
message(str(green_enemy_health), white, 203, 10, font, game_display)
game_display.blit(green_enemy, (160, 25))
green_hit_box = pygame.draw.rect(game_display, white, [180, 35, 60, 70], 1)
message(str(orange_enemy_health), white, 341, 10, font, game_display)
game_display.blit(orange_enemy, (300, 25))
orange_hit_box = pygame.draw.rect(game_display, white, [315, 43, 65, 70], 1)
message(str(pink_enemy_health), white, 496, 10, font, game_display)
game_display.blit(pink_enemy, (440, 25))
pink_hit_box = pygame.draw.rect(game_display, white, [460, 35, 90, 70], 1)
message(str(yellow_enemy_health), white, 623, 10, font, game_display)
game_display.blit(yellow_enemy, (580, 25))
yellow_hit_box = pygame.draw.rect(game_display, white, [590, 40, 85, 70], 1)
# Creating a spaceship, lasers, and enemies
laser = pygame.draw.rect(game_display, red, [spaceship_x + 69, 100, 4, 300])
game_display.blit(spaceship, (spaceship_x, spaceship_y))
health = 10
message("Spaceship durability: " + str(health), white, 20, 480, small_font, game_display)
# Updating Screen so changes take places
pygame.display.flip()
# Setting FPS
FPS = pygame.time.Clock()
FPS.tick(60)
# Executing the function
if __name__ == "__main__":
main()
I don't see what is wrong here, as I believe this is how colliderect is used. I am really close to finishing this program, and any help is appreciated. Thanks.