Pygame 拖动背景图像 [英] Pygame drag background image

查看:71
本文介绍了Pygame 拖动背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的背景图像在鼠标被点击时向相反的方向移动.所以看起来你在左右拖动屏幕,就像你在许多移动应用程序中看到的那样.有没有人有一个简单的方法来做到这一点?

I am trying to make my background image move in the opposite direction as the mouse is moving when it is clicked. So it looks like you are dragging the screen left and right, like you can see in many mobile apps. Does anyone have a simple way of doing this?

推荐答案

你需要的是检测事件类型,例如MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION.
当您检测到鼠标移动时,请使用 event.buttons 属性检查它是否被点击.这是我的工具:

What you need is to detect the event type, such as MOUSEBUTTONDOWN, MOUSEBUTTONUP, MOUSEMOTION.
When you detected a mouse motion, check if it's clicked using the event.buttons attribute. Here's my implement:

import pygame
from pygame.locals import *

pygame.display.init()
screen = pygame.display.set_mode((800, 600))
img = pygame.image.load('sky.png')

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

LeftButton = 0
while 1:
    for e in pygame.event.get():
        if e.type == QUIT: exit(0)
        if e.type == MOUSEMOTION:
            if e.buttons[LeftButton]:
                # clicked and moving
                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)

<小时>

您可能需要阅读:Pygame 事件文档

这篇关于Pygame 拖动背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