day17作业

2018-10-23  本文已影响0人  逆流而上_2eb6

头像


image.png
import pygame
import random
def color():
    return (random.randint(0,255),random.randint(0,255),random.randint(0,255))
screen = pygame.display.set_mode((800,600))
x1 = 1
def d(rect,x,y):
    global x1
    if x1 ==10:
        return
    x1 +=1
    pygame.draw.rect(screen,color(),(*rect,x,y),0)
    pygame.draw.ellipse(screen,color(),(*rect,x,y),0)
    pygame.draw.polygon(screen,(0,200,100),[(rect[0]+x*0.5,rect[1]),(rect[0]+x,rect[1]+y*0.5),\
                        (rect[0]+x*0.5,rect[1]+y),(rect[0],rect[1]+y*0.5)],0)
    pygame.display.flip()
    d((rect[0]+x*0.25,rect[1]+y*0.25),x*0.5,y*0.5)

while 1:
    d((0,0),800,400)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()

画板
bug:先前画的线会被画圆和画直线的操作给擦掉(重合时相当于橡皮檫)

import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
screen.fill((255,255,255))
font= pygame.font.Font("./aa.ttf",50)
function = "画线"
while True:
    # pygame.draw.rect(screen,(255,255,0),(0,50,800,550),0)
    surface = pygame.draw.lines(screen,(255,255,0),True,[(5,55),(790,55),(790,590),(5,590)],5)
    if function == "画线":
        text = font.render("画线", True, (255, 0, 0))
        screen.blit(text, (0, 0))
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN :
                last_pos = event.pos
                bool1 =True
                while bool1:
                    for event1 in pygame.event.get():
                        if event1.type == pygame.MOUSEMOTION:
                            pygame.draw.line(screen, (0, 0, 0),last_pos,event1.pos, 5)
                            last_pos = event1.pos
                            pygame.display.flip()
                        if event1.type == pygame.MOUSEBUTTONUP:
                            bool1 = False

            if event.type == pygame.QUIT :
                    exit()
            elif event.type == pygame.KEYDOWN:
                if chr(event.key)=="r":
                    pygame.draw.rect(screen,(255,255,255),(0,0,800,50),0)
                    function = "画直线"
    if function =="画直线":
        text = font.render("画直线", True, (255, 0, 0))
        screen.blit(text, (0, 0))
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                start_pos = event.pos
                last_pos = event.pos
                bool1 = True
                while bool1:
                    for event in pygame.event.get():
                        if event.type == pygame.MOUSEMOTION:
                            pygame.draw.line(screen, (255, 255, 255), start_pos, last_pos, 3)
                            # 覆盖上一次的线
                            pygame.draw.line(screen, (0, 0, 0), start_pos, event.pos,3)
                            # 画出新的线
                            last_pos = event.pos
                            pygame.display.flip()
                        elif event.type == pygame.MOUSEBUTTONUP:
                            bool1 = False
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.KEYDOWN:
                if chr(event.key) == "r":
                    function = "画圆"
                    pygame.draw.rect(screen,(255,255,255),(0,0,800,50),0)
    if function =="画圆":
        text = font.render("画圆", True, (255, 0, 0))
        screen.blit(text, (0, 0))
        for event in pygame.event.get():
            if event.type == pygame.MOUSEBUTTONDOWN:
                start_pos = event.pos
                r=0
                bool1 = True
                while bool1:
                    for event in pygame.event.get():
                        if event.type == pygame.MOUSEMOTION:
                            pygame.draw.circle(screen, (255, 255, 255), start_pos, int(r), 0)
                            # 覆盖上一次的rr圆
                            last_pos = event.pos
                            r = ((last_pos[0]-start_pos[0])**2+(last_pos[1]-start_pos[1])**2)**0.5
                            pygame.draw.circle(screen, (0,255, 255),start_pos,int(r),1)
                            # 画出新的圆
                            pygame.display.flip()
                        elif event.type == pygame.MOUSEBUTTONUP:
                            bool1 = False
            if event.type == pygame.QUIT:
                exit()
            elif event.type == pygame.KEYDOWN:
                if chr(event.key) == "r":
                    function = "画线"
                    pygame.draw.rect(screen,(255,255,255),(0,0,800,50),0)
    pygame.display.flip()

上一篇下一篇

猜你喜欢

热点阅读