soft-summer-2021 / summer2021

程序设计实践 (2021夏季学期)
34 stars 6 forks source link

python执行到第二个while循环后就直接进入死循环 #78

Closed stevenjonas110 closed 3 years ago

stevenjonas110 commented 3 years ago
def game():
    global score,dir,food,head,headcolor,snake,grade,level,name
    inputUI(window)
    exit = True
    dead = False
    score=0
    dir = 'left'
    food = createfood()

    # 初始化坐标
    head = position(row=int(Row / 2), line=int(Line / 2))
    headcolor = (0, 128, 128)
    snake = []

    while exit:
        # 绘制游戏界面
        pygame.display.flip()
        pygame.draw.rect(window, background_color, (0, 0, wide, height))

        draw(head, headcolor)
        draw(food, foodcolor)

        # 设置帧频
        clock.tick(grade)

        for body in snake:
            draw(body, bodycolor)

        playfont = pygame.font.SysFont('Times', 30)
        text = playfont.render('SCORE:' + str(score), True, (0, 0, 255), (255, 255, 255))
        window.blit(text, (0, 0))

        pygame.display.update()

        # 游戏操作
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit = False
            elif event.type == pygame.KEYDOWN:
                if event.key == 1073741906:
                    if dir == 'left' or dir == 'right':
                        dir = 'up'
                elif event.key == 1073741905:
                    if dir == 'left' or dir == 'right':
                        dir = 'down'
                elif event.key == 1073741904:
                    if dir == 'up' or dir == 'down':
                        dir = 'left'
                elif event.key == 1073741903:
                    if dir == 'up' or dir == 'down':
                        dir = 'right'

                # 空格键实现暂停
                elif event.key==32:
                    pause=True
                    while pause:
                        for event in pygame.event.get():
                            if event.type == pygame.KEYDOWN and event.key==32:
                                pause=False

        eat = (head.row == food.row and head.line == food.line)

        snake.insert(0, copy(head))
        if eat:
            food = createfood()
            score += 1
        else:
            snake.pop()

        if dir == 'left':
            head.line = head.line - 1
        elif dir == 'right':
            head.line = head.line + 1
        elif dir == 'up':
            head.row = head.row - 1
        elif dir == 'down':
            head.row = head.row + 1

        # 难度增加
        grade = grade + int(score / 15)

        # 检测结束
        # 撞边界
        if head.row < 0 or head.line < 0 or head.row > Row + 1 or head.line > Line + 1:
            dead = True

        for body in snake:
            if head.row == body.row and head.line == body.line:
                dead = True

        if dead:
            exit=False
            save(name, score)

    while dead:
        result()
        window.blit(restart, (400, 400))
        window.blit(close, (400, 460))
        pygame.display.update()

        buttons = pygame.mouse.get_pressed()
        x1, y1 = pygame.mouse.get_pos()
        if x1 >= 400 and x1 <= 600 and y1 >= 400 and y1 <= 454:
            window.blit(choose, (350, 400))
            if buttons[0]:
                window.blit(bg, (0, 0))
                return

        elif x1 >= 400 and x1 <= 600 and y1 >= 460 and y1 <= 514:
            window.blit(choose, (350, 460))
            if buttons[0]:
                pygame.quit()
                sys.exit()

        else:
            window.blit(blank, (350, 400))
            window.blit(blank, (350, 460))
hzy1721 commented 3 years ago
 while dead:
    result()
    window.blit(restart, (400, 400))
    window.blit(close, (400, 460))
    pygame.display.update()

    buttons = pygame.mouse.get_pressed()
    x1, y1 = pygame.mouse.get_pos()
    if x1 >= 400 and x1 <= 600 and y1 >= 400 and y1 <= 454:
        window.blit(choose, (350, 400))
        if buttons[0]:
            window.blit(bg, (0, 0))
            return

    elif x1 >= 400 and x1 <= 600 and y1 >= 460 and y1 <= 514:
        window.blit(choose, (350, 460))
        if buttons[0]:
            pygame.quit()
            sys.exit()

    else:
        window.blit(blank, (350, 400))
        window.blit(blank, (350, 460))

这段代码里有更新 dead 的操作吗?如果不执行 returnexit() 就会死循环,检查一下为什么

stevenjonas110 commented 3 years ago
 while dead:
    result()
    window.blit(restart, (400, 400))
    window.blit(close, (400, 460))
    pygame.display.update()

    buttons = pygame.mouse.get_pressed()
    x1, y1 = pygame.mouse.get_pos()
    if x1 >= 400 and x1 <= 600 and y1 >= 400 and y1 <= 454:
        window.blit(choose, (350, 400))
        if buttons[0]:
            window.blit(bg, (0, 0))
            return

    elif x1 >= 400 and x1 <= 600 and y1 >= 460 and y1 <= 514:
        window.blit(choose, (350, 460))
        if buttons[0]:
            pygame.quit()
            sys.exit()

    else:
        window.blit(blank, (350, 400))
        window.blit(blank, (350, 460))

这段代码里有更新 dead 的操作吗?如果不执行 returnexit() 就会死循环,检查一下为什么

这段代码是游戏已经结束后的界面,让用户选择重新开始还是直接退出游戏,所以应该在用户肯定会在两个if中执行一个的,这个循环只是为了保持最后的游戏结束界面。

hzy1721 commented 3 years ago

你说的第二个 while 循环是这个 while dead: 循环吧,按照你说的不就应该一直循环等待用户选择吗,“死循环” 指的是点按钮不管用?

stevenjonas110 commented 3 years ago

你说的第二个 while 循环是这个 while dead: 循环吧,按照你说的不就应该一直循环等待用户选择吗,“死循环” 指的是点按钮不管用?

对,只要进入等待用户选择的页面就直接死机了

stevenjonas110 commented 3 years ago

你说的第二个 while 循环是这个 while dead: 循环吧,按照你说的不就应该一直循环等待用户选择吗,“死循环” 指的是点按钮不管用?

对,只要进入等待用户选择的页面就直接死机了

助教这是里面的result函数,还有最后运行卡死的界面

1630236270(1)

def result(): window.blit(bg, (0, 0)) font = pygame.font.SysFont('Times', 30) text1 = font.render('GAME OVER', True, (0, 0, 255), (255, 255, 255)) window.blit(text1, (300, 200)) text2 = font.render('Your score is:' +str(score), True, (0, 0, 255), (255, 255, 255)) window.blit(text2, (300, 300))