我创建了以下代码,但我不知道为什么无法创建圆形精灵。我真的在网上到处都看到了同样的东西,但是当我尝试它时却没用。我认为我只需要在更新和其他代码行之间切换即可。我是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
首先,您的精灵首先绘制圆形,然后用相同的颜色填充精灵。因此,您什么也看不到是正确的。
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
,我认为缩短这些键以节省一些击键是不值得的。这确实使以后阅读代码更加困难。