gpt4 book ai didi

python - Pygame:为什么我无法绘制圆形 Sprite

转载 作者:行者123 更新时间:2023-12-01 21:10:47 25 4
gpt4 key购买 nike

我创建了以下代码,但我不知道为什么无法创建圆形 Sprite 。我真的在网上到处找,发现了同样的事情,但当我尝试它时,它不起作用。我想我只需要在更新和其他代码行之间切换。我是 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()

这是点类:

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

最佳答案

首先,您的 Sprite 首先绘制圆圈,然后用相同的颜色填充 Sprite 。所以你看不到任何东西是正确的。

pg.draw.circle(self.image,self.color,self.rect.center,5)
self.image.fill(color)

它还在屏幕坐标(比如 200,200)处绘制圆圈,而位图只有 10x10 像素。我修复了坐标,并更改了 Dot 类以根据给定半径调整 Sprite 圆的大小,因为尝试在 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,我认为不值得为了节省一些击键次数而缩短它们。这确实会让以后阅读代码变得更加困难。

关于python - Pygame:为什么我无法绘制圆形 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58944288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com