zhanghao51888
2021-06-14 16:52
采纳率: 100%
浏览 291

为什么这个pygame程序不报错但运行后黑屏,也不显示内容

import sys
import pygame as pg
from pygame.locals import *
import random
import time

pg.init()
screen_size = (1000, 700)  # 游戏窗口的大小为1000*700
screen = pg.display.set_mode(screen_size)
screen_color = (225, 218, 185)  # 游戏窗口背景颜色
snake_body = [[370, 300], [380, 300], [390, 300], [400, 300]]  # 贪吃蛇的初始位置
snake_body_color = (85, 26, 139)  # 贪吃蛇的颜色
pg.display.set_caption('贪吃蛇')  # 游戏窗口的名字
raspberry = [300, 300]  # 树莓的位置
raspberry_color = [250, 0, 0]  # 树莓的颜色
snake_direction = 'RIGHT'  # 贪吃蛇的初始运行方向为'右'
snake_step = 10  # 贪吃蛇的步长为10
score = 0  # 游戏分数初始值为0
font = pg.font.SysFont('comicsansms', 15)
while True:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            pg.quit()
            sys.exit()
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_UP:
                if snake_direction == 'DOWN':
                    snake_direction = 'DOWN'
                else:
                    snake_direction = 'UP'
            if event.key == pg.K_DOWN:
                if snake_direction == 'UP':
                    snake_direction = 'UP'
                else:
                    snake_direction = 'DOWN'
            if event.key == pg.K_LEFT:
                if snake_direction == 'RIGHT':
                    snake_direction = 'RIGHT'
                else:
                    snake_direction = 'LEFT'
            if event.key == pg.K_RIGHT:
                if snake_direction == 'LEFT':
                    snake_direction = 'LEFT'
                else:
                    snake_direction = 'RIGHT'
            if snake_direction == 'UP':
                snake_add = [snake_body[-1][0], snake_body[-1][1] - snake_step]
            if snake_direction == 'DOWN':
                snake_add = [snake_body[-1][0], snake_body[-1][1] + snake_step]
            if snake_direction == 'LEFT':
                snake_add = [snake_body[-1][0] - snake_step, snake_body[-1][1]]
            if snake_direction == 'RIGHT':
                snake_add = [snake_body[-1][0] + snake_step, snake_body[-1][1]]
                # '贪吃蛇'的头部碰撞到游戏窗口四周以及身体的某一部分,游戏结束
            if snake_add in snake_body[:-2]:
                break
            elif snake_add[0] < 0 or (snake_add[0] + 10) > 800 or snake_add[1] < 0 or (snake_add[1] + 10) > 600:
                break
                # 判断蛇有没有吞吃树莓
            if snake_add[0] == raspberry[0] and snake_add[1] == raspberry[1]:
                snake_body.append(raspberry)
                score += 1
                raspberry_swap = [random.randint(0, 80) * 10, random.randint(0, 60) * 10]
                if raspberry_swap not in snake_body:
                    raspberry = raspberry_swap
            else:
                snake_body.append(snake_add)
                snake_body.pop(0)
            if snake_add in snake_body[:-2]:
                break
            elif snake_add[0] < 0 or (snake_add[0] + 10) > 800 or snake_add[1] < 0 or (snake_add[1] + 10) > 600:
                break
            if snake_add[0] == raspberry[0] and snake_add[1] == raspberry[1]:
                snake_body.append(raspberry)
                score += 1
                raspberry_swap = [random.randint(0, 80) * 10, random.randint(0, 60) * 10]
                if raspberry_swap not in snake_body:
                    raspberry = raspberry_swap
            else:
                snake_body.append(snake_add)
                snake_body.pop(0)
            scoreSurf = font.render('Score: %s' % score, True, (0, 225, 0))
            scoreRect = scoreSurf.get_rect()
            screen.blit(scoreSurf, scoreRect)
            pg.draw.rect(screen, raspberry_color, Rect(raspberry[0], raspberry[1], 10, 10))

for item in snake_body:
    pg.draw.rect(screen, snake_body_color, Rect(item[0], item[1], 10, 10))
    pg.display.update()
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题