如何在pygam中制作尺寸相同但位置不同的多个块

2024-06-16 08:23:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试开始一个基本的二维塔防游戏。我想通过在棕色背景上放置几十个35x35绿色方块来设计地图。如何使用一个类来制作多个具有相同尺寸和颜色但位置不同的块?谢谢

    import pygame
    pygame.init()
    white = (255,255,255)
    black = (0,0,0)
    grass = (51,204,51)
    dirt = (192,151,111)
    gameDisplay = pygame.display.set_mode((1200,800))
    pygame.display.set_caption('Tower Defense')
    pygame.display.update()


    gameExit = False

    while not gameExit:
        class Player(object):
            def __init__(self):
                self.rect = pygame.draw.rect((64, 54, 16, 16))

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            #print(event)
        gameDisplay.fill(dirt)
        pygame.draw.rect(gameDisplay,green, [600,400,35,35])
        pygame.display.update()
    pygame.quit()
    quit()

Tags: rectselfevent游戏initdisplayupdatepygame
1条回答
网友
1楼 · 发布于 2024-06-16 08:23:02

你需要使用一个精灵组,它可以容纳任意数量的精灵。使用游戏.pyrect可以将每个精灵设置为不同的坐标。 下面是一个例子:

import pygame
pygame.init()
white = 255,255,255
black = 0,0,0
grass = 51,204,51w
dirt = 192,151,111
gameDisplay = pygame.display.set_mode((1200,800))
pygame.display.set_caption('Tower Defense')

class Player(pygame.sprite.Sprite):
        def __init__(self):
            self.image = pygame.Surface((35,35))
            self.image.fill(color)
            self.rect = self.image.get_rect()
Players = pygame.sprite.Group()

for i in range(#How many of them you want):
    player = Player()
    player.rect.x = i*35
    player.rect.y = i*35
    Players.add(player)

while not gameExit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True
        #print(event)
    gameDisplay.fill(dirt)
    Players.draw(window)
    pygame.display.update()
pygame.quit()
quit()

这里发生的是,我们创建了一个名为“Player”的类,在pygame中使用sprite模块,然后在末尾添加了一行:

^{pr2}$

允许自由编辑对象的x和y坐标

然后我们制作了一个精灵组,它可以容纳我们想要的任意数量的这些物体。好吧,一切都很顺利。现在我们使用for循环创建任意数量的精灵(您可以用需要的这些播放器的数量替换hashtag)。行:

player.rect.x = i*35

定义对象的x rect,由于i是每次for循环运行时都会更改的变量,因此它将类似于:

#first loop in the for loop:
player.rect.x = 0*35 = 0 #The x axis is 0

#second loop in the for loop
player.rect.x = 1*35 = 35 #The x axis is 35

#third loop in the for loop
player.rect.x = 2*35 = 70#The x axis is 70

等等。。。 这同样适用于y轴。我不太明白你说的用对象“填满整个屏幕”是什么意思,因为那样只会造成一个空白的背景。如果要为每个对象设置不同的特定坐标,请使用以下内容替换for循环,并使用x和y坐标替换hashtags:

player = Player()
player.rect.x = #x coordinate
player.rect.y = #y coordinate
Players.add(player)

您必须重复以下代码多次,因此,如果您想要三个具有指定坐标的不同对象,则必须执行以下操作:

player = Player()
player.rect.x = #x coordinate
player.rect.y = #y coordinate
Players.add(player)

player.rect.x = #x coordinate
player.rect.y = #y coordinate
Players.add(player)

player.rect.x = #x coordinate
player.rect.y = #y coordinate
Players.add(player)

这是一场漫无目的的闲谈,所以简单地说:

  • 你补充道自校正= self.image.get-rect()到你创建的任何精灵类,允许它的rect被更改
  • 要更改它,请使用:“播放器.rect.x=#x“和”播放器.rect.y=#y“
  • 如果你想在屏幕上放置这些对象,你可以使用一个for循环,就像上面第一个例子中显示的那样,它在屏幕上对角设置这些对象
  • 如果对象的设置位置没有任何关系,您可以简单地使用第6个代码示例中显示的代码,根据您想要的对象数量,重复任意多次

很抱歉,如果这太难理解了,但我希望它有帮助!在

注意:确保在创建类时使用游戏精灵精灵精灵,而不是object,就像您在代码中所做的那样。在

相关问题 更多 >