简体   繁体   中英

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM