最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Pygame鼠標(biāo)進行圖片的移動與縮放案例詳解

 更新時間:2021年12月24日 14:24:02   作者:高二水令  
pygame是Python的第三方庫,里面提供了使用Python開發(fā)游戲的基礎(chǔ)包。本文將介紹如何通過Pygame實現(xiàn)鼠標(biāo)進行圖片的移動與縮放,感興趣的可以關(guān)注一下

pygame鼠標(biāo)進行拖拽移動圖片、縮放、以及按鈕響應(yīng) 案例

# -*- coding: UTF-8 -*-
#!/usr/bin/env python3
# @Time    : 2021.12
# @Author  : 高二水令
# @Software: 圖層拖拽縮放
import os
import sys
import pygame
from pygame.locals import *


class Background(pygame.sprite.Sprite):
    def __init__(self, image_file, location):
        pygame.sprite.Sprite.__init__(self)  #call Sprite initializer
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = location
# 寫一個函數(shù),判斷一個點是否在某個范圍內(nèi)
# 點(x,y)
# 范圍 rect(x,y,w,h)
def is_in_rect(pos, rect):
    x, y = pos
    rx, ry, rw, rh = rect
    if (rx <= x <= rx+rw) and (ry <= y <= ry+rh):
        return True
    return False
def move_image(pic_bottom,pic_upper,ssn):
#pic_bottom,pic_upper分別是背景圖和上層拖拽圖層,ssn是我自己設(shè)置的路徑信息、不需要可以刪去、需要直接運行可以改成main()
    pygame.init()
    screen = pygame.display.set_mode((710, 520))
    BackGround = Background(pic_bottom, [0, 0])
    screen.fill((255, 255, 255))
    myimage = pygame.image.load('.\\next.png')
    myimage = pygame.transform.scale(myimage, (90, 40))
    myimage_x = 600
    myimage_y = 480
    scale_ = pygame.image.load('.\\Avel_scale.tif')
    scale_ = pygame.transform.scale(scale_, (70, 520))
    scale_x = 632
    scale_y = 0
    screen.blit(BackGround.image, BackGround.rect)
    screen.blit(scale_, (scale_x, scale_y))
    screen.blit(myimage, (myimage_x, myimage_y))
    pygame.display.set_caption('圖像定標(biāo)')
    size = []
    location = [0, 0]

    image = pygame.image.load(pic_upper)
    image_x = 100
    image_y = 100
    screen.blit(image,(image_x, image_y))
    pygame.display.flip()

    is_move = False
    run_flag = True
    while (run_flag==True):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

            # 鼠標(biāo)按下、讓狀態(tài)變成可以移動
            if event.type == pygame.MOUSEBUTTONDOWN:
                w,h = image.get_size()
                if is_in_rect(event.pos, (image_x, image_y, w, h)):
                    is_move = True

            # 鼠標(biāo)彈起、讓狀態(tài)變成不可以移動
            if event.type == pygame.MOUSEBUTTONUP:
                is_move = False


            # 鼠標(biāo)移動對應(yīng)的事件
            if event.type == pygame.MOUSEMOTION:
                if is_move:
                    screen.fill((255, 255, 255))
                    screen.blit(BackGround.image, BackGround.rect)
                    x, y = event.pos
                    image_w, image_h = image.get_size()
                    # 保證鼠標(biāo)在圖片的中心
                    image_y = y-image_h/2
                    image_x = x-image_w/2
                    screen.blit(scale_, (scale_x, scale_y))
                    screen.blit(myimage, (myimage_x, myimage_y))
                    screen.blit(image, (image_x, image_y))
                    #print(image.get_rect())
                    location[0]=event.pos[0]
                    location[1] = event.pos[1]
                    print(event.pos)
                    pygame.display.update()
			#鼠標(biāo)按鈕響應(yīng)、是點擊圖片的位置范圍進行跳轉(zhuǎn)
            if event.type == pygame.MOUSEBUTTONDOWN and myimage_x <= event.pos[0] <= myimage_x + 90 and \
                    myimage_y <= event.pos[1] <= myimage_y + 40:  # 判斷鼠標(biāo)位置以及是否摁了下去
					#這里可以寫按鈕響應(yīng)的功能
					
                    pygame.quit()#關(guān)閉原來窗口
                    #os.system('ui.py')
                    run_flag = False#跳出循環(huán)(不然會報錯)
                    #sys.exit()
             #滾輪縮放
            if event.type == MOUSEWHEEL:
                screen.fill((255, 255, 255))
                screen.blit(BackGround.image, BackGround.rect)
                image_width = image.get_width()
                image_heigt = image.get_height()
                image = pygame.transform.scale(image, (
                    image_width + event.y * image_width / image_heigt * 10, image_heigt + event.y * 10))
                screen.blit(scale_, (scale_x, scale_y))
                screen.blit(myimage, (myimage_x, myimage_y))
                screen.blit(image, (image_x, image_y))
                #print(event)
                print(image_width, image_heigt)
                #print(event.flipped)
                pygame.display.update()


