Pygame:为什么我无法绘制圆形精灵?

4

我创建了以下代码,但不知道为什么无法创建圆形精灵。我在网上找了很多地方,发现都是相同的代码,但当我尝试时它没有起作用。我认为只需要在更新和其他代码行之间切换。我是pygame的新手,请非常明确地说明。

谢谢

这是主要函数:

import pygame as pg
import random
import dots

############ Some color codes  ############

WHITE = (255, 255, 255)
BLUE = (0,   0, 255)
GREEN = (0, 255,   0)
RED = (255,   0,   0)
BLACK = (0, 0, 0)
GREY = (169, 169, 169)
TEXTCOLOR = (0,   0,  0)

###########################################

(width,height)=(800,800)
dotStartPos = (width/2, height/2)
goalPos = (int(width/2), 5)
alldotsaredead = False

# Initiliaze pygame #
pg.init()

# Make screen and filling it with color
window = pg.display.set_mode((width, height))

# Create dots sprite group
dotssprite = pg.sprite.Group()
goaldotsprite = pg.sprite.Group()

# Draw the goal dot
dot_goal=dots.Dots(RED,width/2,5,2,window,0,0)
goaldotsprite.add(dot_goal)
# pg.draw.circle(window, RED, goalPos, 5)



running = True
FONT = pg.font.Font("freesansbold.ttf", 15)
clock = pg.time.Clock()

# Creating dots
# for i in range(50):
#   x = random.randrange(width)
#   y = random.randrange(height)
#   my_dot=dots.Dots(BLUE,x,y,5,window,1,0)
#   dotssprite.add(my_dot)
my_dot=dots.Dots(BLUE,400,400,5,window,1,0)
dotssprite.add(my_dot)
pg.draw.circle(window,GREEN,(300,300),5)

# Function to update screen
def udpatescreen():
    window.fill(WHITE)
    dotssprite.draw(window)
    goaldotsprite.draw(window)
    pg.display.update()



# Function to update dots sprite
def rundots():
    dotssprite.update()

while running:
    for event in pg.event.get():
        if event.type==pg.QUIT:
            running = False
    if alldotsaredead is False:
        rundots()   

    udpatescreen()      

这是点类(dots class):

import pygame as pg
import random
vec = pg.math.Vector2

class Dots(pg.sprite.Sprite):
    def __init__(self,color,x,y,radius,window,id,step):
        pg.sprite.Sprite.__init__(self)
        self.maxspeed = 4
        self.window = window
        self.id = id
        self.color = color
        self.x = x
        self.y = y
        self.pos = vec (self.x,self.y)
        self.radius=radius
        self.image = pg.Surface((10, 10))
        self.rect = self.image.get_rect(center=self.pos)
        pg.draw.circle(self.image,self.color,self.rect.center,5)
        self.image.fill(color)
        self.vel = vec(0, 0)
        self.accel = vec(0, 0)
        self.dead = False

1
欢迎来到StackOverflow。这里适用于 话题提问方式完美问题 ,还有 最小可复现示例。"它没起作用"不是一个问题描述。 - Prune
你得到了什么结果?请非常明确地表达。;) - Kingsley
1个回答

4
首先,你的精灵先绘制了圆形,然后用相同的颜色填充精灵。所以没能看到任何东西是正确的。
pg.draw.circle(self.image,self.color,self.rect.center,5)
self.image.fill(color)

它还在屏幕坐标(如200,200)处绘制圆圈,而位图仅为10x10像素。我更正了坐标,并更改了Dot类以根据给定半径调整精灵圆的大小,因为在10x10位图中绘制半径100的圆没有意义。
import pygame

# Window size
WINDOW_WIDTH      = 400
WINDOW_HEIGHT     = 400

# Colours
SKY_BLUE = ( 161, 255, 254 )
BLUE     = (   5,   5, 180 )
WHITE    = ( 255, 255, 255 )


# DOts Sprite
class Dots( pygame.sprite.Sprite ):
    def __init__(self,color,x,y,radius,window,id,step):
        pygame.sprite.Sprite.__init__(self)
        self.maxspeed = 4
        self.window   = window
        self.id       = id
        self.color    = color
        self.x        = x
        self.y        = y
        self.pos      = pygame.math.Vector2 (self.x,self.y)
        self.radius   = radius
        # Create the circle image
        self.image = pygame.Surface((self.radius*2, self.radius*2))
        self.rect  = self.image.get_rect(center=self.pos)
        self.image.fill(color)
        pygame.draw.circle(self.image, WHITE, (self.radius, self.radius), self.radius)
        self.vel   = pygame.math.Vector2(0, 0)
        self.accel = pygame.math.Vector2(0, 0)
        self.dead  = False



### MAIN
pygame.init()
window  = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Circle Sprite")

# Add a circle
circle_sprite = Dots(BLUE,200,200, 30 ,window,1,0)
circle_sprite_group = pygame.sprite.GroupSingle()
circle_sprite_group.add( circle_sprite )

clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # Update the window, but not more than 60fps
    window.fill( SKY_BLUE )
    circle_sprite_group.draw( window )
    pygame.display.flip()

    # Clamp FPS
    clock.tick_busy_loop(60)

pygame.quit()

注:我还将`pg`改为`pygame`,并将`vec`更改为`pygame.math.Vector2`,我认为缩短这些变量名称以节省几个按键不值得。这真的会使日后阅读代码更加困难。

太好了!很高兴我能帮到你。 - Kingsley
实际上,你也可以在pygame.Surface中添加一个SRCAPLHA标志,它也能正常工作。但我不知道SRCALPHA标志是什么意思?如果你有任何想法,请分享。 - essrifi ayoub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接