Python pygame精灵组绘制未绘制所有精灵

Python pygame精灵组绘制未绘制所有精灵,python,pygame,Python,Pygame,我写了一个程序,创建了4个精灵作为矩形,在pygame界面中反弹。位置将在精灵的更新过程中更新。当我运行程序时,只有1-3个矩形出现,哪些矩形是随机出现的。我添加了一个print语句,可以看到每个sprite都在更新 代码如下: # import pygame library so we can use it! import pygame import random import mygame_colors # run initialization code on the library py

我写了一个程序,创建了4个精灵作为矩形,在pygame界面中反弹。位置将在精灵的更新过程中更新。当我运行程序时,只有1-3个矩形出现,哪些矩形是随机出现的。我添加了一个print语句,可以看到每个sprite都在更新

代码如下:

# import pygame library so we can use it!
import pygame
import random
import mygame_colors

# run initialization code on the library
pygame.init()

# setup display dimensions
display_width = 1200
display_height = 600
FPS = 30

gameSurface = pygame.display.set_mode((display_width, display_height))
pygame.display.set_caption('Window Caption!')
colors = (mygame_colors.RED,mygame_colors.GREEN,mygame_colors.WHITE,mygame_colors.BLUE)

# game code that needs to run only once

class Enemy(pygame.sprite.Sprite):
    def __init__(self, number):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10*(number+1), 10))
        self.number = number
        # self.color = (random.randint(100,255),random.randint(100,255),random.randint(100,255))
        self.color = colors[x]
        self.image.fill(self.color)
        self.rect = self.image.get_rect()
        self.rect.center = (random.randint(0,display_width), random.randint(0,display_width))
        self.x_speed = random.randint(-30,30)
        if self.x_speed == 0:
            self.x_speed += 1
        self.y_speed = random.randint(-30,30)
        if self.y_speed == 0:
            self.y_speed += 1
    def update(self,*args):
        super().update(self,*args)
        self.rect.x += self.x_speed
        self.rect.y += self.y_speed
        x,y = self.rect.center
        if x > display_width or x < 0:
            self.x_speed *= -1
        if y > display_height or y < 0:
            self.y_speed *= -1
        print ("%s %s: %s,%s,%s"%(self.number,self.color,self.rect.x,self.rect.y,self.image))


# create game clock
clock = pygame.time.Clock()

# create a sprite group to keep track of sprites
all_sprites = pygame.sprite.Group()

for x in range(4):
   player = Enemy(x)
   all_sprites.add(player)

# fill entire screen with color
gameSurface.fill(mygame_colors.BLACK)

# main game loop
running = True  # when running is True game loop will run
while running == True:
    # get input events and respond to them
    # if event is .QUIT
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    clock.tick(FPS)

    # game code that repeats every frame
    # clear display (redraw background)
    gameSurface.fill(mygame_colors.BLACK)


    # update
    all_sprites.update()    # draw all sprites
    all_sprites.draw(gameSurface)

    # pygame.draw.rect(gameSurface, r_color, [rectX, rectY, 25, 25])
    # pygame.draw.circle(gameSurface,r_color,(int(rectX),int(rectY)),25)
    # gameSurface.blit(image1, [rectX, rectY])


    # update and redraw entire screen
    pygame.display.flip()
    # pygame.display.update()

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((50, 50))
        self.image.fill(pygameColors.GREEN)
        self.rect = self.image.get_rect()
        self.rect.center = (display_width / 2, display_height / 2)
#导入pygame库以便我们可以使用它!
导入pygame
随机输入
导入mygame_颜色
#在库上运行初始化代码
pygame.init()
#设置显示尺寸
显示宽度=1200
显示高度=600
FPS=30
gamessurface=pygame.display.set_模式((显示宽度、显示高度))
pygame.display.set_标题('窗口标题!')
颜色=(我的游戏颜色。红色,我的游戏颜色。绿色,我的游戏颜色。白色,我的游戏颜色。蓝色)
#只需运行一次的游戏代码
职业敌人(pygame.sprite.sprite):
定义初始化(自身,编号):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=pygame.Surface((10*(数字+1),10))
self.number=number
#self.color=(random.randint(100255)、random.randint(100255)、random.randint(100255))
self.color=颜色[x]
self.image.fill(self.color)
self.rect=self.image.get_rect()
self.rect.center=(random.randint(0,显示宽度),random.randint(0,显示宽度))
self.x_速度=random.randint(-30,30)
如果self.x_速度==0:
self.x_速度+=1
self.y_速度=random.randint(-30,30)
如果self.y_速度==0:
self.y_速度+=1
def更新(自我,*参数):
super().update(self,*args)
self.rect.x+=self.x_速度
self.rect.y+=self.y\u速度
x、 y=自正中心
如果x>显示宽度或x<0:
self.x_速度*=-1
如果y>显示高度或y<0:
self.y_速度*=-1
打印(“%s%s:%s,%s,%s”%(self.number,self.color,self.rect.x,self.rect.y,self.image))
#创建游戏时钟
clock=pygame.time.clock()
#创建精灵组以跟踪精灵
all_sprites=pygame.sprite.Group()
对于范围(4)内的x:
玩家=敌人(x)
所有精灵。添加(玩家)
#用颜色填充整个屏幕
游戏表面。填充(mygame_颜色。黑色)
#主游戏循环
running=True#当running为True时,游戏循环将运行
运行时==True:
#获取输入事件并响应它们
#如果事件为。请退出
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
时钟滴答声(FPS)
#每帧重复的游戏代码
#清除显示(重新绘制背景)
游戏表面。填充(mygame_颜色。黑色)
#更新
所有精灵。更新()#绘制所有精灵
所有精灵。绘制(游戏表面)
#pygame.draw.rect(游戏表面,r_颜色,[rectX,rectY,25,25])
#pygame.draw.circle(游戏表面,r_颜色,(int(rectX),int(rectY)),25)
#blit(图像1,[rectX,rectY])
#更新并重新绘制整个屏幕
pygame.display.flip()
#pygame.display.update()
职业玩家(pygame.sprite.sprite):
定义初始化(自):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=pygame.Surface((50,50))
self.image.fill(pygameColors.GREEN)
self.rect=self.image.get_rect()
self.rect.center=(显示宽度/2,显示高度/2)

一些矩形随机不在窗口中,因为矩形位于区域
显示宽度
*
显示宽度
而不是
显示宽度
*
显示高度

更改矩形的位置以解决此问题:

self.rect.center=(random.randint(0,显示宽度),random.randint(0,显示宽度))

self.rect.center=(random.randint(0,显示宽度),random.randint(0,显示高度))

以下是mygame_colors.py的代码:黑色=(0,0,0)白色=(255,255,255)灰色=(235,235,235)绿色=(0,255,0)红色=(255,0,0)蓝色=(65,231,255)紫色=(255,0,255)黄色=(255,255,255,0)