format小数
":.2f".format(3.1415926)
计算机进行三角函数运算时候非常消耗资源,因此可以利用程序加载时间把结果提前运算出来存在数组中.
绘制圆圈
import random, math, sys, pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("CIRCLE!")
screen.fill((0,0,100))
pos_x = 300
pos_y = 250
radius = 200
angle = 360
color = 0,0,0
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
angle += 1
if angle >= 360:
angle = 0
r = random.randint(0,255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = r, g, b
x = math.cos(math.radians(angle)) * radius
y = math.sin(math.radians(angle)) * radius
pos = (int(pos_x + x), int(pos_y + y))
pygame.draw.circle(screen, color, pos, 10, 0)#这里的参数是指半径为10,实心的小圆圈。参数改成1之后能明显看出端倪
# pos = (300, 250)
# pygame.draw.circle(screen, color, pos, 200, 15)
pygame.display.update()
from datetime import datetime, date, time
import pygame, sys, random, math
from pygame.locals import *
def print_text(font, x, y, text, color=(255,255,255)):
imgText = font.render(text, True, color)
screen.blit(imgText, (x,y))
def wrap_angle(angle):
return abs(angle % 360)
def init_clock_plate( angle_plate=0):
lis = []
for i in range(360):
angle_plate += 1
# print(angle_plate)
x = math.cos(math.radians(angle_plate)) * radius
y = math.sin(math.radians(angle_plate)) * radius
pos = (int(pos_x + x), int(pos_y + y))
lis.append(pos)
return lis
pygame.init()
screen = pygame.display.set_mode((600,500))
white = 255,255,255
pos_x = 300
pos_y = 250
radius = 250
font = pygame.font.Font(None, 36)
orange = 220,180,0
yellow = 255,255,0
pink = 255,100,100
angle_plate = 0
color = 255,255,255 # 设置表盘外圈颜色
list_clock = init_clock_plate()
print(list_clock)
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
sys.exit()
screen.fill((0,0,0))
# 绘制表盘
for i in range(360):
pos = list_clock[i]
pygame.draw.circle(screen, color, pos, 10, 1)
# pygame.draw.circle(screen, white, (pos_x, pos_y), radius, 6)
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = r, g, b
# 绘制表盘数字
for n in range(1, 13):
angle = math.radians(n*(360/12)-90)
x = math.cos(angle)*(radius-20)-10
y = math.sin(angle)*(radius-20)-10
print_text(font, pos_x+x, pos_y+y, str(n))
today = datetime.today()
# print(today.hour)
if today.hour == 12:
hours = 12
else:
hours = today.hour % 12
minutes = today.minute
seconds = today.second
# 绘制时针
hour_angle = wrap_angle(hours*(360/12)-90)
hour_angle = math.radians(hour_angle)
hour_x = math.cos(hour_angle) * (radius - 80)
hour_y = math.sin(hour_angle) * (radius - 80)
target = (pos_x+hour_x, pos_y+hour_y)
pygame.draw.line(screen, pink, (pos_x,pos_y),target,25)
# 绘制分针,注意是60份
min_angle = wrap_angle(minutes * (360 / 60) - 90)
min_angle = math.radians(min_angle)
min_x = math.cos(min_angle) * (radius - 60)
min_y = math.sin(min_angle) * (radius - 60)
target = (pos_x + min_x, pos_y + min_y)
pygame.draw.line(screen, orange, (pos_x, pos_y), target, 12)
# 绘制秒针
sec_angle = wrap_angle(seconds * (360 / 60) - 90)
sec_angle = math.radians(sec_angle)
sec_x = math.cos(sec_angle) * (radius - 40)
sec_y = math.sin(sec_angle) * (radius - 40)
target = (pos_x + sec_x, pos_y + sec_y)
pygame.draw.line(screen, yellow, (pos_x, pos_y), target, 6)
#绘制中心圆
pygame.draw.circle(screen, white, (pos_x, pos_y),20,0)
print_text(font, 0,0, str(hours)+":"+str(minutes)+":"+str(seconds))
pygame.display.update()