これは正しいウェブサイトであるかどうかは分かりませんが、以前はとても助けになりました。私はPythonで問題を抱えていました。 Pygame。Pygame:コンストラクタ引数に基づいて楕円または矩形を描く
私はシンプルなゲームを作っていますが、最近はPythonの学習が始まったばかりです(これまで愛していました)。そして、私は使っているスプライトコンストラクタを持っています。このコンストラクタはオブジェクトを管理しますが、渡された引数に基づいて楕円または矩形を描画します。
#My code
class Block(pygame.sprite.Sprite):
#Variables!
speed = 2
indestructible = True
#Constructor
def __init__(self, color, width, height, name, shapeType):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([width,height])
self.image.fill(color)
#Choose what to draw
if shapeType == "Ellipse":
pygame.draw.ellipse(self.image,color,[0,0,width,height])
elif shapeType == "Rect":
pygame.draw.rect(self.image,color,[0,0,width,height])
elif shapeType == "":
print("Shape type for ",name," not defined.")
pygame.draw.rect(self.image,color,[0,0,width,height])
#Init the Rect class for sprites
self.rect = self.image.get_rect()
私は、正方形を描画するために使用していますコーディングは以下の通りです:
#Add 'white star' to the list
for i in range(random.randrange(100,200)):
whiteStar = Block(white, 1, 1, "White Star", "Rect")
whiteStar.rect.x = random.randrange(size[0])
whiteStar.rect.y = random.randrange(size[1])
whiteStar.speed = 2
block_list.add(whiteStar)
all_sprites_list.add(whiteStar)
これが見事に動作します。それは私のための完全な小さな白い四角形を描画します。
が#Create Planet
planet = Block(green, 15,15, "Planet", "Ellipse")
planet.rect.x = random.randrange(size[0])
planet.rect.y = 30
planet.speed = 1
block_list.add(planet)
all_sprites_list.add(planet)
「惑星」は正しく起動しますが、それは正方形のように行います。しかし、このは動作しません。なぜこうなった?それをどうやって修正することができますか?これを修正するためにビットマップを使用する必要がありますか?または私のコーディングが間違っていますか?ただ、明確にする
、私はself.rect = self.image.get_rect()
が作品以下のコーディングので、楕円を描く作業を行うという事実を知っています。
#Not the code I'm using, but this works and proves self.rect = self.image.get_rect() is not the cause
# Call the parent class (Sprite) constructor
pygame.sprite.Sprite.__init__(self)
# Create an image of the block, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(white)
self.image.set_colorkey(white)
pygame.draw.ellipse(self.image,color,[0,0,width,height])
# Fetch the rectangle object that has the dimensions of the image
# image.
# Update the position of this object by setting the values
# of rect.x and rect.y
self.rect = self.image.get_rect()
ありがとうございました。 :-)
鮮やかな、あなたの助けに感謝、私は今それを修正しました! :-) – Singular1ty