Мне отчаянно нужна помощь. Я пытался реализовать столкновение для моего кода Pygame в течение нескольких часов, но ничего не работает Я исследовал много методов, но ни один из них не запал мне в голову, или есть ли какой-нибудь способ их реализовать.
Вот код:
import pygame
import random
pygame.init()
#SCREEN
screeenWidth = 320
screenHeight = 600
screen = pygame.display.set_mode((screeenWidth, screenHeight))
screenBackground = pygame.image.load('assets/background.png')
screen.blit(screenBackground, (0,0))
pygame.display.set_caption("Evading Game")
#CLOCK
clock = pygame.time.Clock()
#PLAYER
playerX = 20 #which is lane 1
playerY = screenHeight * 0.8
playerImage = pygame.image.load('assets/car_img.png')
lane_dictionary = {1:20,2:85,3:160,4:240}
player_current_lane = 1
#OBSTACLE
obstacle_current_lane = random.randint(1,4)
obstacleX = lane_dictionary[obstacle_current_lane]
obstacleY = 0
obstacleImage = pygame.image.load('assets/obstacle_img.png')
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT: #Lane 1 = 20, lane 2 = 80, lane 3 = 160, lane 4 = 240
if not player_current_lane == 4:
player_current_lane += 1
e = lane_dictionary[player_current_lane]
playerX = e
if event.key == pygame.K_LEFT: #Lane 1 = 20, lane 2 = 80, lane 3 = 160, lane 4 = 240
if not player_current_lane == 1:
player_current_lane -= 1
e = lane_dictionary[player_current_lane]
playerX = e
obstacleY += 1 #Obstacle movement
screen.blit(screenBackground, (0,0)) #Blit background
screen.blit(playerImage, (playerX, playerY)) #blit player
screen.blit(obstacleImage, (obstacleX, obstacleY)) #Blit obstacle
pygame.display.update()
clock.tick(60) #fps
pygame.quit()
quit()