預(yù)覽圖大概是這樣:

如需直接運行就直接把def move_image(pic_bottom,pic_upper,ssn)這句改成if __name__ == '__main__':并把對應(yīng)的值傳進對應(yīng)的位置去?

到此這篇關(guān)于Pygame鼠標(biāo)進行圖片的移動與縮放案例詳解的文章就介紹到這了,更多相關(guān)Pygame圖片的移動與縮放內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python中django框架通過正則搜索頁面上email地址的方法

    python中django框架通過正則搜索頁面上email地址的方法

    這篇文章主要介紹了python中django框架通過正則搜索頁面上email地址的方法,涉及django框架及正則表達式的使用技巧,需要的朋友可以參考下
    2015-03-03
  • python RC4加密操作示例【測試可用】

    python RC4加密操作示例【測試可用】

    這篇文章主要介紹了python RC4加密操作,結(jié)合實例形式分析了python實現(xiàn)RC4加密功能的具體操作步驟與相關(guān)問題解決方法,需要的朋友可以參考下
    2019-09-09
  • Python中實現(xiàn)優(yōu)雅的switch操作的方法小結(jié)

    Python中實現(xiàn)優(yōu)雅的switch操作的方法小結(jié)

    這篇文章主要為大家詳細(xì)介紹了如何在Python中優(yōu)雅地實現(xiàn)?switch?操作,并提供豐富的示例代碼,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • python使用arcpy.mapping模塊批量出圖

    python使用arcpy.mapping模塊批量出圖

    出圖是項目里常見的任務(wù),有的項目甚至?xí)习購垐D片,所以批量出土工具很有必要,這篇文章主要介紹了python使用arcpy.mapping模塊批量出圖,有興趣的可以了解一下。
    2017-03-03
  • TensorFlow人工智能學(xué)習(xí)數(shù)據(jù)類型信息及轉(zhuǎn)換

    TensorFlow人工智能學(xué)習(xí)數(shù)據(jù)類型信息及轉(zhuǎn)換

    這篇文章主要為大家介紹了TensorFlow人工智能學(xué)習(xí)數(shù)據(jù)類型信息及轉(zhuǎn)換,
    2021-11-11
  • python中prettytable庫的使用方法

    python中prettytable庫的使用方法

    prettytable是Python的一個第三方工具庫,用于創(chuàng)建漂亮的ASCII表格,本文主要介紹了python中prettytable庫的使用方法,使用prettytable可以輕松地將數(shù)據(jù)可視化為表格,感興趣的可以了解一下
    2023-08-08
  • Django csrf 驗證問題的實現(xiàn)

    Django csrf 驗證問題的實現(xiàn)

    csrf是通過偽裝來自受信任用戶的請求來利用受信任的網(wǎng)站。這篇文章主要介紹了Django csrf 驗證問題的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • Python海龜繪圖(Turtle)應(yīng)用指南

    Python海龜繪圖(Turtle)應(yīng)用指南

    python2.6版本中后引入的一個簡單的繪圖工具,叫做海龜繪圖(Turtle?Graphics)。海龜繪圖(turtle庫)是python的內(nèi)部模塊,使用前導(dǎo)入即可。本文將展示三個通過海龜繪圖實現(xiàn)的小程序,快來跟隨小編一起學(xué)習(xí)吧
    2022-03-03
  • django xadmin中form_layout添加字段顯示方式

    django xadmin中form_layout添加字段顯示方式

    這篇文章主要介紹了django xadmin中form_layout添加字段顯示方式,具有很好的 參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • 淺談Python中的繼承

    淺談Python中的繼承

    這篇文章主要介紹了Python中繼承的的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06

最新評論

乌拉特后旗| 长沙市| 长乐市| 驻马店市| 安塞县| 乐至县| 呼图壁县| 中宁县| 城口县| 黄龙县| 清涧县| 五家渠市| 阜宁县| 武强县| 汪清县| 延长县| 石狮市| 广平县| 汉源县| 廉江市| 嘉义市| 光山县| 寿阳县| 遂宁市| 马鞍山市| 万州区| 岳普湖县| 公安县| 鄂温| 桂东县| 南木林县| 鹿泉市| 贵溪市| 邵东县| 迁安市| 平山县| 桦甸市| 宿迁市| 辽阳市| 蒲江县| 格尔木市|