简体   繁体   English

Pygame代码不适用于Sprite组中的每个Sprite

[英]Pygame code not applying for every sprite in sprite group

For my pygame tower defence program, I am trying to figure out how to implement collision with other towers that are already placed on the map. 对于我的pygame塔防程序,我试图弄清楚如何与已经放置在地图上的其他塔进行碰撞。

The placement process creates a 48x48 preview sprite that moves in a grid on the players mouse when it is on the game screen. 放置过程会创建一个48x48预览精灵,当它在游戏屏幕上时,它将在玩家鼠标上的网格中移动。 Tower sprites are kept in a group called tower_sprites . 塔精灵保留在称为tower_sprites的组中。 I have this bit of code to check if the sprite is colliding with certain terrain tiles and tower sprites (this is in the sprite update function): 我有这段代码来检查子画面是否与某些地形图块和塔式子画面发生碰撞(这在子画面更新功能中):

if y < C.map_width and x < C.map_height: #if preview sprite coords are on the game screen (x and y are rounded coords of the preview tower at certain points as each tile is 16x16 while the tower is 48x48)
    if live_grid.tilemap[x][y] != 0: #if the coordinate is not grass
        self.map_collide.append(1)
    else:                            #if the coordinate is grass
        self.map_collide.append(0) 
    for sprite_object in tower_sprites:         #for each sprite in tower_sprites
        print (sprite_object)
        if sprite_object.rect.colliderect(self.rect):   #if the rect of the preview sprite collides with a tower sprites rect
            self.tower_collision = True
        else:
            self.tower_collision = False

The for loop onward section is meant to check if the preview sprite is colliding with any tower sprites rect. 从for循环开始的部分旨在检查预览精灵是否与任何塔精灵rect发生冲突。 If so it sets a variable to true or otherwise, False. 如果是这样,它将变量设置为true或其他,否则为False。

The program then checks to see if self.tower_collision is true in a different function within the class and if so, returns a value back to the main game loop: 然后,程序检查类中不同函数中的self.tower_collision是否为true,如果是,则将值返回主游戏循环:

def collide_boolean(self):
    if 1 in self.map_collide: #for map collision
        del self.map_collide[:]
        return False #False = can't place

    if self.tower_collision == True: #for tower collision
        return False

    else:   #for map collision
        del self.map_collide[:]
        return True

The issue this code produces however is that it only checks if the last sprite the player placed collides with the preview sprite, and not others before it. 但是,此代码产生的问题是,它仅检查播放器放置的最后一个精灵是否与预览精灵发生冲突,而不检查它之前的其他精灵。 I want the program to check all tower sprites but im not sure how to do this. 我希望程序检查所有塔精灵,但不确定如何执行此操作。

What do i need to do to change this? 我需要做些什么来改变这个? If you want a full dropbox of the program so far, just ask. 如果您想要到目前为止该程序的完整保管箱,请询问。

EDIT: Dropbox link https://www.dropbox.com/sh/ajwlkwufrxadoqo/AAASwxQPK8gFG-0zixfc2fC1a?dl=0 编辑:Dropbox链接https://www.dropbox.com/sh/ajwlkwufrxadoqo/AAASwxQPK8gFG-0zixfc2fC1a?dl=0

除非与之碰撞的精灵是tower_sprites的最后一个精灵,否则循环中的后续迭代将再次用False覆盖self.tower_collision

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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