我如何对许多不同的矩形进行碰撞检查? Pygame
How do I make a rect check for collisions with a lot of different rects? Pygame
如何检查一个矩形是否与许多不同的矩形发生碰撞?我正在制作一款平台游戏,我在一个平台上遇到了碰撞,但不知道如何添加多个平台。
我不知道如何通过变量添加更多的矩形。我唯一的另一个想法是制作一堆具有不同名称的矩形,并单独检查每个矩形。如果我在我的关卡中放置 30 个矩形,那将不会非常有效,而且它看起来像意大利面条。
我在网上搜索了有关如何执行此操作的信息,但我发现了完全不相关的问题!
如果相关的话,这里是代码。编辑:不再剪断,现在是整个代码。对不起,如果它很长,88 行。
import pygame
import math
# Defining important variables
pygame.init()
keys = pygame.key.get_pressed()
clock = pygame.time.Clock()
# LEVEL DESIGN
ground_rect = []
ground_rect.append(pygame.Rect(0, 200, 200, 50))
ground_rect.append(pygame.Rect(0, 200, 300, 100))
# Screen stuff
bg_color = 204, 255, 220
screen_y = 360
screen_x = 480
screen = pygame.display.set_mode((screen_x, screen_y))
# Player's img and start coordinates.
player_img = pygame.image.load("player.png").convert()
player_y = 0
player_x = 0
player_y_velocity = 0
player_x_velocity = 0
coyote = 0
def pic(img, x, y):
screen.blit(img, (x + 0, y + 0))
def main():
screen.fill(bg_color)
# Get the variables global
global player_y # Y position of player
global player_x # X position of player
global player_y_velocity # Y velocity of player
global player_x_velocity # X velocity of player
global coyote # Checks if you are on ground, and gives a bit of extra frames to jump if not.
# Inputs
if pygame.key.get_pressed()[pygame.K_UP] and coyote > 0:
player_y_velocity = -9
coyote = 0
# This makes your jump lower when you release!
if not pygame.key.get_pressed()[pygame.K_UP] and player_y_velocity < -1:
player_y_velocity = -1
# Move left/right when pressing left/right!
player_x_velocity = (pygame.key.get_pressed()[pygame.K_RIGHT] - pygame.key.get_pressed()[pygame.K_LEFT]) * 2.5
# Velocity effects
player_y_velocity += 0.4
player_y_velocity = min(player_y_velocity, 10)
player_x += player_x_velocity
player_y += player_y_velocity
coyote -= 1
# Rectangles
player_rect = pygame.Rect(player_x, player_y, 32, 32)
# !!Collision!!
for i in range(10):
if player_rect.colliderect(ground_rect):
player_y -= 1
player_y_velocity = 0.5
coyote = 4
player_rect = pygame.Rect(player_x, player_y, 32, 32)
# Draw stuff (pic is a function I made to shorten the blit)
pic(player_img, player_x, player_y)
pygame.draw.rect(screen, (64, 0, 0), ground_rect)
# Main script
while True:
main()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
if player_y > 376:
pygame.quit()
break
clock.tick(60)
编辑:我将包括我尝试制作地面矩形列表的方式以及我收到的错误消息。
# LEVEL DESIGN
ground_rect = []
ground_rect.append(pygame.Rect(0, 200, 200, 50))
ground_rect.append(pygame.Rect(0, 200, 300, 100))
这是我正在使用的 IDE Pycharm 向我抛出的错误。
Expected type 'Union[Rect, RectType, tuple[int, int, int, int], list[int], tuple[Union[tuple[float, float], list[float], Vector2, Vector2], Union[tuple[float, float], list[float], Vector2, Vector2]], list[Union[tuple[float, float], list[float], Vector2, Vector2]], _HasRectAttribute]', got 'list[Rect]' instead
这是回溯日志:
Traceback (most recent call last):
File "C:\Users\Matt\PycharmProjects\lettherebechaos\main.py", line 77, in <module>
main()
File "C:\Users\Matt\PycharmProjects\lettherebechaos\main.py", line 65, in main
if player_rect.colliderect(ground_rect):
TypeError: Argument must be rect style object
正如 Alian 所提到的,您必须使用列表来存储多个矩形(如果您想要的不仅仅是几个,则可以使用任何东西)。然后您可以遍历该列表并检查冲突。这是一个快速的新示例,因为您的代码没有提供任何相关内容。
import pygame
#make a list and add 30 rects in it
rects = []
for x in range(5):
for y in range(6):
rects.append(pygame.Rect(x * 50 + 10, y * 50 + 10, 40, 40))
pygame.init()
screen = pygame.display.set_mode((260, 310))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((255, 255, 255))
#go through all the rects and check for collision
for rect in rects:
color = (255, 0, 0)
if (rect.collidepoint(pygame.mouse.get_pos())):
color = (0, 255, 0)
pygame.draw.rect(screen, color, rect)
pygame.display.update()
如何检查一个矩形是否与许多不同的矩形发生碰撞?我正在制作一款平台游戏,我在一个平台上遇到了碰撞,但不知道如何添加多个平台。
我不知道如何通过变量添加更多的矩形。我唯一的另一个想法是制作一堆具有不同名称的矩形,并单独检查每个矩形。如果我在我的关卡中放置 30 个矩形,那将不会非常有效,而且它看起来像意大利面条。
我在网上搜索了有关如何执行此操作的信息,但我发现了完全不相关的问题!
如果相关的话,这里是代码。编辑:不再剪断,现在是整个代码。对不起,如果它很长,88 行。
import pygame
import math
# Defining important variables
pygame.init()
keys = pygame.key.get_pressed()
clock = pygame.time.Clock()
# LEVEL DESIGN
ground_rect = []
ground_rect.append(pygame.Rect(0, 200, 200, 50))
ground_rect.append(pygame.Rect(0, 200, 300, 100))
# Screen stuff
bg_color = 204, 255, 220
screen_y = 360
screen_x = 480
screen = pygame.display.set_mode((screen_x, screen_y))
# Player's img and start coordinates.
player_img = pygame.image.load("player.png").convert()
player_y = 0
player_x = 0
player_y_velocity = 0
player_x_velocity = 0
coyote = 0
def pic(img, x, y):
screen.blit(img, (x + 0, y + 0))
def main():
screen.fill(bg_color)
# Get the variables global
global player_y # Y position of player
global player_x # X position of player
global player_y_velocity # Y velocity of player
global player_x_velocity # X velocity of player
global coyote # Checks if you are on ground, and gives a bit of extra frames to jump if not.
# Inputs
if pygame.key.get_pressed()[pygame.K_UP] and coyote > 0:
player_y_velocity = -9
coyote = 0
# This makes your jump lower when you release!
if not pygame.key.get_pressed()[pygame.K_UP] and player_y_velocity < -1:
player_y_velocity = -1
# Move left/right when pressing left/right!
player_x_velocity = (pygame.key.get_pressed()[pygame.K_RIGHT] - pygame.key.get_pressed()[pygame.K_LEFT]) * 2.5
# Velocity effects
player_y_velocity += 0.4
player_y_velocity = min(player_y_velocity, 10)
player_x += player_x_velocity
player_y += player_y_velocity
coyote -= 1
# Rectangles
player_rect = pygame.Rect(player_x, player_y, 32, 32)
# !!Collision!!
for i in range(10):
if player_rect.colliderect(ground_rect):
player_y -= 1
player_y_velocity = 0.5
coyote = 4
player_rect = pygame.Rect(player_x, player_y, 32, 32)
# Draw stuff (pic is a function I made to shorten the blit)
pic(player_img, player_x, player_y)
pygame.draw.rect(screen, (64, 0, 0), ground_rect)
# Main script
while True:
main()
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
if player_y > 376:
pygame.quit()
break
clock.tick(60)
编辑:我将包括我尝试制作地面矩形列表的方式以及我收到的错误消息。
# LEVEL DESIGN
ground_rect = []
ground_rect.append(pygame.Rect(0, 200, 200, 50))
ground_rect.append(pygame.Rect(0, 200, 300, 100))
这是我正在使用的 IDE Pycharm 向我抛出的错误。
Expected type 'Union[Rect, RectType, tuple[int, int, int, int], list[int], tuple[Union[tuple[float, float], list[float], Vector2, Vector2], Union[tuple[float, float], list[float], Vector2, Vector2]], list[Union[tuple[float, float], list[float], Vector2, Vector2]], _HasRectAttribute]', got 'list[Rect]' instead
这是回溯日志:
Traceback (most recent call last):
File "C:\Users\Matt\PycharmProjects\lettherebechaos\main.py", line 77, in <module>
main()
File "C:\Users\Matt\PycharmProjects\lettherebechaos\main.py", line 65, in main
if player_rect.colliderect(ground_rect):
TypeError: Argument must be rect style object
正如 Alian 所提到的,您必须使用列表来存储多个矩形(如果您想要的不仅仅是几个,则可以使用任何东西)。然后您可以遍历该列表并检查冲突。这是一个快速的新示例,因为您的代码没有提供任何相关内容。
import pygame
#make a list and add 30 rects in it
rects = []
for x in range(5):
for y in range(6):
rects.append(pygame.Rect(x * 50 + 10, y * 50 + 10, 40, 40))
pygame.init()
screen = pygame.display.set_mode((260, 310))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((255, 255, 255))
#go through all the rects and check for collision
for rect in rects:
color = (255, 0, 0)
if (rect.collidepoint(pygame.mouse.get_pos())):
color = (0, 255, 0)
pygame.draw.rect(screen, color, rect)
pygame.display.update()