gpt4 book ai didi

python pygame将随机坐标星星放在屏幕上但重叠

转载 作者:行者123 更新时间:2023-12-01 06:47:37 28 4
gpt4 key购买 nike

我试图将星星放在屏幕上并给它们随机的 x,y 坐标

但是当屏幕刷新时,之前的星星仍然在屏幕上,并且与下一个星星重叠

我把setting.screen.fill((255,0,0))为什么这个背景不刷新以前的星星?是什么原因造成的???

代码如下:

#!/usr/bin/python
import sys
import pygame as p
from random import randint

#13-1

class Setting():
def __init__(self,width,height):
self.w=width
self.h=height
self.screen=p.display.set_mode((self.w,self.h),p.RESIZABLE,0)
p.display.set_caption("EXE 13-1")

class Star(p.sprite.Sprite):
def __init__(self):
super().__init__()
pic=p.image.load("../image/star.jpg").convert_alpha()
self.image=p.transform.smoothscale(pic,(100,100))
self.rect=self.image.get_rect()
self.rect.x=(self.rect.width)/2
def create(self,setting,stars):
spacex=setting.w-2*self.rect.x
numbers=int(spacex/(2*self.rect.x))
rows=int(setting.h/(2*self.rect.height))
for row in range(rows):
for number in range(numbers):
star=Star()
star.rect.x=randint(-10,10)+2*self.rect.width*number
star.rect.y=randint(-10,10)+2*self.rect.height*row
stars.add(star)
def blit(self,setting,stars):
stars.draw(setting.screen)
# for star in stars.sprites():
# setting.screen.blit(star.image,star.rect)


def game():
p.init()
setting=Setting(1200,800)
star=Star()
stars=p.sprite.Group()
while True:
for event in p.event.get():
if event.type == p.QUIT:
sys.exit()
elif event.type == p.KEYDOWN:
if event.key == p.K_ESCAPE:
sys.exit()
setting.screen.fill((255,0,0))
star.create(setting,stars)
star.blit(setting,stars)
p.display.flip()
game()

最佳答案

引起此问题的原因是,在每个帧中重新创建了整个星星并将其附加到 pygame.sprite.Group 星星。但该组永远不会被清空,并且不断增长:

while True:

# [...]

star.create(setting,stars) # <-- adds new objects to Group "stars" in every frame

最简单的解决方案是 empty()每帧中的

def game():

# [...]

while True:

# [...]

# remove all stars from the group
stars.empty()
# create new stars
star.create(setting,stars)

但更好的解决方案是将星星保留在中并随机化它们的位置。请注意,在每一帧中重新创建星星将会对性能产生影响。
在主应用程序循环之前创建星星,并为主应用程序循环中的每个星星设置一个新的随机位置:

class Star(p.sprite.Sprite):

# [...]

def create(self,setting,stars):
spacex=setting.w-2*self.rect.x
numbers=int(spacex/(2*self.rect.x))
rows=int(setting.h/(2*self.rect.height))
for row in range(rows):
for number in range(numbers):
star=Star()

# store row and colume of the stare
star.col = number
star.row = row

star.randomize()
stars.add(star)

# calculate random position
def randomize(self):
self.rect.x=randint(-10,10)+2*self.rect.width*self.col
self.rect.y=randint(-10,10)+2*self.rect.height*self.row
def game():
p.init()
setting=Setting(1200,800)
star=Star()
stars=p.sprite.Group()
star.create(setting,stars)
while True:
for event in p.event.get():
if event.type == p.QUIT:
sys.exit()
elif event.type == p.KEYDOWN:
if event.key == p.K_ESCAPE:
sys.exit()

# randomize positions of stars
for star in stars:
star.randomize()

setting.screen.fill((255,0,0))
star.blit(setting,stars)
p.display.flip()
<小时/>

最后,我建议从类方法 createblit 中删除 self 参数。只需将星星的图像放置一次就足够了:

class Star(p.sprite.Sprite):
def __init__(self, col, row, image):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.col, self.row = col, row
self.randomize()

def randomize(self):
self.rect.x=randint(-10,10)+2*self.rect.width*self.col
self.rect.y=randint(-10,10)+2*self.rect.height*self.row

def create(setting,stars):
pic = p.image.load("../image/star.jpg").convert_alpha()
pic = p.transform.smoothscale(pic,(100,100))
numbers, rows = setting.w // 100, setting.h // 100
for row in range(rows):
for number in range(numbers):
star=Star(number, row, pic)
stars.add(star)

def randomizeStars(stars):
for star in stars:
star.randomize()

def blit(setting,stars):
stars.draw(setting.screen)
def game():
p.init()
setting=Setting(1200,800)
stars=p.sprite.Group()
Star.create(setting,stars)
while True:
for event in p.event.get():
if event.type == p.QUIT:
sys.exit()
elif event.type == p.KEYDOWN:
if event.key == p.K_ESCAPE:
sys.exit()

Star.randomizeStars(stars)

setting.screen.fill((255,0,0))
Star.blit(setting,stars)
p.display.flip()

关于python pygame将随机坐标星星放在屏幕上但重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59159379/

28 4 0