使用 pygame.draw.circle() 得到四分之一圆
Getting quarter of a circle with pygame.draw.circle()
我和我的朋友正在尝试获得一个圆形精灵,我们用矩形做到了,但我们的代码显然得到了四分之一的圆。 (顺便说一下,在我们的语言中,“sirkel”表示圆形,“firkant”表示矩形
我们不知道为什么会这样。这没有任何意义。我们试图改变位置,看看是否有重叠的东西,但没有。我们正在使用一个名为 Repl.it 的网站,因此可能存在一些问题,因为它非常有用,它会把东西弄乱,但不会那么多。
这是main.py:
import pygame
from firkant import Firkant
from sirkel import Sirkel
pygame.init()
PLAYER=0
screen = pygame.display.set_mode([600, 600])
from pygame.locals import (
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
GREEN = (40,40,40)
pos1=60
pos2=235
pos3=410
posses = [pos1, pos2, pos3]
Firkant1 = Firkant(GREEN, 130, 130)
Firkant1.rect.x = pos1
Firkant1.rect.y = pos1
Firkant2 = Firkant(GREEN, 130, 130)
Firkant2.rect.x = pos2
Firkant2.rect.y = pos1
Firkant3 = Firkant(GREEN, 130, 130)
Firkant3.rect.x = pos3
Firkant3.rect.y = pos1
Firkant4 = Firkant(GREEN, 130, 130)
Firkant4.rect.x = pos1
Firkant4.rect.y = pos2
Firkant5 = Firkant(GREEN, 130, 130)
Firkant5.rect.x = pos2
Firkant5.rect.y = pos2
Firkant6 = Firkant(GREEN, 130, 130)
Firkant6.rect.x = pos3
Firkant6.rect.y = pos2
Firkant7 = Firkant(GREEN, 130, 130)
Firkant7.rect.x = pos1
Firkant7.rect.y = pos3
Firkant8 = Firkant(GREEN, 130, 130)
Firkant8.rect.x = pos2
Firkant8.rect.y = pos3
Firkant9 = Firkant(GREEN, 130, 130)
Firkant9.rect.x = pos3
Firkant9.rect.y = pos3
Sirkel1 = Sirkel((0,0,0), 400, 300)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(Firkant1, Firkant2, Firkant3, Firkant4, Firkant5, Firkant6, Firkant7, Firkant8, Firkant9, Sirkel1)
for a in posses:
x = a
for b in posses:
pass
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
screen.fill((100, 100, 100))
all_sprites_list.update()
all_sprites_list.draw(screen)
pygame.display.flip()
pygame.quit()
这是square.py:
import pygame
WHITE = (255, 255, 255)
SS = 600
class Firkant(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
pygame.draw.rect(self.image, color, (0, 0, width, height))
self.rect = self.image.get_rect()
这是circle.py
import pygame
WHITE = (255, 255, 255)
class Sirkel(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
pygame.draw.circle(self.image, color, (width, height), 200)
self.rect = self.image.get_rect()
pygame.draw.circle
的第3个参数为圆心。如果矩形是(width
,height
)大小,则中心点是(width // 2
,height // 2
):
pygame.draw.circle(self.image, color, (width, height), 200)
radius = min(width, height) // 2
pygame.draw.circle(self.image, color, (width // 2, height // 2), radius)
您还可以使用 pygame.Rect
对象的 center
属性:
self.rect = self.image.get_rect()
pygame.draw.circle(self.image, color, self.rect.center, min(self.rect.center))
我和我的朋友正在尝试获得一个圆形精灵,我们用矩形做到了,但我们的代码显然得到了四分之一的圆。 (顺便说一下,在我们的语言中,“sirkel”表示圆形,“firkant”表示矩形
我们不知道为什么会这样。这没有任何意义。我们试图改变位置,看看是否有重叠的东西,但没有。我们正在使用一个名为 Repl.it 的网站,因此可能存在一些问题,因为它非常有用,它会把东西弄乱,但不会那么多。
这是main.py:
import pygame
from firkant import Firkant
from sirkel import Sirkel
pygame.init()
PLAYER=0
screen = pygame.display.set_mode([600, 600])
from pygame.locals import (
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
K_ESCAPE,
KEYDOWN,
QUIT,
)
GREEN = (40,40,40)
pos1=60
pos2=235
pos3=410
posses = [pos1, pos2, pos3]
Firkant1 = Firkant(GREEN, 130, 130)
Firkant1.rect.x = pos1
Firkant1.rect.y = pos1
Firkant2 = Firkant(GREEN, 130, 130)
Firkant2.rect.x = pos2
Firkant2.rect.y = pos1
Firkant3 = Firkant(GREEN, 130, 130)
Firkant3.rect.x = pos3
Firkant3.rect.y = pos1
Firkant4 = Firkant(GREEN, 130, 130)
Firkant4.rect.x = pos1
Firkant4.rect.y = pos2
Firkant5 = Firkant(GREEN, 130, 130)
Firkant5.rect.x = pos2
Firkant5.rect.y = pos2
Firkant6 = Firkant(GREEN, 130, 130)
Firkant6.rect.x = pos3
Firkant6.rect.y = pos2
Firkant7 = Firkant(GREEN, 130, 130)
Firkant7.rect.x = pos1
Firkant7.rect.y = pos3
Firkant8 = Firkant(GREEN, 130, 130)
Firkant8.rect.x = pos2
Firkant8.rect.y = pos3
Firkant9 = Firkant(GREEN, 130, 130)
Firkant9.rect.x = pos3
Firkant9.rect.y = pos3
Sirkel1 = Sirkel((0,0,0), 400, 300)
all_sprites_list = pygame.sprite.Group()
all_sprites_list.add(Firkant1, Firkant2, Firkant3, Firkant4, Firkant5, Firkant6, Firkant7, Firkant8, Firkant9, Sirkel1)
for a in posses:
x = a
for b in posses:
pass
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
screen.fill((100, 100, 100))
all_sprites_list.update()
all_sprites_list.draw(screen)
pygame.display.flip()
pygame.quit()
这是square.py:
import pygame
WHITE = (255, 255, 255)
SS = 600
class Firkant(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
pygame.draw.rect(self.image, color, (0, 0, width, height))
self.rect = self.image.get_rect()
这是circle.py
import pygame
WHITE = (255, 255, 255)
class Sirkel(pygame.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
pygame.draw.circle(self.image, color, (width, height), 200)
self.rect = self.image.get_rect()
pygame.draw.circle
的第3个参数为圆心。如果矩形是(width
,height
)大小,则中心点是(width // 2
,height // 2
):
pygame.draw.circle(self.image, color, (width, height), 200)
radius = min(width, height) // 2
pygame.draw.circle(self.image, color, (width // 2, height // 2), radius)
您还可以使用 pygame.Rect
对象的 center
属性:
self.rect = self.image.get_rect()
pygame.draw.circle(self.image, color, self.rect.center, min(self.rect.center))