如何解决如何使一条线水平和垂直跟随鼠标
我想在 x 轴和 y 轴上沿着鼠标光标绘制一条线,但我不太确定该怎么做。我只想让 x 线穿过整个窗口而不转动,但只上下跟随鼠标和 y 线做同样的事情,但只向左和向右跟随鼠标。 pygame 窗口为 400 x 400,具有我正在努力处理的代码的函数名称称为“drawLine(pos)”。
感谢您的帮助!
代码:
import pygame
WIDTH,HEIGHT = 400,400
WINDOW = pygame.display.set_mode((WIDTH,HEIGHT))
# Defining Color variables
BLACK = (0,0)
WHITE = (255,255,255)
RED = (255,0)
BLUE = (0,0)
# Drawing the Circle
def drawCircle(pos):
pygame.draw.circle(WINDOW,WHITE,pos,10)
# Drawing Vertical Line
def drawLine(pos):
x = pygame.mouse.get_pos()[0]
y = pygame.mouse.get_pos()[1]
line = pygame.draw.line(WINDOW,RED,(WIDTH,HEIGHT/2),(0,2)
lineRect = line.get_rect(center=(x,y))
WINDOW.blit(line,lineRect)
# Adding Text
def location_message(pos_type,locX,locY):
font = pygame.font.Font('freesansbold.ttf',32)
textSurf = font.render(pos_type,True,WHITE)
textRect = textSurf.get_rect()
textRect.center = (locX,locY)
WINDOW.blit(textSurf,textRect)
# Main function - runs everything
def main():
pygame.init()
run = True
pygame.display.update()
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEMOTION:
# Defining mouse position Variables
mouse_position = pygame.mouse.get_pos()
x = pygame.mouse.get_pos()[0]
y = pygame.mouse.get_pos()[1]
# Polishing x & y text numbers
if x < 100:
locationX = 60
else:
locationX = 70
if y < 100:
locationY = 60
else:
locationY = 70
# Printing mouse x & y cordinates
print("Mouse Location: " + str(mouse_position))
# Drawing Circle on mouse position
WINDOW.fill(BLACK)
drawCircle(mouse_position)
drawLine(mouse_position)
# Adding Text with Mouse X & Y Cordinates
location_message("X: ",25,15)
location_message("Y: ",50)
location_message(str(x),(locationX),15)
location_message(str(y),(locationY),50)
# Update pygame display
pygame.display.update()
pygame.quit()
# Executes everything
if __name__ == "__main__":
main()
解决方法
我通过将高度设置为 y 来解决它
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。