第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在PyGame中拖動(dòng)多個(gè)圖像?

如何在PyGame中拖動(dòng)多個(gè)圖像?

慕田峪4524236 2023-12-26 16:25:02
import pygamefrom pygame.locals import *pygame.display.init()screen = pygame.display.set_mode((1143,677 ))img = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")imgPos = pygame.Rect((0, 0), (0, 0))LeftButton = 0while 1: for e in pygame.event.get():    if e.type == QUIT: exit(0)    if e.type == MOUSEMOTION:        if e.buttons[LeftButton]:            rel = e.rel            imgPos.x += rel[0]            imgPos.y += rel[1]screen.fill(0)screen.blit(img, imgPos)pygame.display.flip()pygame.time.delay(30)我試圖用多個(gè)圖像來(lái)實(shí)現(xiàn)這一點(diǎn),并且圖像總是重疊,并且只能移動(dòng)一個(gè)圖像,所以嘗試了以下方法: import pygame from pygame.locals import * pygame.display.init() screen = pygame.display.set_mode((1143,677 )) img = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png") img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\03.png") imgPos = pygame.Rect((0, 0), (0, 0)) imgPos1 = pygame.Rect((1, 1), (1, 1)) LeftButton = 0 while 1:     for e in pygame.event.get():    if e.type == QUIT: exit(0)    if e.type == MOUSEMOTION:        if e.buttons[LeftButton]:            rel = e.rel            imgPos.x += rel[0]            imgPos.y += rel[1]screen.fill(0)screen.blit(img, imgPos)screen.blit (img1, imgPos1)pygame.display.flip()pygame.time.delay(30)所以圖像重疊并且可以移動(dòng)第二個(gè)圖像,我想讓兩個(gè)圖像用鼠標(biāo)移動(dòng),我希望分別移動(dòng)圖像
查看完整描述

1 回答

?
蝴蝶不菲

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

PyGame 中的單位是像素。您的對(duì)象重疊,因?yàn)榇怪焙退轿恢玫牟町愔挥幸粋€(gè)像素。


imgPos = pygame.Rect((0, 0), (0, 0))

imgPos1 = pygame.Rect((1, 1), (1, 1))

增加對(duì)象的距離并根據(jù)相應(yīng)圖像的大小設(shè)置矩形的大小。例如


imgPos = pygame.Rect((0, 0), img.get_size())

offsetX = img.get_width()

imgPos1 = pygame.Rect((offsetX, 0), img1.get_size())

或者,可以從 by生成pygame.Rect對(duì)象。矩形的位置必須由關(guān)鍵字參數(shù)設(shè)置:pygame.Surfaceget_rect

imgPos?=?img.get_rect(topleft?=?(0,?0))
imgPos1?=?img1.get_rect(topleft?=?(img.get_width(),?0))

如果要選擇要移動(dòng)的圖像,則需要添加一個(gè)變量來(lái)指示當(dāng)前選擇的圖像:

current_image?=?None

單擊圖像時(shí),會(huì)發(fā)生變化current_image。用于collidepoint()驗(yàn)證鼠標(biāo)是否單擊了圖像:

if e.type == pygame.MOUSEBUTTONDOWN:

? ? if imgPos.collidepoint(e.pos):

? ? ? ? current_image = 0

? ? elif imgPos1.collidepoint(e.pos):

? ? ? ? current_image = 1

? ? else:?

? ? ? ? current_image = None

移動(dòng)img如果current_image == 0和移動(dòng)img1如果current_image == 1:


if e.type == MOUSEMOTION:

? ? if e.buttons[LeftButton]:

? ? ? ? rel = e.rel

? ? ? ? if current_image == 0:

? ? ? ? ? ? imgPos.x += rel[0]

? ? ? ? ? ? imgPos.y += rel[1]

? ? ? ? elif current_image == 1:

? ? ? ? ? ? imgPos1.x += rel[0]

? ? ? ? ? ? imgPos1.y += rel[1]

import pygame

from pygame.locals import *


pygame.display.init()

screen = pygame.display.set_mode((1143,677 ))


img = pygame.image.load(r"C:\Users\ga-sa\Downloads\As.png")

img1 = pygame.image.load(r"C:\Users\ga-sa\Downloads\03.png")

imgPos = img.get_rect(topleft = (20, 20))

imgPos1 = img1.get_rect(topleft = (60, 20))

current_image = None


LeftButton = 0

while 1:

? ? for e in pygame.event.get():

? ? ? ? if e.type == QUIT:

? ? ? ? ? ? pygame.quit()

? ? ? ? ? ? exit(0)


? ? ? ? if e.type == pygame.MOUSEBUTTONDOWN:

? ? ? ? ? ? if imgPos.collidepoint(e.pos):

? ? ? ? ? ? ? ? current_image = 0

? ? ? ? ? ? elif imgPos1.collidepoint(e.pos):

? ? ? ? ? ? ? ? current_image = 1

? ? ? ? ? ? else:?

? ? ? ? ? ? ? ? current_image = None


? ? ? ? if e.type == MOUSEMOTION:

? ? ? ? ? ? if e.buttons[LeftButton]:

? ? ? ? ? ? ? ? rel = e.rel

? ? ? ? ? ? ? ? if current_image == 0:

? ? ? ? ? ? ? ? ? ? imgPos.x += rel[0]

? ? ? ? ? ? ? ? ? ? imgPos.y += rel[1]

? ? ? ? ? ? ? ? elif current_image == 1:

? ? ? ? ? ? ? ? ? ? imgPos1.x += rel[0]

? ? ? ? ? ? ? ? ? ? imgPos1.y += rel[1]

? ?

? ? screen.fill(0)

? ? screen.blit(img, imgPos)

? ? screen.blit (img1, imgPos1)

? ? pygame.display.flip()

? ? pygame.time.delay(30)



查看完整回答
反對(duì) 回復(fù) 2023-12-26
  • 1 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)