-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArcTest.py~
83 lines (66 loc) · 3.05 KB
/
ArcTest.py~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import random
from math import hypot,atan2,cos,sin,pi,sqrt,degrees
import pygame, sys
from pygame.locals import *
import time
def betterArc(surf, color, (x,y,w,h), start, end, width=1):
trans = [0,0,0,0]
tmpSurf = pygame.Surface((w,h),SRCALPHA)
pygame.draw.ellipse(tmpSurf,color,(0,0,w,h))
if width:#Hollow the ellipse if needed.
pygame.draw.ellipse(tmpSurf,trans,(width,width,w-width*2,h-width*2))
#rotate backwards to line start up with 0, then chop off the bottom half, giving arc: start->start+pi
rotSurf = pygame.transform.rotate(tmpSurf,-degrees(start))
pygame.draw.rect(rotSurf,trans,(0,rotSurf.get_height()/2,rotSurf.get_width(),rotSurf.get_height()))
rotSurf = pygame.transform.rotate(rotSurf,degrees(start))
if end-start > pi:#we need more than 180 degrees
#Draw the half arc we have. (start->start+pi)
transx = (rotSurf.get_width()-w)/2
transy = (rotSurf.get_height()-h)/2
surf.blit(rotSurf,(x-transx,y-transy))
#Cut off the remaning arc from the half we have.
rotAngle = degrees(pi - end)
rotSurf = pygame.transform.rotate(rotSurf,rotAngle)
pygame.draw.rect(rotSurf,trans,(0,0,rotSurf.get_width(),rotSurf.get_height()/2))
rotSurf = pygame.transform.rotate(rotSurf,180-rotAngle)
#Draw the second part.
transx = (rotSurf.get_width()-w)/2
transy = (rotSurf.get_height()-h)/2
surf.blit(rotSurf,(x-transx,y-transy))
else:#We don't need all of this arc
#Cut off the end point to end+pi (leaving you with start->end)
rotAngle = degrees(pi-end)
rotSurf = pygame.transform.rotate(rotSurf,rotAngle)
pygame.draw.rect(rotSurf,trans,(0,rotSurf.get_height()/2,rotSurf.get_width(),rotSurf.get_height()/2))
rotSurf = pygame.transform.rotate(rotSurf,-rotAngle)
#Draw our arc.
transx = (rotSurf.get_width()-w)/2
transy = (rotSurf.get_height()-h)/2
surf.blit(rotSurf,(x-transx,y-transy))
pygame.init()
fpsClock = pygame.time.Clock()
window = pygame.display.set_mode((200,200))
pygame.display.set_caption("ArcTest")
arc = 0
start = 0
while True:
for event in pygame.event.get():
if event.type == QUIT or event.type == MOUSEBUTTONDOWN:
pygame.quit()
sys.exit()
window.fill([0,255,0])
#Arc with a width
betterArc(window,[0,0,255],(0,0,100,200),start,start+arc,20)
pygame.draw.arc(window,[0,0,0],(100,0,100,200),start,start+arc,20)
#Defaults to a width of 1, like pygames
betterArc(window,[255,0,0],(0,0,100,200),start,start+arc)
pygame.draw.arc(window,[255,255,255],(100,0,100,200),start,start+arc)
#Also, if 0 or None, etc. It fills the entire arc
betterArc(window,[255,0,0],(30,30,40,140),start,start+arc,0)
pygame.draw.arc(window,[255,0,0],(130,30,40,140),start,start+arc,20)
#Update stuff
pygame.display.update()
dt = fpsClock.tick(30)/1000.0
arc = (arc+dt*1.5)%(pi*2)
start = (start + dt)%(pi*2)