Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

python - Collision for Pygame Game Map

I am trying to make a maze game in Pygame but am unable to achieve collision for the 1 (maze wall) in the array. I tried to put the collision detection in the loop creating the map but it is not working. I also put the collision detection in the main loop but only the top left rect detected the collision, not all the 1 rects. How would I go about fixing this? Thank you!

import pygame
pygame.init()

screen = pygame.display.set_mode((700,700))
pygame.display.set_caption("Game")

speed = 20
x = 200
y = 600


def game_map():

    global rect_one
    surface = pygame.Surface((100, 100), pygame.SRCALPHA)
    rect_one = pygame.draw.rect(surface, (0, 0, 255), (0, 0, 50, 50)) 

    global rect_two
    surface_one = pygame.Surface((80, 80), pygame.SRCALPHA)
    rect_two = pygame.draw.rect(surface_one, (255, 255, 255), (0, 0, 50, 50)) 

    tileX = 0
    tileY = 0

    global tile_list
    tile_list = []
    map = [
            [1,1,1,1,1,1,1,1,1,1,1,1,1],
            [1,0,0,0,1,0,1,0,0,0,0,0,1],
            [1,0,1,0,0,0,1,0,1,1,1,0,1],
            [1,0,0,0,1,1,1,0,0,0,0,0,1],
            [1,0,1,0,0,0,0,0,1,1,1,0,1],
            [1,0,1,0,1,1,1,0,1,0,0,0,1],
            [1,0,1,0,1,0,0,0,1,1,1,0,1],
            [1,0,1,0,1,1,1,0,1,0,1,0,1],
            [1,0,0,0,0,0,0,0,0,0,1,0,1],
            [1,1,1,1,1,1,1,1,1,1,1,1,1]
            ]

    for y, row in enumerate(map):
        tileX = 0
        for x, cell in enumerate(row):
            image = surface if cell == 1 else surface_one
            screen.blit(image, [x*50, y*50]) 
            tile_list.append(rect_one)
    pygame.display.update() 


def player():
    player = pygame.draw.rect(screen, (255,0,0), (x, y, 20, 20))   

    for i in tile_list:
        if player.colliderect(i):
            print("hello")

loop = True

while loop:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = False

    #player controls
    keys = pygame.key.get_pressed()
    
    if keys[pygame.K_LEFT]:
        x -= speed
    if keys[pygame.K_RIGHT]:
        x += speed
    if keys[pygame.K_UP]:
        y -= speed
    if keys[pygame.K_DOWN]:
        y += speed

    screen.fill((255,255,255))
    game_map()
    player()
    pygame.display.update()


pygame.quit()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your tile_list only contains one Rect multiple times.

I simplified your code a little bit and use a Rect with the correct coordinates for each 1 in your map. Also note the comments:

import pygame
pygame.init()

screen = pygame.display.set_mode((700,700))
pygame.display.set_caption("Game")

speed = 10
player_x = 200
player_y = 600

# Use a constant. There's not need to make big Surfaces and then draw a smaller rect on them to create the map.
TILESIZE = 50

tile_list = []
map = [
        [1,1,1,1,1,1,1,1,1,1,1,1,1],
        [1,0,0,0,1,0,1,0,0,0,0,0,1],
        [1,0,1,0,0,0,1,0,1,1,1,0,1],
        [1,0,0,0,1,1,1,0,0,0,0,0,1],
        [1,0,1,0,0,0,0,0,1,1,1,0,1],
        [1,0,1,0,1,1,1,0,1,0,0,0,1],
        [1,0,1,0,1,0,0,0,1,1,1,0,1],
        [1,0,1,0,1,1,1,0,1,0,1,0,1],
        [1,0,0,0,0,0,0,0,0,0,1,0,1],
        [1,1,1,1,1,1,1,1,1,1,1,1,1]
        ]

# let's create a single Surface for the map and reuse that
grid = pygame.Surface((len(map[0]) * TILESIZE, len(map) * TILESIZE), pygame.SRCALPHA)
for y, row in enumerate(map):
    for x, cell in enumerate(row):

        # if we want a wall, we draw it on the new Surface
        # also, we store the Rect in the tile_list so collision detection works
        if cell:
            rect = pygame.draw.rect(grid, 'blue', (x*TILESIZE, y*TILESIZE, TILESIZE, TILESIZE))
            tile_list.append(rect)

loop = True
clock = pygame.time.Clock()
while loop:
    clock.tick(60)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = False

    #player controls
    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT]:
        player_x -= speed
    if keys[pygame.K_RIGHT]:
        player_x += speed
    if keys[pygame.K_UP]:
        player_y -= speed
    if keys[pygame.K_DOWN]:
        player_y += speed

    screen.fill((255,255,255))

    # draw the map surface to the screen
    screen.blit(grid, (0 ,0))
    player = pygame.draw.rect(screen, (255,0,0), (player_x, player_y, 20, 20))   

    # now collision detection works because for each 1 in the map
    # there's a Rect in tile_list with the correct coordinates
    for i in tile_list:
        if player.colliderect(i):
            print("colliding")
            break
    else:
        print('not colliding')
        
    pygame.display.update()


pygame.quit()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...