zhanghao51888 2021-06-14 16:52 采纳率: 33.3%
浏览 593
已结题

为什么这个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条回答 默认 最新

  • 小P聊技术 2021-06-14 17:31
    关注
    import pygame
    from sys import exit
    import random
    
    
    class Point():
        row = 0
        clo = 0
    
        def __init__(self, row, clo):
            self.row = row
            self.clo = clo
    
        def copy(self):
            return Point(row=self.row, clo=self.clo)
    
    
    # 初始化
    pygame.init()
    width = 800
    hight = 400
    
    ROW = 30
    CLO = 40
    
    direct = 'left'
    window = pygame.display.set_mode((width, hight))
    pygame.display.set_caption('贪吃蛇游戏')
    
    # 蛇头坐标定在中间
    head = Point(row=int(ROW / 2), clo=int(CLO / 2))
    # 初始化蛇身的元素数量
    snake = [
        Point(row=head.row, clo=head.clo + 1),
        Point(row=head.row, clo=head.clo + 2),
        Point(row=head.row, clo=head.clo + 3)
    ]
    
    
    # 生成食物并且不让食物生成在蛇的身体里面
    def gen_food():
        while 1:
            position = Point(row=random.randint(0, ROW - 1), clo=random.randint(0, CLO - 1))
            is_coll = False
            if head.row == position.row and head.clo == position.clo:
                is_coll = True
            for body in snake:
                if body.row == position.row and body.clo == position.clo:
                    is_coll = True
                    break
            if not is_coll:
                break
        return position
    
    
    # 定义坐标
    # 蛇头颜色可以自定义
    head_color = (0, 158, 128)
    # 食物坐标
    snakeFood = gen_food()
    # 食物颜色
    snakeFood_color = (255, 255, 0)
    
    snake_color = (200, 147, 158)
    
    
    # 需要执行很多步画图操作 所以定义一个函数
    def rect(point, color):
        # 定位 画图需要left和top
        left = point.clo * width / CLO
        top = point.row * hight / ROW
        # 将方块涂色
        pygame.draw.rect(window, color, (left, top, width / CLO, hight / ROW))
    
    
    quit = True
    # 设置帧频率
    clock = pygame.time.Clock()
    while quit:
        # 处理帧频 锁帧
        clock.tick(30)
        # pygame.event.get()获取当前事件的队列 可以同时发生很多事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit = False
            elif event.type == pygame.KEYDOWN:
                # 这里小细节蛇不可以直接左右上下 要判断当前是在什么状态下前行
                if event.key == 273 or event.key == 119:
                    if direct == 'left' or direct == 'right':
                        direct = 'top'
                if event.key == 274 or event.key == 115:
                    if direct == 'left' or direct == 'right':
                        direct = 'bottom'
                if event.key == 276 or event.key == 97:
                    if direct == 'top' or direct == 'bottom':
                        direct = 'left'
                if event.key == 275 or event.key == 100:
                    if direct == 'top' or direct == 'bottom':
                        direct = 'right'
        # 吃东西
        eat = (head.row == snakeFood.row and head.clo == snakeFood.clo)
    
        # 处理蛇的身子
        # 1.把原来的头插入到snake的头上
        # 2.把最后一个snake删掉
        if eat:
            snakeFood = Point(row=random.randint(0, ROW - 1), clo=random.randint(0, CLO - 1))
        snake.insert(0, head.copy())
        if not eat:
            snake.pop()
    
        # 移动一下
        if direct == 'left':
            head.clo -= 1
        if direct == 'right':
            head.clo += 1
        if direct == 'top':
            head.row -= 1
        if direct == 'bottom':
            head.row += 1
        dead = False
        if head.clo < 0 or head.row < 0 or head.clo >= CLO or head.row >= ROW:
            dead = True
        for body in snake:
            if head.clo == body.clo and head.row == body.row:
                dead = True
                break
        if dead:
            print('Game Over')
            quit = False
        # 背景画图
        pygame.draw.rect(window, (245, 135, 155), (0, 0, width, hight))
    
        # 蛇头
        rect(head, head_color)
        # 绘制食物
        rect(snakeFood, snakeFood_color)
        # 绘制蛇的身子
        for body in snake:
            rect(body, snake_color)
    
        # 交还控制权
        pygame.display.flip()
    
    

    在这里插入图片描述

    完整地址:

    用pygame制作贪吃蛇游戏(详细):https://blog.csdn.net/weixin_44051713/article/details/90042927

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 10月24日
  • 已采纳回答 10月16日

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统