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

时间:2016-01-04 23:13:03

标签: python image animation pygame mouse

我写了一个小小的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()

2 个答案:

答案 0 :(得分:1)

所以这是我在互联网上找到的代码,它正在按照我的意愿运行。 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)

答案 1 :(得分:0)

你不需要这个功能。您所需要的只是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:< / p>

if event.type == MOUSEBUTTONUP:
    held = False

另外,为了确保该函数实际上会记录鼠标仍被按下的事实,请将此行放在if事件的MOUSEBUTTONDOWN语句中:

held = True

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

if held:
    holding 

要移动多张图片,请将其位置更改为等于coordinates或以某种方式与coordinates相关联。 if语句不必一次只移动一个图像。