您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“Pygame鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放怎么實(shí)現(xiàn)”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“Pygame鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放怎么實(shí)現(xiàn)”吧!
# -*- 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 # 寫(xiě)一個(gè)函數(shù),判斷一個(gè)點(diǎn)是否在某個(gè)范圍內(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)變成可以移動(dòng) 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)變成不可以移動(dòng) if event.type == pygame.MOUSEBUTTONUP: is_move = False # 鼠標(biāo)移動(dòng)對(duì)應(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)位置以及是否摁了下去 #這里可以寫(xiě)按鈕響應(yīng)的功能 pygame.quit()#關(guān)閉原來(lái)窗口 #os.system('ui.py') run_flag = False#跳出循環(huán)(不然會(huì)報(bào)錯(cuò)) #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__':并把對(duì)應(yīng)的值傳進(jìn)對(duì)應(yīng)的位置去
到此,相信大家對(duì)“Pygame鼠標(biāo)進(jìn)行圖片的移動(dòng)與縮放怎么實(shí)現(xiàn)”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。