如何在pygame中用鼠标移动图像?

2024-05-15 23:45:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一个小pygame程序,通过点击和移动鼠标来移动图像。

我努力使移动函数movableImg与我自己的x, y参数一起工作,而不是像现在这样与预定义的x, y参数一起工作。

这是我的代码:

import pygame
import time

pygame.init()

display_width = 800
display_height = 600

white = (255, 255, 255)

gameDisplay = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()

drag = 0    #switch with which I am seting if I can move the image
x = 100   #x, y coordinates of the image
y = 100

img = pygame.image.load('button.png')   #my image and then his width and height
imgWidth = 100
imgHeight = 100


def image(imgX,imgY):   #function to blit image easier
    gameDisplay.blit(img, (imgX, imgY))


def movableImg():   #function in which i am moving image
    global drag, x, y
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    image(x, y)

    if click[0] == 1 and x + imgWidth > mouse[0] > x and y + imgHeight > mouse[1] > y:  #asking if i am within the boundaries of the image
        drag = 1                                                                        #and if the left button is pressed

    if click[0] == 0:   #asking if the left button is pressed
        drag = 0

    if drag == 1:   #moving the image
        x = mouse[0] - (imgWidth / 2)   #imgWidth / 2 because i want my mouse centered on the image
        y = mouse[1] - (imgHeight / 2)


def main_loop():
    while True:        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(white)

        movableImg()

        pygame.display.update()
        clock.tick(60)

main_loop()
pygame.quit()
quit()

Tags: andtheimageifdisplayamwidthpygame
2条回答

你不需要这个功能。你所需要的只是在你的for循环中的一些事件。您可以简单地找出鼠标单击的时间,而不是一个凌乱、复杂的函数:

for event in pygame.event.get():
    if event.type == MOUSEBUTTONDOWN:    #Remember to use: from pygame.locals import *
        coordinates = pygame.mouse.get_pos()  
        #set x and y to respective values of coordinates
        #Enter necessary code here after

现在coordinates是我在这里创建的一个预定义变量,在本例中,它用于存储鼠标的坐标。当event.typeMOUSEBUTTONDOWN时,这意味着鼠标已被单击,程序应开始在该if语句下执行代码。事件只激活一次,这意味着你不能拖动。我建议您创建一个函数,当用户按下鼠标时,该函数允许事件继续,如下所示:

def holding():
    global held, coordinates
    if held:
        coordinates = pygame.mouse.get_pos()
        #Enter code here

held将是一个布尔变量:如果单击鼠标,则等于True;否则等于False。在这种情况下,为了防止程序认为您正在无限地单击鼠标,请添加另一个if语句来检查鼠标是否已释放,如果已释放,请将held更改为False

if event.type == MOUSEBUTTONUP:
    held = False

另外,要确保函数将实际注册鼠标仍被按住的事实,请将这一行放在if语句中的MOUSEBUTTONDOWN事件:

held = True

最后,要运行该函数,请在循环前面添加一个if语句,以便在需要时运行该函数:

if held:
    holding 

要移动多个图像,请将它们的位置更改为等于coordinates或与coordinates相关。if语句不必一次只移动一个图像。

这是我在网上找到的代码,我想怎么用就怎么用。SOURCE

import os,sys
import pygame as pg #lazy but responsible (avoid namespace flooding)

class Character:
    def __init__(self,rect):
        self.rect = pg.Rect(rect)
        self.click = False
        self.image = pg.Surface(self.rect.size).convert()
        self.image.fill((255,0,0))
    def update(self,surface):
        if self.click:
            self.rect.center = pg.mouse.get_pos()
        surface.blit(self.image,self.rect)

def main(Surface,Player):
    game_event_loop(Player)
    Surface.fill(0)
    Player.update(Surface)


def game_event_loop(Player):
    for event in pg.event.get():
        if event.type == pg.MOUSEBUTTONDOWN:
            if Player.rect.collidepoint(event.pos):
                Player.click = True
        elif event.type == pg.MOUSEBUTTONUP:
            Player.click = False
        elif event.type == pg.QUIT:
            pg.quit(); sys.exit()

if __name__ == "__main__":
    os.environ['SDL_VIDEO_CENTERED'] = '1'
    pg.init()
    Screen = pg.display.set_mode((1000,600))
    MyClock = pg.time.Clock()
    MyPlayer = Character((0,0,150,150))
    MyPlayer.rect.center = Screen.get_rect().center
    while 1:
        main(Screen,MyPlayer)
        pg.display.update()
        MyClock.tick(60)

相关问题 更多 >