亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

pygame鼠標(biāo)進(jìn)行拖拽移動圖片、縮放、以及按鈕響應(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ù),判斷一個點(diǎn)是否在某個范圍內(nèi)
# 點(diǎn)(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è)置的路徑信息、不需要可以刪去、需要直接運(yùn)行可以改成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)、是點(diǎn)擊圖片的位置范圍進(jìn)行跳轉(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ù)覽圖大概是這樣:

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

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

相關(guān)文章

  • python通過實(shí)例講解反射機(jī)制

    python通過實(shí)例講解反射機(jī)制

    這篇文章主要介紹了python通過實(shí)例講解反射機(jī)制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • python+pytest接口自動化參數(shù)關(guān)聯(lián)

    python+pytest接口自動化參數(shù)關(guān)聯(lián)

    這篇文章主要介紹了python+pytest接口自動化參數(shù)關(guān)聯(lián),參數(shù)關(guān)聯(lián),也叫接口關(guān)聯(lián),即接口之間存在參數(shù)的聯(lián)系或依賴,更多相關(guān)內(nèi)容需要的小伙伴可可以參考一下
    2022-06-06
  • 使用rst2pdf實(shí)現(xiàn)將sphinx生成PDF

    使用rst2pdf實(shí)現(xiàn)將sphinx生成PDF

    這篇文章主要介紹了使用rst2pdf實(shí)現(xiàn)將sphinx生成PDF的相關(guān)資料,以及使用過程用遇到的錯誤的處理方法,非常的全面,需要的朋友可以參考下
    2016-06-06
  • Python實(shí)現(xiàn)井字棋小游戲

    Python實(shí)現(xiàn)井字棋小游戲

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)井字棋小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 在Pytorch中計算自己模型的FLOPs方式

    在Pytorch中計算自己模型的FLOPs方式

    今天小編就為大家分享一篇在Pytorch中計算自己模型的FLOPs方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 對python實(shí)現(xiàn)模板生成腳本的方法詳解

    對python實(shí)現(xiàn)模板生成腳本的方法詳解

    今天小編就為大家分享一篇對python實(shí)現(xiàn)模板生成腳本的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Pytorch平均池化nn.AvgPool2d()使用方法實(shí)例

    Pytorch平均池化nn.AvgPool2d()使用方法實(shí)例

    平均池化層,又叫平均匯聚層,下面這篇文章主要給大家介紹了關(guān)于Pytorch平均池化nn.AvgPool2d()使用方法的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • python實(shí)現(xiàn)大戰(zhàn)外星人小游戲?qū)嵗a

    python實(shí)現(xiàn)大戰(zhàn)外星人小游戲?qū)嵗a

    這篇文章主要介紹了python實(shí)現(xiàn)大戰(zhàn)外星人小游戲,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-12-12
  • python 捕獲shell腳本的輸出結(jié)果實(shí)例

    python 捕獲shell腳本的輸出結(jié)果實(shí)例

    下面小編就為大家?guī)硪黄猵ython 捕獲shell腳本的輸出結(jié)果實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01
  • PYQT5 實(shí)現(xiàn)給listwidget的滾動條添加滾動信號

    PYQT5 實(shí)現(xiàn)給listwidget的滾動條添加滾動信號

    這篇文章主要介紹了PYQT5 實(shí)現(xiàn)給listwidget的滾動條添加滾動信號,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論