[英]How to create free game collision with pyglet?
I have started working on a 2d game that is comparable to Terraria in terms of what I am dealing with. 就我正在处理的东西而言,我已经开始研究与Terraria相当的2d游戏。 My problem is as follows The world is made of tiles/blocks then there is the player.
我的问题如下:世界由瓷砖/砖块组成,然后有玩家。 I can't seem to find a way to make the player collide with the tiles, why not a set ground level?
我似乎找不到找到使玩家与瓷砖碰撞的方法,为什么不设置地面水平? because if there isn't a tile under the player he should fall and what if the players on a hill?
因为如果玩家下方没有瓷砖,他应该掉下来,而如果玩家在山上怎么办? So long story short I need a way to make the player collide with the tile beneath them.
长话短说,我需要一种使玩家与他们下面的瓷砖碰撞的方法。
So here is what I have tried: making a simplified grid to verify if the is a tile below, a for loop where I compared all the tiles to the players feet but this was too much data to feed and I couldn't find a way to say check the tile under, above, left, and right of the player and only those positions. 所以,这就是我尝试的方法:制作一个简化的网格以验证the是否是下面的图块,一个for循环,在该循环中,我将所有图块与玩家的脚进行了比较,但该数据太多,无法提供数据,我找不到办法也就是说要检查玩家下方,上方,左侧和右侧的磁贴,以及仅查看那些位置。 I am going to try to show useful code but it will be a bit jumbled seeing as I cant pas't all the files in.
我将尝试显示有用的代码,但由于无法粘贴所有文件,因此会有些混乱。
class Physics:
@classmethod
def fall(cls, dt, o, world, mod):
if world.grid_fill[(o.grid_pos[0], o.grid_pos[1]-1)] == "Filled":
o.falling = False
else:
o.y -= o.height//8*dt
o.update_pos(o.x, o.y)
@classmethod
def run_physics(cls, dt, o, world):
for obj in o:
Physics.fall(dt, obj, world, None)
# player and Physics are in 2 different files as is world and the main loop
class Player:
def __init__(self, sprite, x, y, world):
self.img = sprite
self.img.set_position(x, y)
self.key_handler = key.KeyStateHandler()
self.type = "Player"
self.x = x
self.y = y
self.pos = (self.x, self.y)
self.grid_pos = world.pix_cords[self.pos]
self.width = self.img.width
self.height = self.img.height
self.speed = self.width//4
self.falling = False
def update_pos(self, x, y):
self.img.set_position(x, y)
def draw(self):
self.img.draw()
def check_fall(self, tile):
if self.y - self.height//2 == tile.y + tile.height:
return False
else:
return True
def update(self):
if self.key_handler[key.A]:
self.x -= self.speed
if self.key_handler[key.D]:
self.x += self.speed
if self.key_handler[key.W]:
self.y += self.speed
self.update_pos(self.x, self.y)
Before you say I didn't show how the ground is made that is because I can explain this by just saying it is made using a batch draw and all sprite cornets are centered to the sprite. 在您说之前,我没有显示地面是如何形成的,这是因为我可以通过仅说说它是使用批处理绘制并且所有子画面小锥角都位于该子画面居中来解释这一点。 Alright, when I run this code the player falls indefinitely but I want it to stop when there is a tile under it.
好的,当我运行这段代码时,播放器会无限期掉落,但是当它下面有一块瓷砖时,我希望它停止。
If you need to see the batch for the tile I will but I don't know if that is needed and if you see anything unprofessional about how this is written I am always willing to learn how to write more professionally. 如果您需要查看磁贴的批处理,但我不知道是否需要,并且如果您看到关于此如何写的任何非专业知识,我总是乐于学习如何更专业地编写。
This was a stupid question. 这是一个愚蠢的问题。 Just need to make a simplified positioning system based on the blocks, not sprites.
只需要基于块而不是精灵创建一个简化的定位系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.