I am making a game where the player hits the ball against a wall and it bounces off, but as I was setting up the player and the wall I tried to make it so you can collide with the wall, but I phased right through.
Btw, I have tried get_rect(topleft = (X, Y)), but that didn’t work either script:
import pygame
WIDTH, HEIGHT = 700, 600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
GREY = [117,117,117]
Player = pygame.image.load("Player.png")
Wall = pygame.image.load("Wall.png")
PX = 0
PY = 0
def draw_window():
global GREY
global Player
global PX
global PY
WIN.fill(GREY)
WIN.blit(Player, (PX,PY))
WIN.blit(Wall, (510,0))
Black = [0,0,0]
Wall_rect = Wall.get_rect()
pygame.draw.rect(WIN, Black, (510, 0, Wall.get_width(), Wall.get_height()), True)
pygame.draw.rect(WIN, Black, (PX, PY, Player.get_width(), Player.get_height()), True)
pygame.display.update()
def main():
global PX
global PY
global Wall
global Player
Wall_rects = Wall.get_rect(topleft = (510,0))
Wall_rect = pygame.Rect(510, 0, Wall.get_width(), Wall.get_height())
Player_rect = pygame.Rect(PX, PY, Player.get_width(), Player.get_height())
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
keys = pygame.key.get_pressed()
speed = 1.5
if keys[pygame.K_LEFT]:
PX -= speed
if keys[pygame.K_RIGHT]:
PX += speed
if keys[pygame.K_DOWN]:
PY += speed
if keys[pygame.K_UP]:
PY -= speed
if pygame.Rect.colliderect(Wall_rect, Player_rect):
print("COllision")
draw_window()
main()