使用基于地图的碰撞检测Pygame滚动背景

时间:2019-05-01 23:17:54

标签: python pygame

我正在创建一个使用地图进行碰撞的游戏,在我尝试向游戏中添加滚动条之前,所有这些操作一直有效。应该让背景移动,而不是让播放器移动,但是这样做是在碰撞检测中。

每个碰撞点的大小从16乘16到1乘1。 事实证明,这是很成问题的,对解决它的任何帮助将不胜感激

我尝试更改了玩家的碰撞值,屏幕尺寸,但最终我不确定导致此问题的确切原因是什么,我最好的猜测是我移除了玩家的动作,而将其保留在屏幕上相同的位置导致了碰撞应该如何随位置变化而发生变化的问题,但这不能解释碰撞点的缩小。

import pygame, sys

clock = pygame.time.Clock()

from pygame.locals import *
pygame.init() # initiates pygame

pygame.display.set_caption('The Game')

WINDOW_SIZE = (600,400)

screen = pygame.display.set_mode(WINDOW_SIZE,0,32)

display = pygame.Surface((300,200))

moving_right = False
moving_left = False
moving_up = False
moving_down = False

game_map = [['2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'],
           ['2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'],
           ['2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'],
           ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
           ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
           ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
           ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
           ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
           ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
           ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'],
           ['2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'],
           ['2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2','2'],
           ['0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0']]

player_rect = pygame.Rect(100,100,16,16)
black = (45,45,45)
black1 = (60,60,60)

def collision_test(rect,tiles):
   hit_list = []
   for tile in tiles:
       if rect.colliderect(tile):
           hit_list.append(tile)
   return hit_list

def move(rect,movement,tiles):
   collision_types = {'top':False,'bottom':False,'right':False,'left':False}
   rect.x += movement[0]
   hit_list = collision_test(rect,tiles)
   for tile in hit_list:
       if movement[0] > 0:
           rect.right = tile.left
           collision_types['right'] = True
       elif movement[0] < 0:
           rect.left = tile.right
           collision_types['left'] = True
   rect.y += movement[1]
   hit_list = collision_test(rect,tiles)
   for tile in hit_list:
       if movement[1] > 0:
           rect.bottom = tile.top
           collision_types['bottom'] = True
       elif movement[1] < 0:
           rect.top = tile.bottom
           collision_types['top'] = True
   return rect, collision_types

while True:
   display.fill((155,155,155))
   tile_rects = []
   y = 0
   for layer in game_map:
       x = 0
       for tile in layer: # tiles
           if tile == '2':
               pygame.draw.rect(display,black,(x*16-player_rect.x,y*16-player_rect.y,16,16))
           if tile != '0':
               tile_rects.append(pygame.Rect(x*16-player_rect.x,y*16-player_rect.y,16,16))
           x += 1
       y += 1

   player_movement = [0,0]
   if moving_right == True:
       player_movement[0] += 2
   if moving_left == True:
       player_movement[0] -= 2
   if moving_up == True:
       player_movement[1] -= 2
   if moving_down == True:
       player_movement[1] += 2
   player_rect,collisions = move(player_rect,player_movement,tile_rects)

   pygame.draw.rect(display,black1,(142,100,16,16)) #replacing the 142 and 100 with the rect.x and y, and remove the rect.x and y from the tiles, will make it work like the original


   for event in pygame.event.get(): # event loop
       if event.type == QUIT:
           pygame.quit()
           sys.exit()
       if event.type == KEYDOWN:
           if event.key == K_RIGHT:
               moving_right = True
           if event.key == K_LEFT:
               moving_left = True
           if event.key == K_UP:
               moving_up = True
           if event.key == K_DOWN:
               moving_down = True
       if event.type == KEYUP:
           if event.key == K_RIGHT:
               moving_right = False
           if event.key == K_LEFT:
               moving_left = False
           if event.key == K_UP:
               moving_up = False
           if event.key == K_DOWN:
               moving_down = False
   screen.blit(pygame.transform.scale(display,WINDOW_SIZE),(0,0))
   pygame.display.update()
   clock.tick(60)

我的预期结果是,碰撞点不会改变,而是保持16乘16的比例,不幸的是,这些图块的碰撞点已缩小为1乘1的比例,导致很多问题。

1 个答案:

答案 0 :(得分:3)

您的代码有点难以理解,但是我已经知道问题出在哪里。

move函数中,更改player_rect的坐标以使其移动。但是,当您绘制它(pygame.draw.rect(display,black1,(142,100,16,16)))时,您将其绘制在中间。因此,您绘制的内容与经过移动测试的内容之间不匹配。

老实说,我不改变很多代码就找不到解决方法。
如果要保持背景滚动,请考虑不要移动播放器,而是朝相反的方向移动磁贴。我的意思是,如果玩家向左走,则向右移动磁贴,依此类推。
当前,您在每次迭代中创建图块。我建议在主循环之外创建它们,并在循环中移动它们(编辑其rect的坐标)。