【发布时间】:2021-06-23 14:56:06
【问题描述】:
我有这段代码可以保存一个对象(通常很小,如绘图)的位置和矩形,然后在我的屏幕中生成它们(blit)。我遇到过,如果我放置太多或太大的物体,我猜测矩形碰撞然后游戏崩溃,但有时,即使我没有很多物体,它也会因为这种情况而崩溃。
我该如何解决这个问题?我猜要添加一个 if 语句,以便它检查是否接近崩溃游戏或类似的东西,我保存图像的矩形在 for i in self.game_images 中: :
class GameScene(Scene):
def __init__(self, game, images, main_image, next_scene):
super().__init__(next_scene)
self.game = game
self.main_image = main_image
self.game_images = images
# Fade effect set-up
self.fade = False
self.fade_time = 0
self.current_alpha = 255
self.part = 1
self.record_text = font.render('Atiende',True, PURPLE)
self.correct_image_rect = None
# Trying to use colliderect so it doesnt overlap
# this is the same code as before but adapted to use the gameimage class and the rects stored there
self.rects = []
for i in self.game_images:
position_set = False
while not position_set:
x = random.randint(100,950)
y = random.randint(100,600)
i.rect.x = x
i.rect.y = y
margin = 5
rl = [rect.inflate(margin*2, margin*2) for rect in self.rects]
if len(self.rects) == 0 or i.rect.collidelist(rl) < 0:
self.rects.append(i.rect)
position_set = True
# this makes a number and object pair, and allows us to set the correct rects for the correct gameimage classes
for i, rect in enumerate(self.rects):
self.game_images[i].rect = rect
# this is the fade stuff from before that was in draw. It really belongs here tbh
def update(self, dt):
if self.part == 1 and self.fade:
self.fade_time += dt
if self.fade_time > fade_timer:
self.fade_time = 0
self.main_image.set_alpha(self.current_alpha)
self.record_text.set_alpha(self.current_alpha)
# Speed whichin the image dissapears
self.current_alpha -= 5
if self.current_alpha <= 0:
self.fade = False
self.part = 2
else:
# we reset the main image alpha otherwise it will be invisible on the next screen (yeah, this one caught me out lol!)
self.main_image.set_alpha(255)
# draw is similar to before, but a bit more streamlined as the fade stuff is not in update
def draw(self, screen):
super().draw(screen)
if self.part == 1:
screen.blit(self.record_text, (550, 20))
screen.blit(self.main_image.image, (580, 280))
else:
# Second half
text2 = font.render('¿Qué has visto?',True, PURPLE)
screen.blit(text2, (400,5))
# Show all similar images
for game_image in self.game_images:
game_image.draw(screen)
# We associate the correct rect to the correct image, to pass it later to the CORRECT Screen
self.correct_image_rect = self.game_images[self.game_images.index(self.main_image)].rect
# again we pass the event to the game object the same as with the other classes
def get_event(self, event):
if self.part == 2:
if self.game.level == 13:
self.game.game_over = True
if self.correct_image_rect.collidepoint(event.pos):
return 'CORRECT'
for rect in self.rects:
if not self.correct_image_rect.collidepoint(event.pos) and rect.collidepoint(event.pos):
return 'INCORRECT'
【问题讨论】:
-
“游戏崩溃”——肯定有一些错误和回溯?
-
我很确定你最终会陷入无限循环。
-
我很新,所以我没有回溯对不起,至于它是否以无限循环结束,我不太明白你的意思,我猜你是说它得到了一个无限循环,这就是它崩溃的原因。另外我不得不说,这段代码并不是所有的代码。我的理解是它崩溃了,因为当我对它们进行闪电战时,保存的矩形之间可能存在冲突。