在pygame中制造玩家不应该穿过的障碍物的问题



我已经用pygame学习使用Python大约一个月了。我想实现我的第一个小项目,这是一个类似塞尔达的游戏,你可以导航一只小章鱼,躲避敌人(后来也可以战斗(并收集宝藏。

我可以移动我的角色,敌人也可以自己走。我可以使用rect方法来检测玩家和敌人之间的碰撞,也可以通过走过一个矩形来切换到关卡中的另一个部分(地图(。

现在我想创造一个你不能通过的障碍物(棕榈树、岩石等(,玩家应该被迫绕过这些障碍物。

我已经学会了如何剪裁角色(章鱼(接触障碍物(棕榈树(的位置/侧面(上、下、右、左(。但我的章鱼还是会穿过棕榈树。

def collision(source, target):
if not source.colliderect(target): return
overlap = source.clip(target)
if overlap.width > overlap.height:     #verticale collision
if source.y < target.y:            #top
source.bottom = target.top
#print ("oben")
else:
source.top = target.bottom
#print ("Tako_oben =" + str(source.top) + "Palme UNTEN =" + str(target.bottom))
print("Tako_oben =" + str(source.top) + "Tako_unten =" + str(source.bottom))
#print("unten")
else:                                  # horizontale collision
if source.x < target.x:            #linke Seite
source.right = target.left
print("links")
else:
source.left = target.right
#print("rechts")

这是我调用函数的地方:

class Map :
def __init__(self,game, x, y):
self.game = game #stellt Bezug zum Spiel
#self.tako_chan = Tako_Chan(self,x,y)

self.x = x  #wird in unteren Methode () verwendet
self.y = y  #wird in unteren Methode () verwendet


self.map_01 = pygame.image.load("map_01.PNG")
self.map_02 = pygame.image.load("map_02.PNG")
self.warp_point = pygame.Rect(630, 620, 150,80 ) #x y width height
self.warp_point_2 = pygame.Rect(500, -70, 150, 80)  # x y width height
self.palm_rec = pygame.Rect(450, 300, 60, 150)  # x y width height

def update (self):


if self.game.location == "01":
self.game.screen.blit(self.map_01, (self.x, self.y))  # Bild/Position Vorgänger
pygame.draw.rect(self.game.screen, (0, 255, 0), self.warp_point, 4)
pygame.draw.rect(self.game.screen, (150, 200, 0), self.palm_rec, 5)
self.game.snake.move_horizon()
self.game.snake.update()
if self.game.tako_chan.tako_chan_rect.colliderect(self.palm_rec):

None


collision (self.game.tako_chan.tako_chan_rect, self.palm_rec)

如果您在pygame中需要碰撞,您可以使用pygame.Rect()方法,该方法包含3个参数(x、y、width、height(,在该方法中,您可以检测玩家是否击中了玩家的一侧。

pygame中i代码冲突的方式是首先检测两个矩形之间的冲突,在你的情况下是玩家和棕榈树(如果我没有弄错的话(

我不知道你是如何创造球员运动的,所以我只想向你展示我的方式,希望它能有所帮助:

import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))
x,y = 50,50
run = True
player_speed = 1
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
run = False
key = pygame.key.get_pressed()
if key[pygame.K_a]:
x -= player_speed
if key[pygame.K_d]:
x += player_speed
player_rect = pygame.Rect(x,y,player_width,player_height)
palm_tree_rect = pygame.Rect(palm_x,palm_y,palm_width,palm_height)
if player_rect.colliderect(palm_tree_rect):
if player_rect.right >= palm_tree_rect.x and player_rect.right <= palm_tree_rect.x + (player_speed + 2) :
player_rect.x = palm_tree_rect.x - player_rect.width
if player_rect.left >= palm_tree_rect.right - (player_speed + 2) and player_rect.left <= palm_tree_rect.right:
player_rect.x = palm_tree_rect.right
if player_rect.top <= palm_tree_rect.bottom and player_rect.top >= palm_tree_rect.bottom - (player_speed + 2):
player_rect.y = palm_tree_rect.bottom
if player_rect.bottom >= palm_tree_rect.top and player_rect.bottom <= palm_tree_rect.top + (player_speed + 2):
player_rect.y = palm_tree_rect.top - player_rect.height
pygame.display.flip()

我把player_speed+2放在这里的唯一原因是,例如,你检测到从区块到玩家的5个像素之间的碰撞,而玩家每秒移动大约10个像素,你无法检测到从块到玩家的五个像素碰撞,因为玩家没有达到5个像素,所以它会在检测到之前进入区块。

希望这有帮助:(享受游戏。

此外,我有一个像pygame一样创建的引擎,名为pyeasygame,你可以在命令提示符下的pip install pyeasygame安装它,你可以自由使用它,它可以像创建播放器、冲突框、写一行代码一样工作,如果你在使用我的引擎时有任何困惑,如果你确实使用它,通过键入cmd和pip install pyeasygame来定位文件,它会说它已经安装在。。。\站点包,转到文件资源管理器中的那个位置,找到名为pyeasygame的文件夹并打开它,找到文件init.py,在那里你会找到所有的代码,这应该不难理解,因为它也是用pygame编写的。

最新更新