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

Python 如何在pygame中制作尺寸相同但位置不同的多个块,python,class,pygame,Python,Class,Pygame,我一直在尝试开始一个基本的2d塔防游戏。我想通过在棕色背景上放置几十个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_

我一直在尝试开始一个基本的2d塔防游戏。我想通过在棕色背景上放置几十个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()

您需要使用一个精灵组,该组可以容纳任意数量的精灵。使用pygame.rect可以将每个精灵设置为不同的坐标。 以下是一个例子:

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中使用了精灵模块,然后在最后添加了一行:

self.rect = self.image.get_rect()
允许自由编辑对象的x和y坐标

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

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循环替换为以下内容,将hashtag替换为x和y坐标:

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.rect=self.image.get_rect()添加到您创建的任何精灵类中,允许更改它的rect
  • 要更改它的rect,可以使用:“player.rect.x=#x”和“player.rect.y=#y”
  • 如果您希望在屏幕上放置这些对象的位置存在某种顺序,可以使用类似于上面第一个示例中所示的for循环,该循环将这些对象设置为斜穿过屏幕
  • 如果对象的设置位置没有任何关系,您可以简单地使用第6个代码示例中显示的代码,根据您想要的对象数量,重复任意次数
很抱歉,如果这太难理解,但我希望它有帮助


注意:确保在创建类时使用pygame.sprite.sprite,而不是像在代码中所做的那样使用object来创建它。

您需要使用一个sprite组,该组可以容纳任意数量的sprite。使用pygame.rect可以将每个精灵设置为不同的坐标。 以下是一个例子:

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中使用了精灵模块,然后在最后添加了一行:

self.rect = self.image.get_rect()
允许自由编辑对象的x和y坐标

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

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循环替换为以下内容,将hashtag替换为x和y坐标:

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.rect=self.image.get_rect()添加到您创建的任何精灵类中,允许更改它的rect
  • 要更改它的rect,可以使用:“player.rect.x=#x”和“player.rect.y=#y”
  • 如果您希望在屏幕上放置这些对象的位置存在某种顺序,可以使用类似于上面第一个示例中所示的for循环,该循环将这些对象设置为斜穿过屏幕
  • 如果对象的设置位置没有任何关系,您可以简单地使用第6个代码示例中显示的代码,根据您想要的对象数量,重复任意次数
很抱歉,如果这太难理解,但我希望它有帮助


注意:确保在创建类时使用的是pygame.sprite.sprite,而不是object,正如您在代码中所做的那样。

您是否已经熟悉类和pygame.sprite.spriteS?如果没有,我建议阅读的第12章和第13章。或者,您可以创建更改其位置并将其添加到列表中。另外,将类移出while循环,否则每次迭代都会一次又一次地定义它。您已经熟悉类和
pygame.sprite.sprite
s了吗?如果没有,我建议阅读的第12章和第13章。或者,您可以创建更改其位置并将其添加到列表中。另外,将类移出while循环,否则每次迭代都会一次又一次地定义它。