Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在pygame中沿x轴拖动对象_Python_Python 3.x_Pygame - Fatal编程技术网

Python 在pygame中沿x轴拖动对象

Python 在pygame中沿x轴拖动对象,python,python-3.x,pygame,Python,Python 3.x,Pygame,我希望能够使用鼠标沿x轴(黑线)拖动蓝色对象,使其不会在y方向移动。当我试图拖动它时,什么也没发生。问题在哪里 import pygame def initialize(): pygame.init() global height, width height = 600 width = 900 screen = pygame.display.set_mode((width, height)) screen.fill((255, 255, 255)

我希望能够使用鼠标沿x轴(黑线)拖动蓝色对象,使其不会在y方向移动。当我试图拖动它时,什么也没发生。问题在哪里

import pygame

def initialize():
    pygame.init()
    global height, width
    height = 600
    width = 900
    screen = pygame.display.set_mode((width, height))
    screen.fill((255, 255, 255))
    pygame.draw.line(screen, (0, 0 ,0), (0, height / 2), (width, height / 2), 3)
    return screen

def object():
    dragging = False
    object_1 = pygame.rect.Rect(width / 4, height / 2 - 75, 50, 150)

    if event.type == pygame.MOUSEBUTTONDOWN:
        if event.button == 1:
            if object_1.collidepoint(event.pos):
                dragging = True
                mouse_x, mouse_y = event.pos
                offset_x = object_1.x - mouse_x

    elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:
                dragging = False

    elif event.type == pygame.MOUSEMOTION:
        if dragging:
            mouse_x, mouse_y = event.pos
            object_1.x = mouse_x + offset_x

    return object_1

if __name__ == "__main__":
    running = True
    screen = initialize()

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

            object_1 = object()
        pygame.draw.rect(screen, (0, 0, 250), object_1)
        pygame.display.update()

您必须在主应用程序循环之前创建一次对象,并且必须处理应用程序循环中的事件。
此外,还必须在应用程序循环中重新绘制整个场景。主应用程序循环必须:

  • 通过或处理事件
  • 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
  • 清除整个显示或绘制背景
  • 绘制整个场景(
    blit
    所有对象)
  • 通过或更新显示
添加创建对象的函数:

def create_object():
object_1=pygame.rect.rect(宽/4,高/2-75,50,150)
返回对象_1
在应用程序循环之前创建对象:

如果名称=“\uuuuu main\uuuuuuuu”:
# [...]
object_1=创建_object()
运行时:
# [...]
添加一个可以拖动对象的函数:

拖动=False
定义拖动对象(事件,对象1):
全局拖动,偏移量x
对于事件中的事件:
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==1:
如果对象1.碰撞点(事件位置):
拖动=真
鼠标x,鼠标y=event.pos
偏移量x=对象x 1.x-鼠标x
elif event.type==pygame.MOUSEBUTTONUP:
如果event.button==1:
拖动=错误
elif event.type==pygame.MOUSEMOTION:
如果拖动:
鼠标x,鼠标y=event.pos
对象_1.x=鼠标_x+偏移量_x
在应用程序循环中获取一次事件列表,并将事件传递给函数
drag\u object

运行时:
# [...]
拖动对象(事件,对象1)
清除显示、绘制场景并更新应用程序循环中的显示:

运行时:
# [...]
屏幕填充((255、255、255))
pygame.draw.line(屏幕,(0,0,0),(0,高度/2),(宽度,高度/2),3)
pygame.draw.rect(屏幕,(0,0250),对象_1)
pygame.display.update()
请参见示例:

导入pygame
def初始化():
pygame.init()
全局高度、宽度
高度=600
宽度=900
screen=pygame.display.set_模式((宽度、高度))
返回屏幕
def create_object():
object_1=pygame.rect.rect(宽/4,高/2-75,50,150)
返回对象_1
拖动=错误
定义拖动对象(事件,对象1):
全局拖动,偏移量x
对于事件中的事件:
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==1:
如果对象1.碰撞点(事件位置):
拖动=真
鼠标x,鼠标y=event.pos
偏移量x=对象x 1.x-鼠标x
elif event.type==pygame.MOUSEBUTTONUP:
如果event.button==1:
拖动=错误
elif event.type==pygame.MOUSEMOTION:
如果拖动:
鼠标x,鼠标y=event.pos
对象_1.x=鼠标_x+偏移量_x
如果名称=“\uuuuu main\uuuuuuuu”:
运行=真
screen=initialize()
object_1=创建_object()
运行时:
events=pygame.event.get()
对于事件中的事件:
如果event.type==pygame.QUIT:
运行=错误
拖动对象(事件,对象1)
屏幕填充((255、255、255))
pygame.draw.line(屏幕,(0,0,0),(0,高度/2),(宽度,高度/2),3)
pygame.draw.rect(屏幕,(0,0250),对象_1)
pygame.display.update()

或者,您可以为对象创建一个类:

导入pygame
def初始化():
pygame.init()
全局高度、宽度
高度=600
宽度=900
screen=pygame.display.set_模式((宽度、高度))
返回屏幕
类MyObject:
定义初始化(自):
self.rect=pygame.rect.rect(宽/4,高/2-75,50,150)
self.draging=False
self.offset_x=0
def拖动(自身、事件):
对于事件中的事件:
如果event.type==pygame.MOUSEBUTTONDOWN:
如果event.button==1:
如果self.rect.collidepoint(事件位置):
self.draging=True
self.offset_x=self.rect.x-event.pos[0]
elif event.type==pygame.MOUSEBUTTONUP:
如果event.button==1:
self.draging=False
elif event.type==pygame.MOUSEMOTION:
如果自动拖动:
self.rect.x=event.pos[0]+self.offset\u x
def绘图(自我、冲浪):
pygame.draw.rect(surf,(0,0250),object_1)
如果名称=“\uuuuu main\uuuuuuuu”:
运行=真
screen=initialize()
object_1=MyObject()
运行时:
events=pygame.event.get()
对于事件中的事件:
如果event.type==pygame.QUIT:
运行=错误
对象_1.拖动(事件)
屏幕填充((255、255、255))
pygame.draw.line(屏幕,(0,0,0),(0,高度/2),(宽度,高度/2),3)
对象_1.绘制(屏幕)
pygame.display.update()