import pygame
import sys
import datetime
class Display:
pygame.init() # 初始化
username = None
font_kele = pygame.font.Font('kele.ttf', 26)
id = None
music = False
display_width = 1300 # 窗口宽度
display_height = 700 # 窗口高度
mouse = False
game_display = pygame.display.set_mode((display_width, display_height)) # 显示窗口
clock = pygame.time.Clock()
time_passed = clock.tick(30)
points = []
x = 0
y = 0
size = 5
color = (255, 255, 255)
is_in_colorbox = pygame.Rect(0, 0, 150, display_height)
R = 150
B = 150
G = 150
is_R = False
is_G = False
is_B = False
R_button = pygame.Rect(20, 150, 30, 255 * 2)
G_button = pygame.Rect(60, 150, 30, 255 * 2)
B_button = pygame.Rect(100, 150, 30, 255 * 2)
def screemshot(self):#截图函数
now = datetime.datetime.now()
filename = now.strftime("%m-%d %H-%M")
print(now.strftime("%Y-%m-%d %H:%M:%S"))
img = pygame.Surface((1300,700))
img.blit(self.game_display,(0,0),((0,0),(1300,700)))
pygame.image.save(img, "image\\"+filename+".png")
def colorbox(self):
self.game_display.blit(self.font_kele.render('红 绿 蓝', True, (50, 50, 50)), (22, 120))
pygame.draw.rect(self.game_display, (222, 222, 222), (20, 150, 30, 255 * 2), border_radius=3) # 调色背景
pygame.draw.rect(self.game_display, (222, 222, 222), (60, 150, 30, 255 * 2), border_radius=3)
pygame.draw.rect(self.game_display, (222, 222, 222), (100, 150, 30, 255 * 2), border_radius=3)
pygame.draw.rect(self.game_display, (255, 0, 0), (20, 150, 30, self.R * 2), border_radius=3) # 当前颜色
pygame.draw.rect(self.game_display, (0, 255, 0), (60, 150, 30, self.G * 2), border_radius=3)
pygame.draw.rect(self.game_display, (0, 0, 255), (100, 150, 30, self.B * 2), border_radius=3)
pygame.draw.rect(self.game_display, (255, 255, 255), (20, 150, 30, 255 * 2), 3, border_radius=3) # 调色边框
pygame.draw.rect(self.game_display, (255, 255, 255), (60, 150, 30, 255 * 2), 3, border_radius=3)
pygame.draw.rect(self.game_display, (255, 255, 255), (100, 150, 30, 255 * 2), 3, border_radius=3)
pygame.draw.circle(self.game_display, self.color, (70, 60), 50) # 显示当前颜色
pygame.draw.circle(self.game_display, (150, 150, 150), (70, 60), 50, 5)
def drawline(self): # 画线函数
if self.points == []:
return True
for i in range(len(self.points) - 1):
if self.points[i][0] == (-1, -1) or self.points[i + 1][0] == (-1, -1):
continue
else:
pygame.draw.line(self.game_display, self.points[i][1], self.points[i][0], self.points[i + 1][0],
self.points[i][2])
pygame.display.update()
def nextp(self): # 判断下一个点的函数
if self.points == []:
return True
(last_x, last_y) = self.points[-1][0]
if (last_x - self.x) ** 2 + (last_y - self.y) ** 2 >= (self.size * 3) ** 2:
return True
def back(self): # 撤销上一步
while True:
if self.points == []:
return
self.points.pop(-1)
if self.points == [] or self.points[-1][0] == (-1, -1):
return
def draw(self):
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_BACKSPACE:
self.back(self)
print(len(self.points))
# if event.key == pygame.K_UP:
# if self.B_button.width < 30:
# self.B_button.width += 1
# if event.key == pygame.K_DOWN:
# if self.B_button.width > 10:
# self.B_button.width -= 1
if event.key == pygame.K_SPACE:
self.watch(self)
if event.key == pygame.K_b:
self.screemshot(self)
if event.key == pygame.K_UP:
if not self.mouse:
self.size = self.size + 3 if self.size <10 else self.size
if event.key == pygame.K_DOWN:
if not self.mouse:
self.size = self.size - 3 if self.size > 5 else self.size
if event.key == pygame.K_LEFT:
self.screemshot(self)
if event.key == pygame.K_RIGHT:
self.screemshot(self)
if event.type == pygame.MOUSEBUTTONDOWN:
if self.R_button.collidepoint(pos):
self.is_R = True
if self.G_button.collidepoint(pos):
self.is_G = True
if self.B_button.collidepoint(pos):
self.is_B = True
elif not self.is_in_colorbox.collidepoint(pos):
self.mouse = True
if event.type == pygame.MOUSEBUTTONUP:
self.is_R = False
self.is_G = False
self.is_B = False
self.mouse = False
if not self.is_in_colorbox.collidepoint(pos):
self.points.append(((-1, -1), (0, 0, 0),0))
self.x, self.y = pygame.mouse.get_pos()
self.game_display.fill((250, 250, 200))
if (pygame.mouse.get_pos()[1] - self.R_button.y) >= 0 and (pygame.mouse.get_pos()[1] - self.R_button.y) <= 510:
if self.is_R:
self.R = (pygame.mouse.get_pos()[1] - self.R_button.y) / 2
if self.is_G:
self.G = (pygame.mouse.get_pos()[1] - self.G_button.y) / 2
if self.is_B:
self.B = (pygame.mouse.get_pos()[1] - self.B_button.y) / 2
self.color = (self.R, self.G, self.B)
if self.mouse:
if self.points == [] or self.nextp(self):
self.points.append(((self.x, self.y), (self.color),self.size))
pygame.draw.rect(self.game_display, (200, 200, 150), self.is_in_colorbox)
self.colorbox(self)
pygame.draw.circle(self.game_display, self.color,(self.x,self.y), self.size/2) # 显示当前颜色
self.drawline(self)
pygame.display.update()
def main(self):
while True:
self.draw(self)
drawer = Display
drawer.main(drawer)
评论1