python 是否可以在移动的版pygame中使用FINGERDOWN独立拖动两个精灵?

7eumitmz  于 2023-01-01  发布在  Python
关注(0)|答案(1)|浏览(133)

我正在为android开发一款乒乓风格的游戏,通过拖拽触摸屏来控制球拍。这个想法是两个玩家可以在同一台设备上玩,每个玩家都可以通过触摸屏来控制球拍。是否可以使用pygame 2.0中的触摸事件同时拖拽两个精灵到不同的位置?
我已经可以使用FINGERDOWN/FINGERMOTION/FINGERUP事件一次移动一个拨片,但不知道如何使用多点触控来同时独立控制两个拨片。如果我拖动两个拨片,它们似乎都遵循一个FINGERDOWN事件的坐标。
这是我的代码,我希望格式是好的,这是我第一次在网站上张贴. GIF示例显示问题

import sys
import pygame
from pygame.locals import *

class Player(pygame.sprite.Sprite):
    
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((200,200))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect(center = (x, y))
        self.drag = False
        
    def update(self, events):
        for e in events:
            if e.type == FINGERDOWN:
                for finger, pos in fingers.items():
                    if self.rect.collidepoint(pos):
                        self.drag = True
                        self.mx, self.my = pos
                        self.offx = self.rect.x - self.mx
                        self.offy = self.rect.y - self.my
                    
            if e.type == FINGERMOTION:
                if self.drag:
                    for finger, pos in fingers.items():
                        self.mx, self.my = pos
                        self.rect.y = self.my + self.offy
                    
            if e.type == FINGERUP:
                self.drag = False
        

def handle_events():
    global events
    for event in events:
        if pygame.event == QUIT:
            pygame.quit()
            sys.exit()
        
        if event.type == VIDEORESIZE:
             DW, DH = event.w, event.h
        
        if event.type == FINGERDOWN:
             x = int(event.x * DS.get_width())
             y = int(event.y * DS.get_height())
             fingers[event.finger_id] = x, y
             
        if event.type == FINGERMOTION:
            x = int(event.x * DS.get_width())
            y = int(event.y * DS.get_height())
            fingers[event.finger_id] = x, y
            
        if event.type == FINGERUP:
             fingers.pop(event.finger_id, None)
pygame.init()

#display settings
DW = 0
DH = 0
DS = pygame.display.set_mode((DW, DH), pygame.FULLSCREEN | pygame.RESIZABLE)
DW, DH = DS.get_size()

FPS = 60

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

#player intialisation
p = Player(0 + 100, DH/2)
p2 = Player(DW - 100, DH/2)
sprites = pygame.sprite.Group()
sprites.add(p, p2)

fingers = {}
clock = pygame.time.Clock()

while True:
    events = pygame.event.get()
    handle_events()
    
    DS.fill(BLACK)
    sprites.draw(DS)
    sprites.update(events)
    pygame.display.update()
    
    clock.tick(FPS)
0h4hbjxa

0h4hbjxa1#

事件FINGERDOWNFINGERUPFINGERMOTION为您提供finger_id(另请参阅如何在移动的游戏中使用Pygame触摸事件?)。
必须将触摸对象的手指ID存储在属性中。仅当移动具有相应ID的手指时才拖动对象:

class Player(pygame.sprite.Sprite):
    
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((200,200))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect(center = (x, y))
        self.drag = False
        self.finger_id = None
        
    def update(self, events):
        for e in events:
            if e.type == FINGERDOWN:
                for finger, pos in fingers.items():
                    if self.rect.collidepoint(pos):
                        self.finger_id = finger
                        self.drag = True
                        self.mx, self.my = pos
                        self.offx = self.rect.x - self.mx
                        self.offy = self.rect.y - self.my
                    
            if e.type == FINGERMOTION:
                if self.drag:
                    for finger, pos in fingers.items():
                        if finger == self.finger_id:
                            self.mx, self.my = pos
                            self.rect.y = self.my + self.offy
                    
            if e.type == FINGERUP:
                if not self.finger_id in fingers:
                    self.drag = False

实际上,您根本不需要全局fingersMap

import sys
import pygame
from pygame.locals import *

class Player(pygame.sprite.Sprite):
    
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((200, 200))
        self.image.fill("red")
        self.rect = self.image.get_rect(center = (x, y))
        self.drag = False
        self.finger_id = None
        
    def update(self, events):
        for e in events:
            if e.type == FINGERDOWN:
                x = int(e.x * DS.get_width())
                y = int(e.y * DS.get_height())
                if self.rect.collidepoint((x, y)):
                    self.finger_id = e.finger_id
                    self.drag = True
                    self.mx, self.my = x, y
                    self.offx = self.rect.x - self.mx
                    self.offy = self.rect.y - self.my
                    
            if e.type == FINGERMOTION:
                if self.drag and self.finger_id == e.finger_id:
                    x = int(e.x * DS.get_width())
                    y = int(e.y * DS.get_height())
                    self.mx, self.my = x, y
                    self.rect.y = self.my + self.offy
                    
            if e.type == FINGERUP:
                if self.finger_id == e.finger_id:
                    self.drag = False
        

def handle_events(events):
    for event in events:
        if event.type == VIDEORESIZE:
             DW, DH = event.w, event.h

pygame.init()

#display settings
DW = 0
DH = 0
#DS = pygame.display.set_mode((DW, DH), pygame.FULLSCREEN | pygame.RESIZABLE)
DS = pygame.display.set_mode((640, 480))
DW, DH = DS.get_size()

FPS = 60

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

#player intialisation
p = Player(0 + 100, DH/2)
p2 = Player(DW - 100, DH/2)
sprites = pygame.sprite.Group()
sprites.add(p, p2)

clock = pygame.time.Clock()

run = True
while run:
    events = pygame.event.get()
    run = not any(event for event in events if event.type == QUIT)
    handle_events(events)
    
    DS.fill(BLACK)
    sprites.draw(DS)
    sprites.update(events)
    pygame.display.update()
    
    clock.tick(FPS)

pygame.quit()
sys.exit()

相关问题