【问题标题】:pygame crashes when double clicking on screen双击屏幕时pygame崩溃
【发布时间】:2015-06-30 13:36:25
【问题描述】:

每当我尝试制作蛇游戏时,这仍然不完整,每当我的蛇与边界碰撞时,它都会进入下一个屏幕打印

你太烂了再试一次

但如果我双击 pygame 窗口上的任意位置,它有时会崩溃。

左键点击次数不止2次,但最后还是点击了一段时间后崩溃。

我必须做些什么来解决这个问题,因为在我玩蛇的初始屏幕上不存在这个问题。

我使用的代码如下:

import pygame
import random
pygame.init()
font = pygame.font.Font(None,25)
def mess(msg,color):
    screen_text = font.render(msg, True , color)
    display.blit(screen_text , [(w/2) - 50 , h/2] )
score = 0
w = 800
h = 600
display = pygame.display.set_mode((w,h))
pygame.display.update()
red = (255,0,0)
white = (255,255,255)
black = (0,0,0)
yellow = (255,255,0)
bye = False
xpos = w/2
ypos = h/2
xch  = 0
ych  = 0
b    = 10
while True:
    wa = random.randrange (10,700)
    ha = random.randrange (10,500)
    if wa%10 == 0 and ha%10 == 0:
        break

time = pygame.time.Clock()
FPS = 10
up = False
side = False
lenth = 10
while not bye:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_w and up == False:
                 ych = -b
                 xch = 0
                 up = True
                 side = False
            if event.key == pygame.K_s and up == False:
                 ych = +b
                 xch = 0
                 up = True
                 side = False

            if event.key ==pygame.K_d and side == False:
                 xch = +b
                 ych = 0
                 side = True
                 up = False

            if event.key == pygame.K_a and side == False:
                 xch = -b
                 ych = 0
                 side = True
                 up = False
    if xpos == wa  and ypos == ha:
        lenth = lenth + 10
        while True:
            wa = random.randrange (10,700)
            ha = random.randrange (10,500)
            if wa%10 ==0 and ha%10 == 0:
                break

        score = score + 10
    if xpos == w - 10  or xpos == 0 or ypos == h - 10 or ypos == 0:
        xch = 0
        ych = 0
        display.fill(black)
        mess("You suck Try again!",red)
        pygame.display.update()
        break

    xpos = xpos + xch
    ypos = ypos + ych
    time.tick(FPS)

   display.fill(white)
   display.fill(red , rect=[xpos,ypos,10,10])#snake
   display.fill(yellow ,rect=[xpos + 0.41 , ypos +10 ,10 , lenth])#bady
   display.fill(black, rect=[wa,ha,10,10])  #food
   pygame.display.update()

【问题讨论】:

  • 下次请花点时间让您的问题可读。

标签: python crash pygame


【解决方案1】:

我不知道这是否能完全解决您的问题。但是像这样没有睡眠的 while 循环会导致你的程序占用你所有的 CPU 资源,这可能会导致你的程序崩溃。解决此问题的一种方法是更改​​此代码...

while True:
    wa = random.randrange (10,700)
    ha = random.randrange (10,500)
    if wa%10 ==0 and ha%10 == 0:
        break

到这里……

wa = random.randrange(1,70)*10
ha = random.randrange(1,50)*10

这将同时删除 while 循环,并且仍然给出一个在 10 和 700 以及 10 和 500 之间可被 10 整除的数字。

【讨论】:

  • 我试过了,但问题仍然存在,谢谢,虽然这段代码对我来说更有效率和有用。
【解决方案2】:

当你的蛇离开屏幕时,会执行以下 if 语句:

if xpos == w - 10  or xpos == 0 or ypos == h - 10 or ypos == 0:
    xch = 0
    ych = 0
    display.fill(black)
    mess("You suck Try again!",red)
    pygame.display.update()
    break

因此,在将一些文本绘制到显示器并调用pygame.display.update() 之后,您调用break 结束您的while 循环。

所以你的游戏不会崩溃,它只是结束。

【讨论】:

  • 我知道这是它应该做的,它应该结束,但是在下一个屏幕中,如果尝试随机点击屏幕上的某个地方,它应该什么都不做,但点击几下它就会崩溃(窗口变得无响应) 这是不应该做的。
猜你喜欢
  • 1970-01-01
  • 2013-04-22
  • 1970-01-01
  • 2023-03-31
  • 2018-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多