Pygame开发Pie Game报event无key属性、图形不渲染问题求解
Pie Game运行异常修复方案
问题根因
你遇到的两个核心问题均由缩进错误导致:
event对象无key属性报错:判断数字键1-4的分支缩进错误,和event.type == KEYUP判断同级,没有嵌套在键盘事件的判断逻辑内。Pygame事件队列包含鼠标移动、窗口重绘等大量非键盘事件,这类事件本身没有key属性,触发对应事件时直接读取event.key就会抛出属性错误。- 黑屏、图形不渲染:清屏、绘制元素、刷新屏幕的所有渲染逻辑,全部写在了
while True主循环的外部,程序启动后会无限循环遍历事件,永远执行不到渲染代码,自然无法显示画面。
另外你当前每个扇形的半径边绘制逻辑有误,四个块都固定画向上、向右的两条半径,除第一块外其余扇形的边都会错位,无法拼成完整披萨。
具体修改步骤
- 调整按键判断分支的缩进:将判断
pygame.K_1到pygame.K_4的所有elif块向内缩进一层,放到elif event.type == KEYUP:的代码块内,和ESC键退出的判断同级。 - 调整渲染逻辑的缩进:将从
screen.fill((0,0,200))开始,到pygame.display.update()结束的所有渲染、刷新代码,向内缩进一层,放到主循环内部、事件遍历for循环的后面,保证每帧都会执行绘制。 - 修正每个扇形对应的半径线段:
- 第2块(左上90°-180°):绘制向上半径、向左半径
- 第3块(左下180°-270°):绘制向左半径、向下半径
- 第4块(右下270°-360°):绘制向下半径、向右半径
修正后完整代码
# -*- coding = utf-8 -*- # @File : piegame.py # @Software:PyCharm import math import sys import pygame from pygame.locals import * pygame.init() screen = pygame.display.set_mode((600,500)) pygame.display.set_caption("The Pie Game - Press 1,2,3,4") myfont = pygame.font.Font(None,60) color = 200,80,60 width = 4 x = 300 y = 250 radius = 200 position = x-radius,y-radius,radius*2,radius*2 piece1 = False piece2 = False piece3 = False piece4 = False while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYUP: if event.key == pygame.K_ESCAPE: sys.exit() elif event.key == pygame.K_1: piece1 = True elif event.key == pygame.K_2: piece2 = True elif event.key == pygame.K_3: piece3 = True elif event.key == pygame.K_4: piece4 = True # clear the screen screen.fill((0,0,200)) # draw the four numbers testImg1 = myfont.render("1",True,color) screen.blit(testImg1,(x+radius/2-20,y-radius/2)) testImg2 = myfont.render("2",True,color) screen.blit(testImg2,(x-radius/2,y-radius/2)) testImg3 = myfont.render("3",True,color) screen.blit(testImg3,(x-radius/2,y+radius/2-20)) testImg4 = myfont.render("4",True,color) screen.blit(testImg4,(x+radius/2-20,y+radius/2-20)) #should the pieces be drawn? if piece1: start_angle=math.radians(0) end_angle =math.radians(90) pygame.draw.arc(screen,color,position,start_angle,end_angle,width) pygame.draw.line(screen,color,(x,y),(x,y-radius),width) pygame.draw.line(screen,color,(x,y),(x+radius,y),width) if piece2: start_angle=math.radians(90) end_angle =math.radians(180) pygame.draw.arc(screen,color,position,start_angle,end_angle,width) pygame.draw.line(screen,color,(x,y),(x,y-radius),width) pygame.draw.line(screen,color,(x,y),(x-radius,y),width) if piece3: start_angle=math.radians(180) end_angle =math.radians(270) pygame.draw.arc(screen,color,position,start_angle,end_angle,width) pygame.draw.line(screen,color,(x,y),(x-radius,y),width) pygame.draw.line(screen,color,(x,y),(x,y+radius),width) if piece4: start_angle = math.radians(270) end_angle = math.radians(360) pygame.draw.arc(screen, color, position, start_angle, end_angle, width) pygame.draw.line(screen, color, (x, y), (x, y + radius), width) pygame.draw.line(screen, color, (x, y), (x + radius, y), width) #is the pie finished? if piece1 and piece2 and piece3 and piece4: color = 0,255,0 pygame.display.update()
修正后运行程序,按1-4键会依次点亮对应扇形,四块全部点亮后线条会变成绿色,符合原教材的设计效果。
内容的提问来源于stack exchange,提问作者zhw054
相关产品推荐
相关产品推荐

