python - Pygame - 预览图片

标签 python python-2.7 pygame

首先,这是一项学校作业,所以我想提前说明一下。其次,我只是寻求有关方法的建议,可能对代码有帮助。我正在使用我们书中的一些预先存在的代码来开发 MSPAINT 样式的克隆。代码中已经有按下鼠标键1时draw.line的使用,老师要我们增加画圆或长方形的能力。我正在研究圆圈部分,我已经想出(感谢这里的论坛)如何实现我想用 MOUSEBUTTONDOWN 和 MOUSEBUTTONUP 事件做的事情。这让我想到了一个新问题。我怎么会blit 然后删除然后 blit 预览圆圈直到它是用户想要的大小然后他们释放鼠标按钮并查看最终的 blit ...

while keepGoing:
    clock.tick(30)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keepGoing = False
        elif event.type == pygame.MOUSEMOTION:
            lineEnd = pygame.mouse.get_pos()
            if pygame.mouse.get_pressed() == (1,0,0):
                pygame.draw.line(background, drawColor, lineStart, lineEnd, lineWidth)
            lineStart = lineEnd
        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:
                    circleStart = pygame.mouse.get_pos()
        elif event.type == pygame.MOUSEBUTTONUP and event.button == 3: 
                    circleEnd = pygame.mouse.get_pos()
                    size = (circleEnd[0] - circleStart[0])
                    pygame.draw.circle(background, drawColor, circleStart, size, lineWidth)
        elif event.type == pygame.KEYDOWN:
            myData = (event, background, drawColor, lineWidth, keepGoing)
            myData = checkKeys(myData)
            event, background, drawColor, lineWidth, keepGoing) = myData

非常感谢

-本

最佳答案

所以经过一番思考,这是我使用 pygame 想出的最佳解决方案。告诉我您的想法以及它是否对您有所帮助。

import pygame,sys,math #---- Import modules we will need

pygame.init() #---- Initialize the module

def get_rad(origin_x,origin_y,x,y): #----- Returns the appropriate radius
    return math.sqrt((origin_x - x)**2 + (origin_y - y)**2) #----- Distance between 2
                                                            #----- points

screen = pygame.display.set_mode((400,400)) #----- Sets up the screen
clock  = pygame.time.Clock() #------- Sets up the clock

mouse_button = 0 #--------- This variable is used to determine whether a mouse button
                 #--------- has been pressed

draw_final_circle = False #---------- This variable lets us know that we should draw the 
                          #---------- final circle

while True: #------ main loop

    clock.tick(60) #------ Limit the Fps

    mouse_button0  = mouse_button #-------- This variable holds the previous value of 
                                  #-------- mouse_button(it will be useful later)

    mouse_x,mouse_y = pygame.mouse.get_pos() #----- Get the mosue coordinates

    for e in pygame.event.get(): #---- Cycle through events

        if e.type == pygame.QUIT: pygame.quit();sys.exit() #--Quit when window is closed

        if e.type == pygame.MOUSEBUTTONDOWN: #---- If the mouse button is pressed
            if mouse_button == 0: #---- if the mouse button is released
                mouse_button = 1 #----- set it to pressed basically
                originx,originy = mouse_x,mouse_y #---- keep the mouse_x,mouse_y pos

        if e.type == pygame.MOUSEBUTTONUP: #---- if the mouse button is released
             if mouse_button == 1: #-------- if it is pressed
                 mouse_button = 0 #--------- set it to released

    screen.fill((255,255,255)) #---- clear the screen


    #-------- If a mouse button is pressed and a circle can be drawn (rad>width) ------#
    if mouse_button == 1 and get_rad(originx,originy,mouse_x,mouse_y) > 1:

        rad = int(get_rad(originx,originy,mouse_x,mouse_y)) #---- get the radius(as int)
        pos = mouse_x,mouse_y
        pygame.draw.circle(screen,(0,0,0),pos,rad,1) #--- draw the circle

    #----------------------------------------------------------------------------------#


    #---------- if the button is released but in the previous loop it was pressed -----#
    if mouse_button == 0 and mouse_button0 == 1:

        draw_final_circle = True #----- set the final circle boolean to True

    if draw_final_circle: #----- if the final circle is decided
        pygame.draw.circle(screen,(0,0,0),pos,rad,1) #---- keep drawing it 

    pygame.display.flip() #----- flip the buffer

关于python - Pygame - 预览图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22403203/

相关文章:

python - 以秒为单位获取python单元测试持续时间

python - 从 Django 数据库中提取数据

python - Sprite不显示pygame

python - 如何在Python中排序堆积条形图?

python - 如何在Python 2.7.1中将一串字母变成3个字母的单词

python - 列表、字符串和快速排序

python-2.7 - 如何使用 Bokeh 制作圆形动画

python-2.7 - 使用 Python 2 和 3 安装附加包 Jupyter

python - 如何相对于其中心缩放 PyGame 图像(表面)?

python - Pygame 中的抗锯齿形状