|
# 导入
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)))
# 在屏幕上写出1,2,3,4 应该没问题
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]找不到解决办法,求助
|
|