python - Pygame 查询 - 在同一类中生成多个 Sprite

标签 python pygame collision sprite

不重要的序言:

您好,我正在使用 Python 和 Pygame 来创建游戏。这是为了提高我的编程技能,而不是认真尝试游戏创作。最近我为了 Objective C 而暂时离开了 Python,但现在我又回到了它。这是我短暂休息之前遇到的一个问题,现在又回到了一个原本让我困惑的问题。我花了很长时间研究它,我怀疑我已经找到了解决方案,但只是未能理解它。我对一些不好的命名约定/缺乏评论表示歉意。我正在努力改进这一点。

问题实质:

无论如何,我已经附上了我正在使用的四张图像。该程序使用一个简单的函数来在屏幕上定位各种图 block 。鼠标光标是一把剑。这是整个图像,但我稍后会更改它。每当光标与 Tile 碰撞时,我都会在 shell 中将程序类型设置为“蓝色”。我的目标是当它与该颜色的“任何”图 block 碰撞时发生这种情况。

从长远来看,我希望能够修改这些图 block Sprite 的属性。各种游戏部件会相互作用,我需要保存每个 Sprite 的状态。我还会为其他 Sprite 设置交互。

现在 Sprite 都在生成图像,但我的图 block 碰撞矩形在生成每个图像后只是移动。我认为考虑到代码,这是有意义的,但我需要一种处理多个 Sprite 的方法,每个 Sprite 都有一个用于碰撞的矩形。

谢谢

编辑:由于新用户限制,我无法添加图像。它们可用 enter link description here我想我在某处读到人们可以(并且确实)在这里编辑帖子。因此,如果有人能够将图像移动到此线程中,欢迎这样做。

import random,math,sys,os
import pygame
from pygame.locals import *

pygame.init() #Initializing Pygame

#Colors
black=(0,0,0)

#Screen
screen=pygame.display.set_mode((1200,800),0,0)
pygame.display.set_caption("Nero's Sandbox")
pygame.mouse.set_visible(False)
clock=pygame.time.Clock()
fps=40

#Game Functions:

def terminate():
    pygame.quit()
    sys.exit()

def numgen(x,y):
    return random.randint(x,y)

#Loop Variables
tri=2

#Groups:

allsprites = pygame.sprite.Group()
alltiles = pygame.sprite.Group()
allmice = pygame.sprite.Group()

#Mouse Classes

class Pointy(pygame.sprite.DirtySprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load('redsword.png').convert() #31x32 image
        self.image.set_colorkey(black)
        self.rect=self.image.get_rect()
        self.set=pygame.sprite.Group()
        self.add(allmice, allsprites, self.set)
        pygame.sprite.RenderPlain((self.set,allmice,allsprites))

    def update(self):
        screen.fill(black)
        alltiles.draw(screen)
        if event.type == pygame.MOUSEMOTION:
            pos = pygame.mouse.get_pos()
            self.rect.topright = pos
            self.set.draw(screen)

#Tile Sprites - only one rect is being recognized.

class Tile(pygame.sprite.Sprite):
    def __init__(self, graphic):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(graphic).convert()
        self.image = pygame.transform.scale((self.image),(50,50))
        self.rect=self.image.get_rect()
        self.add(alltiles, allsprites)
        self.set=pygame.sprite.RenderPlain((self))

    def update(self, x, y):
        pos = (x,y)
        self.rect.topleft = pos

#Micers
pointy1=Pointy()

#Game Loops

while True:  #Ensures all loops within program are constantly called when conditions are met.  
    screen.fill(black)
    while tri==2:                                   
        for event in pygame.event.get():
            if event.type == QUIT:
                terminate()
        pygame.display.flip()
        x = 0
        y = 50
        w = 0
        while  x!=600:
               x=x+50
               w = w+1
               if w%2==0:
                    purpletile1=Tile('purplesquare.png')
                    purpletile1.set.update(x,y)
                    purpletile1.set.draw(screen)
               else:
                   c=numgen(1,2)
                   if c==1:
                           bluetile1=Tile('lightbluesquare.png')
                           bluetile1.set.update(x,y)
                           bluetile1.set.draw(screen)
                   if c==2:
                           redtile1=Tile('redsquare.png')
                           redtile1.set.update(x,y)
                           redtile1.set.draw(screen)
               if x>=600 and y!=450:
                    if y<450:
                        x = 0
                        y = y+50
                        w=w-1
                    if y>=450:
                       tri=3

    while tri==3:
        for event in pygame.event.get():
           if event.type == QUIT:
               terminate()
           alltiles.draw(screen)
           pointy1.set.update()
           pointy1.set.draw(screen)
           pygame.display.flip()
           clock.tick(fps)
           if pygame.sprite.collide_rect(pointy1,bluetile1):
               print('blue')

最佳答案

我自己也遇到了同样的问题!我做了一些调试,看来我的类的所有实例都共享同一个 pygame.Rect() 实例

您可能想要更改线路:

pygame.sprite.Sprite.__init__(self)

super.__init__(self)

这样,pygame.sprite.Sprite 的 init 就会为你的图 block 设置属性。我可能是错的,我并不完全熟悉 python 的继承语法,但这就是我的做法。

我也可能是 get_rect 导致所有类使用相同的矩形,但这似乎不太可能。

我希望我能提供一些帮助,并且记住 pygame.Rect 是一个对象,所以不知何故你只实例化一次。

关于python - Pygame 查询 - 在同一类中生成多个 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7657446/

相关文章:

python - 如何正确使用 tf.nn.max_pool_with_argmax

python - 将 `http_conn_id` 传递给 SlackWebhookOperator 时出现问题

python - '鸡'对象没有属性 'rect'

java - 椭圆形碰撞检测无法正常工作

python - 在Python中根据字符串值创建分类列

python - Pygame 中的顺序图像呈现

python - 图像出现在屏幕上,但不随按键事件移动

python - 碰撞检测问题 - PyGame

ios - 让 SKPhysicsBody 变得坚不可摧?

Java 碰撞检测未按预期运行