HIT暑假python作业三《超级画板》
作业上交版代码编译版
程序运行效果**(运行时确保同目录下有文件夹img,里面有pen,eraser,plus,minus四个png图象,如果没有可以使用下文的编译版就不需要这四个图象)**
作业上交版代码
# -*- coding: utf-8 -*-
import pygame
#pyinstaller -F -p F:\pythoncode\venv\lib\site-packages homework3.py
colors = [(255,0,255),(0,0,255),(0,255,255),(0,255,0),(255,255,0),(255,0,0),(192,192,192),(128,128,128),
(128,0,128),(0,0,128),(0,128,128),(0,128,0),(128,128,0),(128,0,0),(0,0,0),(0,192,128),(255,255,255)]
def windowsupdate():
# screen.fill((100,100,100))
penimg = pygame.image.load('./img/pen.png')
eraserimg = pygame.image.load('./img/eraser.png')
plusimg = pygame.image.load('./img/plus.png')
minusimg = pygame.image.load('./img/minus.png')
screen.blit(penimg,penarea)
screen.blit(eraserimg,eraserarea)
screen.blit(plusimg,plusarea)
screen.blit(minusimg,minusarea)
pygame.draw.rect(screen,(0,0,0),previewimg,width=1)
pygame.draw.circle(screen,colors[nowcolor],(imgx+30,imgy+165),thickness/2)
for i in range(0,16):
pygame.draw.rect(screen,colors[i],colorareas[i])
# screen.blit(pygame.Surface.fill(color = colors[i],rect = colorareas[i]),colorareas[i])
pygame.display.update()#flip函数将重新绘制整个屏幕对应的窗口,update函数仅仅重新绘制窗口中有变化的区域。
print(pygame.version)
pygame.init()
pygame.display.set_caption('超级画板')
screen = pygame.display.set_mode(size = (640,480))
screen.fill((255, 255, 255))
imgx = 10
imgy = 10
penarea = pygame.Rect(imgx,imgy,32,32)
eraserarea = pygame.Rect(imgx,imgy+50,32,32)
plusarea = pygame.Rect(imgx,imgy+100,32,32)
minusarea = pygame.Rect(imgx+35,imgy+100,32,32)
previewimg = pygame.Rect(imgx,imgy+135,60,60)
colorareas = []
for i in range(0,8):
colorareas.append(pygame.Rect(imgx,imgy+200+30*i,30,30))
for i in range(0,8):
colorareas.append(pygame.Rect(imgx+30,imgy+200+15+30*i,30,30))
pencolor = 14#初始参数colors[14]为黑色
nowcolor = 14
thickness = 4
windowsupdate()#窗口更新
# pygame.draw.line(screen,colors[1],(100,100),(200,200),3)
# pygame.display.update()
while True:
event = pygame.event.wait()
# print(event)
if event.type == 1025:#如果鼠标有点击
x = event.__dict__['pos'][0]
y = event.__dict__['pos'][1]
if penarea.collidepoint(x,y) == 1:
nowcolor = pencolor
elif eraserarea.collidepoint(x,y) == 1:
nowcolor = 16
elif plusarea.collidepoint(x,y) == 1:
if thickness= 4:
thickness -= 2
pygame.draw.circle(screen, colors[16], (imgx + 30, imgy + 165), thickness / 2 + 1)
else:
for i in range(0,16):
if colorareas[i].collidepoint(x,y):
pencolor = i
nowcolor = i
break
elif event.type == 1024:#鼠标在移动状态
if event.__dict__['buttons'][0] == 1:#鼠标在左键点击状态
fromxy = (event.__dict__['pos'][0]-event.__dict__['rel'][0],event.__dict__['pos'][1]-event.__dict__['rel'][1])
pygame.draw.line(screen, colors[nowcolor], fromxy, event.__dict__['pos'], thickness)
elif event.type ==769:#按下了键盘
if event.__dict__['key'] == 27:#按下了esc
screen.fill(colors[16])#全部变为白色
elif event.type ==256:#按下了退出
break
windowsupdate()
编译版
该编译版本隐藏了cmd窗口(编译命令见第7行,记得把安装pyinstaller 模块,并且把-p后面的路径改为自己的库的路径)
# -*- coding: utf-8 -*-
# @Time : 2021/7/22 10:19
# @Author : hzh
# @File : homework3.py
# @Software : PyCharm
import pygame
#pyinstaller -F -w -p F:\pythoncode\venv\lib\site-packages homework3.py
imgwb = [b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x1c\x00\x00\x00\x1c\x08\x04\x00\x00\x00\xd8\x04\x17\x1f\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00u0\x00\x00\xea`\x00\x00:\x98\x00\x00\x17p\x9c\xbaQ:\\\x8c\x00\x00\x00%tEXtdate:create\x002018-04-19T10:33:30+08:00\xd4n\xa2\xc8\x00\x00\x00%tEXtdate:modify\x002018-04-19T10:33:30+08:00\xa53\x1at\x00\x00\x00NtEXtsoftware\x00ImageMagick 6.9.1-10 Q16 x86_64 2018-03-22 http://www.imagemagick.org\xc1\xfe\xac\x16\x00\x00\x00\x18tEXtThumb::Document::Pages\x001\xa7\xff\xbb/\x00\x00\x00\x18tEXtThumb::Image::Height\x00200}\xd7\x15i\x00\x00\x00\x17tEXtThumb::Image::Width\x00200\xee&E4\x00\x00\x00\x19tEXtThumb::Mimetype\x00image/png?\xb2VN\x00\x00\x00\x17tEXtThumb::MTime\x001524105210k\xf5\xa6p\x00\x00\x00\x0ftEXtThumb::Size\x000BB\x94\xa2>\xec\x00\x00\x00DtEXtThumb::URI\x00file:///workspace/tmp/mogr2_9_3625a7f8902eb9_18af65_50[0]\xa9\xc7\xb4\xbd\x00\x00\x00\x00IEND\xaeB`\x82',b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00 \x00\x00\x00 \x08\x04\x00\x00\x00\xd9s\xb2\x7f\x00\x00\x00\x04gAMA\x00\x00\xb1\x8f\x0b\xfca\x05\x00\x00\x00 cHRM\x00\x00z&\x00\x00\x80\x84\x00\x00\xfa\x00\x00\x00\x80\xe8\x00\x00u0\x00\x00\xea`\x00\x00:\x98\x00\x00\x17p\x9c\xbaQ |