如何在 pygame 中应用碰撞?我已经看到了很多,但没有什么能记住
How can you apply collision in pygame? I have seen a lot but nothing is sticking to mind
我迫切需要帮助。几个小时以来,我一直在尝试为我的 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()
创建一个 pygame.Rect
对象,大小为 playerImage
,玩家位置为(playerX
,playerY
)。
创建第二个pygame.Rect
,大小为obstacleImage
,障碍物位置为(obstacleX
,obstacleY
)。
使用 colliderect
检测两个矩形之间的碰撞。例如:
while running:
# [...]
playerRect = playerImage.get_rect(topleft = (playerX, playerY))
obstacleRect = obstacleImage.get_rect(topleft = (obstacleX, obstacleY))
if playerRect.colliderect(obstacleRect):
print("hit")
# [...]
我迫切需要帮助。几个小时以来,我一直在尝试为我的 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()
创建一个 pygame.Rect
对象,大小为 playerImage
,玩家位置为(playerX
,playerY
)。
创建第二个pygame.Rect
,大小为obstacleImage
,障碍物位置为(obstacleX
,obstacleY
)。
使用 colliderect
检测两个矩形之间的碰撞。例如:
while running:
# [...]
playerRect = playerImage.get_rect(topleft = (playerX, playerY))
obstacleRect = obstacleImage.get_rect(topleft = (obstacleX, obstacleY))
if playerRect.colliderect(obstacleRect):
print("hit")
# [...]