Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.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 使光标无法在精灵游戏中移动_Python_Python 3.x_Pygame - Fatal编程技术网

Python 使光标无法在精灵游戏中移动

Python 使光标无法在精灵游戏中移动,python,python-3.x,pygame,Python,Python 3.x,Pygame,所以我的问题很简单:我如何制作一个我的鼠标无法通过的精灵?我一直在试验,我发现了一种不可靠的方法,这也是超级故障。如果有人知道我该怎么做,请帮忙 以下是我当前使用的代码: import pygame import pyautogui import sys import time pygame.init() game_display = pygame.display.set_mode((800,600)) pygame.mouse.set_visible(True) pygame.event.s

所以我的问题很简单:我如何制作一个我的鼠标无法通过的精灵?我一直在试验,我发现了一种不可靠的方法,这也是超级故障。如果有人知道我该怎么做,请帮忙

以下是我当前使用的代码:

import pygame
import pyautogui
import sys
import time

pygame.init()
game_display = pygame.display.set_mode((800,600))
pygame.mouse.set_visible(True)
pygame.event.set_grab(True)
exit = False

class Wall(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((30, 100))
        self.image.fill((255, 255, 255))
        self.rect = self.image.get_rect()
        self.rect.center = (200, 200)

    def collision(self):
        loc = pygame.mouse.get_pos()
        yy = loc[1]
        xx = loc[0]
        if yy >= self.rect.top and yy <= self.rect.bottom and xx >= self.rect.left and xx <= self.rect.right:
            if xx >= 200: 
                pyautogui.move(216 - xx, 0)
            if xx <= 200: 
                pyautogui.move(-xx + 184, 0)            

w = Wall()
all_sprites = pygame.sprite.Group()
all_sprites.add(w)
print(w.rect.top)
print(w.rect.bottom)
while (not exit):
    mouse_move = (0,0)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                exit = True 
    w.collision()
    clock = pygame.time.Clock()
    game_display.fill((0, 0, 0))
    clock.tick(30)
    all_sprites.update()
    all_sprites.draw(game_display)
    pygame.display.flip()
pygame.quit()
导入pygame
导入pyautogui
导入系统
导入时间
pygame.init()
game_display=pygame.display.set_模式((800600))
pygame.mouse.set_可见(True)
pygame.event.set_grab(True)
退出=错误
类墙(pygame.sprite.sprite):
定义初始化(自):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=pygame.Surface((30100))
self.image.fill((255,255,255))
self.rect=self.image.get_rect()
self.rect.center=(200200)
def碰撞(自):
loc=pygame.mouse.get_pos()
yy=loc[1]
xx=loc[0]
如果yy>=self.rect.top,yy=self.rect.left,xx=200:
pyautogui.move(216-xx,0)

如果xx要执行所需操作,必须检查从上一个鼠标位置到新鼠标位置的直线是否与矩形相交。编写一个函数
IntersectLineRec
,该函数检查并使用交叉点,并返回按距离排序的交叉点列表。 该函数返回具有点和距离的Tule列表:

e、 g

[((215.0177.0),12.0),((185.0177.0),42.0)]
prev_loc=pygame.mouse.get_pos()
类墙(pygame.sprite.sprite):
# [...]
def碰撞(自):
全球优先权
loc=pygame.mouse.get_pos()
intersect=INTERSECTLINERC(上一个loc、loc、self.REC)
上一个位置=位置
如果相交:
ip=[*相交[0][0]]
对于范围(2)中的i:
如果ip[i]==loc[i]else loc[i],则tp=自校正中心[i]
如果ip[i]
函数
IntersectLineRec
必须检查矩形四个角之间的四条外线之一是否与鼠标位置之间的线相交:

def IntersectLineRec(p1、p2、rect):
iL=[
相交线(p1、p2、直下左、直下右),
相交线(p1、p2、右下角矩形、右上角矩形),
相交线(p1、p2、正上右、正上左),
相交直线(p1、p2、矩形左上角、矩形左下角)]
如果i[0]],则iL中i的iDist=[(i[1],pygame.math.Vector2(i[1][0]-p1[0],i[1][1]-p1[1]).length()
iDist.sort(key=lambda t:t[1])
返回iDist
IntersectLineRec
检查由to点定义的to无休止线是否相交。然后检查交点是否位于由每条直线定义的矩形中(直线是矩形的对角线):

def IntersectLineLine(l1_p1、l1_p2、l2_p1、l2_p2):
isect,xPt=交叉端直线(l1_p1,l1_p2,l2_p1,l2_p2)
isect=isect和pInrect(xPt,l1_p1,l1_p2)和pInrect(xPt,l2_p1,l2_p2)
返回isect,xPt
要检查点是否在轴对齐的矩形中,必须检查点的两个坐标是否都在矩形的坐标范围内:

范围内的定义(坐标、范围、范围): 如果范围小于范围: 返回坐标>=范围和坐标=范围和坐标0 P=pygame.math.Vector2(*l1\u p1) Q=pygame.math.Vector2(*l2_p1) line1=pygame.math.Vector2(*l1_p2)-P line2=pygame.math.Vector2(*l2_p2)-Q 如果line1.length()==0或line2.length()==0: 返回(假,(0,0)) #检查这些线是否不平行 R、 S=(line1.normalize(),line2.normalize()) dot_R_nvS=R.dot(pygame.math.Vector2(S[1],-S[0])) 如果abs(dot_R_nvS)<0.001: 返回(假,(0,0)) #计算直线的交点 #t=dot(Q-P,(S.y,-S.x))/dot(R,(S.y,-S.x)) #X=P+R*t ptVec=Q-P t=ptVec.dot(pygame.math.Vector2(S[1],-S[0]))/dot\R\u nvS xPt=P+R*t 返回(True,(xPt[0],xPt[1]))
请参见动画:


它在什么方面“油嘴滑舌”?鼠标的问题是,如果移动得太快,它会从一个位置“传送”到另一个位置,并且永远不会存在于两个位置之间。确切地说。这就是问题所在,它不能正常工作。天啊,我没想到会有如此深入的事情发生。谢谢我会试试看它是如何工作的,我把所有这些函数放在哪里?嗯,我试过了,它看起来和以前一样工作。你能告诉我精灵的密码吗?这可能会起作用,但我会再试一次,虽然比我想象的更棘手,但现在应该可以了。
def IntersectEndlessLineLine(l1_p1, l1_p2, l2_p1, l2_p2):

    # calculate the line vectors and test if both lengths are > 0
    P = pygame.math.Vector2(*l1_p1)
    Q = pygame.math.Vector2(*l2_p1)
    line1 = pygame.math.Vector2(*l1_p2) - P
    line2 = pygame.math.Vector2(*l2_p2) - Q
    if line1.length() == 0 or line2.length() == 0:
        return (False, (0, 0))

    # check if the lines are not parallel
    R, S = (line1.normalize(), line2.normalize())
    dot_R_nvS = R.dot(pygame.math.Vector2(S[1], -S[0]))
    if abs(dot_R_nvS) < 0.001:
        return (False, (0, 0))

    # calculate the intersection point of the lines
    # t  =  dot(Q-P, (S.y, -S.x)) / dot(R, (S.y, -S.x))
    # X  =  P + R * t
    ptVec = Q-P
    t = ptVec.dot(pygame.math.Vector2(S[1], -S[0])) / dot_R_nvS
    xPt = P + R * t
    return (True, (xPt[0], xPt[1]))