找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 6496|回复: 3

[已解决] pygame:'Event' object has no attribute 'key'(已解决)

5

主题

12

帖子

12

积分

贫民

积分
12
TheSkyyy 发表于 2016-12-11 15:41:57 | 显示全部楼层 |阅读模式
# 导入
import math as m
import pygame as p
import sys
from pygame.locals import *
import random as r
# pygame初始化及设置一些必要参数
p.init()
screen = p.display.set_mode((
600, 500))
p.display.set_caption(
'Pie')
myfont = p.font.Font(
None, 60)

color = r.randint(
0, 255), r.randint(0, 255), r.randint(0, 255)
width =
4
x = 300
y = 250
radius = 200
#弧形所需pos
pos = x-radius, y-radius, radius*2, radius*2
#四个弧形判定量
piece1 = False
piece2 = False
piece3 = False
piece4 = False
while True:
   
for event in p.event.get():
        
if event.type == QUIT:
            sys.exit()
        
elif event.type == KEYUP:
            
if event.key == p.K_ESCAPE:
                sys.exit()
            
elif event.key == p.K_1:         #出错行
                piece1 =
True
            elif event.key == p.K_2:
                piece2 =
True
            elif event.key == p.K_3:
                piece3 =
True
            elif event.key == p.K_4:
                piece4 =
True
# 屏幕底色
screen.fill((r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)))

   
# 在屏幕上写出1234 应该没问题
textImg1 = myfont.render('1', True, color)
    screen.blit(textImg1, (x + radius /
2, y - radius / 2))
    textImg2 = myfont.render(
'2', True, color)
    screen.blit(textImg2, (x - radius /
2, y - radius / 2))
    textImg3 = myfont.render(
'3', True, color)
    screen.blit(textImg3, (x - radius /
2, y + radius / 2))
    textImg4 = myfont.render(
'4', True, color)
    screen.blit(textImg4, (x + radius /
2, y + radius / 2))

   
# 判定及画出弧形及所需的直线
if piece1:
        start_angle = m.radians(
0)
        end_angle = m.radians(
90)
        p.draw.arc(screen, color, pos, start_angle, end_angle, width)
        p.draw.line(screen, color, (x, y), (x + radius, y), width)
        p.draw.line(screen, color, (x, y), (x, y + radius), width)
   
if piece2:
        start_angle = m.radians(
90)
        end_angle = m.radians(
180)
        p.draw.arc(screen, color, pos, start_angle, end_angle, width)
        p.draw.line(screen, color, (x,y), (x-radius, y), width)
   
if piece3:
        start_angle = m.radians(
180)
        end_angle = m.radians(
270)
        p.draw.arc(screen, color, pos, start_angle, end_angle, width)
        p.draw.line(screen, color, (x, y), (x + radius, y + radius), width)
   
if piece4:
        start_angle = m.radians(
270)
        end_angle = m.radians(
360)
        p.draw.arc(screen, color, pos, start_angle, end_angle, width)

   
if piece1 and piece2 and piece3 and piece4:
        color = r.randint(
0, 255), r.randint(0, 255), r.randint(0,255)

    p.display.update()



[size=16.6667px]Traceback (most recent call last):  File "F:/pycharm/pygame_pie.py", line 30, in <module>    elif event.key == p.K_1:AttributeError: 'Event' object has no attribute 'key'
[size=16.6667px]写一个简单的小游戏,就是一个一个的画1/4弧形,每一步必须成功才能做下一步。[size=16.6667px]找不到解决办法,求助

回复

使用道具 举报

50

主题

1057

帖子

1108

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1108

活跃会员热心会员最佳导师默默耕耘突出贡献优秀版主荣誉管理论坛元老

blueelwang 发表于 2016-12-13 08:11:21 | 显示全部楼层
good
回复

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
ccczz 发表于 2020-5-27 21:35:22 | 显示全部楼层
楼主你好请问你的解决了吗我也遇到一样的问题了能不能告诉一下我
回复 支持 反对

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
ccczz 发表于 2020-5-27 21:57:52 | 显示全部楼层

大佬能不能详细说一下哪里错了,我是初学着,谢谢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表