在pygame中沿x轴拖动对象 [英] Dragging object along x-axis in pygame
问题描述
我希望能够使用鼠标沿 x 轴(黑线)拖动蓝色对象,使其不会在 y 方向移动.当我尝试拖动它时,没有任何反应.问题出在哪儿?
导入pygame定义初始化():pygame.init()全局高度,宽度高度 = 600宽度 = 900screen = 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)返回画面定义对象():拖动 = 假object_1 = pygame.rect.Rect(宽/4,高/2 - 75, 50, 150)如果 event.type == pygame.MOUSEBUTTONDOWN:如果 event.button == 1:如果 object_1.collidepoint(event.pos):拖动 = 真mouse_x, mouse_y = event.posoffset_x = object_1.x - mouse_xelif event.type == pygame.MOUSEBUTTONUP:如果 event.button == 1:拖动 = 假elif event.type == pygame.MOUSEMOTION:如果拖动:mouse_x, mouse_y = event.posobject_1.x = mouse_x + offset_x返回对象_1如果 __name__ == "__main__":运行 = 真屏幕 = 初始化()在跑步的时候:对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:运行 = 错误object_1 = object()pygame.draw.rect(屏幕, (0, 0, 250), object_1)pygame.display.update()
您必须在主应用程序循环之前创建对象一次,并且必须处理应用程序循环中的事件.
此外,您必须在应用程序循环中重绘整个场景.主应用程序循环必须:
- 通过
导入pygame定义初始化():pygame.init()全局高度,宽度高度 = 600宽度 = 900screen = pygame.display.set_mode((width, height))返回画面def create_object():object_1 = pygame.rect.Rect(宽/4,高/2 - 75, 50, 150)返回对象_1拖动 = 假def drag_object(events, object_1):全局拖动,offset_x对于事件中的事件:如果 event.type == pygame.MOUSEBUTTONDOWN:如果 event.button == 1:如果 object_1.collidepoint(event.pos):拖动 = 真mouse_x, mouse_y = event.posoffset_x = object_1.x - mouse_xelif event.type == pygame.MOUSEBUTTONUP:如果 event.button == 1:拖动 = 假elif event.type == pygame.MOUSEMOTION:如果拖动:mouse_x, mouse_y = event.posobject_1.x = mouse_x + offset_x如果 __name__ == "__main__":运行 = 真屏幕 = 初始化()object_1 = create_object()在跑步的时候:事件 = pygame.event.get()对于事件中的事件:如果 event.type == pygame.QUIT:运行 = 错误drag_object(事件,object_1)screen.fill((255, 255, 255))pygame.draw.line(screen, (0, 0 ,0), (0, height/2), (width, height/2), 3)pygame.draw.rect(屏幕, (0, 0, 250), object_1)pygame.display.update()
<小时>
或者,您可以为对象创建一个类:
导入pygame定义初始化():pygame.init()全局高度,宽度高度 = 600宽度 = 900screen = pygame.display.set_mode((width, height))返回画面类我的对象:def __init__(self):self.rect = pygame.rect.Rect(宽/4, 高/2 - 75, 50, 150)self.dragging = 假self.offset_x = 0def拖动(自我,事件):对于事件中的事件:如果 event.type == pygame.MOUSEBUTTONDOWN:如果 event.button == 1:如果 self.rect.collidepoint(event.pos):self.dragging = Trueself.offset_x = self.rect.x - event.pos[0]elif event.type == pygame.MOUSEBUTTONUP:如果 event.button == 1:self.dragging = 假elif event.type == pygame.MOUSEMOTION:如果自我拖动:self.rect.x = event.pos[0] + self.offset_x定义绘制(自我,冲浪):pygame.draw.rect(surf, (0, 0, 250), object_1)如果 __name__ == "__main__":运行 = 真屏幕 = 初始化()object_1 = MyObject()在跑步的时候:事件 = pygame.event.get()对于事件中的事件:如果 event.type == pygame.QUIT:运行 = 错误object_1.drag(事件)screen.fill((255, 255, 255))pygame.draw.line(screen, (0, 0 ,0), (0, height/2), (width, height/2), 3)object_1.draw(屏幕)pygame.display.update()
I want to be able to drag the blue object along the x-axis (black line) using mouse so that it does not move in y-direction. When I try to drag it, nothing happens. Where is the problem?
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()
解决方案You have to create the object once before the main application loop and you have to handle the events in the application loop.
Furthermore you have to redraw the entire scene in the application loop. The main application loop has to:- handle the events by either
pygame.event.pump()
orpygame.event.get()
. - update the game states and positions of objects dependent on the input events and time (respectively frames)
- clear the entire display or draw the background
- draw the entire scene (
blit
all the objects) - update the display by either
pygame.display.update()
orpygame.display.flip()
Add a function which creates an object:
def create_object(): object_1 = pygame.rect.Rect(width / 4, height / 2 - 75, 50, 150) return object_1
Create an object before the application loop:
if __name__ == "__main__": # [...] object_1 = create_object() while running: # [...]
Add a function which can drag an object:
dragging = False def drag_object(events, object_1): global dragging, offset_x for event in events: 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
Get the list of events once in the application loop and pass the events to the function
drag_object
:while running: # [...] drag_object(events, object_1)
Clear the display, draw the scene and update the display in the application loop:
while running: # [...] screen.fill((255, 255, 255)) pygame.draw.line(screen, (0, 0 ,0), (0, height / 2), (width, height / 2), 3) pygame.draw.rect(screen, (0, 0, 250), object_1) pygame.display.update()
See the example:
import pygame def initialize(): pygame.init() global height, width height = 600 width = 900 screen = pygame.display.set_mode((width, height)) return screen def create_object(): object_1 = pygame.rect.Rect(width / 4, height / 2 - 75, 50, 150) return object_1 dragging = False def drag_object(events, object_1): global dragging, offset_x for event in events: 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 if __name__ == "__main__": running = True screen = initialize() object_1 = create_object() while running: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: running = False drag_object(events, object_1) screen.fill((255, 255, 255)) pygame.draw.line(screen, (0, 0 ,0), (0, height / 2), (width, height / 2), 3) pygame.draw.rect(screen, (0, 0, 250), object_1) pygame.display.update()
Alternatively you can create a class for the object:
import pygame def initialize(): pygame.init() global height, width height = 600 width = 900 screen = pygame.display.set_mode((width, height)) return screen class MyObject: def __init__(self): self.rect = pygame.rect.Rect(width / 4, height / 2 - 75, 50, 150) self.dragging = False self.offset_x = 0 def drag(self, events): for event in events: if event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: if self.rect.collidepoint(event.pos): self.dragging = True self.offset_x = self.rect.x - event.pos[0] elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: self.dragging = False elif event.type == pygame.MOUSEMOTION: if self.dragging: self.rect.x = event.pos[0] + self.offset_x def draw(self, surf): pygame.draw.rect(surf, (0, 0, 250), object_1) if __name__ == "__main__": running = True screen = initialize() object_1 = MyObject() while running: events = pygame.event.get() for event in events: if event.type == pygame.QUIT: running = False object_1.drag(events) screen.fill((255, 255, 255)) pygame.draw.line(screen, (0, 0 ,0), (0, height / 2), (width, height / 2), 3) object_1.draw(screen) pygame.display.update()
这篇关于在pygame中沿x轴拖动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- handle the events by either