I'm creating a small 2D game and I want to detect collision between two PNG files. For this, I suppose I need to convert a surface to rect because I don't think surface can be used for collision detect. If you think about any other solutions to this than converting from surface to rect, let me know.
Error:
File "c:\Users\xxxx\Desktop\Projects\Games\game.py", line 44, in <module>
window.blit(kert, (1720, 0))
TypeError: argument 1 must be pygame.Surface, not pygame.Rect
Entire code (if it even matters):
# Importing pygame and random library:
import pygame, random
# Starting up the pygame module:
pygame.init()
# Variables:
# PNG FILES:
kert = pygame.image.load('veriscarykert.png').get_rect()
henry = pygame.image.load('veriscaryhentai.png').get_rect()
# FONT:
# Parameters - Font name; Font Size
font = pygame.font.SysFont('None', 48)
# TEXT:
# Parameters - Text; Bool; RGB Value
text = font.render("CATCH THE SCARI KERT!!!", True, (0, 0, 0))
# Set the width and height of the pygame window
# Windowed
window = pygame.display.set_mode((1920,1020))
# Set the name for the pygame window:
pygame.display.set_caption('CATCH SCARI KERT!!')
# Define coordinates of the henry PNG file in X and Z:
henry_x = 540
henry_z = 540
# Variable for collision:
collide = kert.colliderect(henry)
# While loop
while True:
# Set the window's colour with RGB value
# In this case, I've chosen yellow as the colour.
# Putting window.fill in the while loop removes trails of the moving PNG file
window.fill((255,255,0))
# Apply the kert PNG file to the window:
# PARAMETERS - Variable; Coordinates
window.blit(kert, (1720, 0))
# Apply the kert PNG file to the window:
# PARAMETERS - Variable; Coordinates
window.blit(henry, (henry_x, henry_z))
# Apply text to the window:
# PARAMETERS - Variable; Coordinates
window.blit(text, (100, 300))
# Variable for key presses:
key = pygame.key.get_pressed()
# If henry and kert collide
if collide:
# Define kert variable coordinates with the random module
kert_x = random.randint(50, 1720)
kert_z = random.randint(50, 900)
# If the user presses the close button on the window's border:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# Close the game
# If W key is pressed
if key[pygame.K_UP]:
# Remove 5 from Z coordinates
henry_z -= 5
# If S key is pressed
if key[pygame.K_DOWN]:
# Add 5 to Z coordinates
henry_z += 5
# If A key is pressed
if key[pygame.K_LEFT]:
# Remove 5 from X coordinates
henry_x -= 5
# If D key is pressed
if key[pygame.K_RIGHT]:
# Add 5 to X coordinates
henry_x += 5
# Save changes
pygame.display.update()