在Pygame中以光标方向绘制无限长的线 [英] Drawing line with infinity length in the direction of cursor in Pygame

查看:54
本文介绍了在Pygame中以光标方向绘制无限长的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关pygame的帮助.我正在用Python开发简单的游戏来学习Pygame.我想做一个可以旋转并可以用激光线射击的太空飞船.我已经完成了箭头键的控制,我们也可以用鼠标的位置旋转飞船,但是拍摄时遇到了问题.我想用从飞船位置到鼠标方向无限长的线.我该怎么做?这是我的代码:

I'm searching for some help with pygame. I'm developing simple game in Python to learn Pygame. I want to make spaceship which we can rotate and we can shooting with laser line. I have done controlling by arrow keys, we can also rotating spaceship with mouse position, but I've got a problem with shooting. I want to make a line with infinity length from spaceship position to mouse direction. How I can do that? Here is my code:

  def draw_objects(self):
        SCREEN.fill(BLACK)
        self.target = pygame.mouse.get_pos()
        self.x = self.player.position[0] #player x position
        self.y = self.player.position[1] #player y position
        self.mx = self.target[0] #mouse x position
        self.my = self.target[1] #mouse y position
        self.slope=float(float(self.y-self.my)/float(self.x-self.mx+0.1)) #slope
        self.x_new =  DISPLAY_WIDTH #ray length
        self.y_new = self.y + self.slope * (self.x_new - self.x) 
        self.player.draw()
        self.draw_columns()
        for agent in self.all_agents:
            agent.draw()
            agent.draw_vectors()
        if self.player.shoot == True:
            pygame.draw.line(SCREEN, GREEN, self.player.position,(self.x_new, self.y_new), 2)

        pygame.display.update()

它不能正常工作,因为它只能在飞船的右边工作.在其他情况下,它会向光标反射出一条线.

It's not working properly, because it's work only to the right of spaceship. In other cases it draws a line in reflection to cursor.

感谢您的帮助.

推荐答案

furas是正确的,您必须检查鼠标是在播放器的左侧还是右侧,并在 DISPLAY_WIDTH 取反的情况下取反.它在左侧.我来到了一个类似的解决方案:

furas is right, you have to check whether the mouse is on the left or right side of the player and negate the DISPLAY_WIDTH if it's on the left. I came to a similar solution:

def target_coords(position):
    x, y = position  # Unpack player position into x, y.
    mx, my = pygame.mouse.get_pos()  # Unpack mouse pos into mx, my.
    slope = (y-my) / (x-mx+0.1)
    # This is a conditional expression, similar to `if condition: ... `else: ... `.
    x_new = DISPLAY_WIDTH if x < mx else -DISPLAY_WIDTH
    y_new = y + slope * (x_new - x)
    return x_new, y_new

请注意,此函数仅计算目标坐标并返回它们(函数最好只做一件事).画线以及其他功能.

Note that this function only computes the target coordinates and returns them (functions should preferably do only one thing). Draw the line and everything else in another function.

还有另一种解决方案:您可以使用 pygame向量,然后首先将向量计算到目标,对其进行归一化并按所需的长度( DISPLAY_WIDTH )进行缩放.

There's also an alternative solution: You could use pygame vectors and first calculate the vector to the target, normalize it and scale it by the desired length (the DISPLAY_WIDTH).

import pygame
from pygame.math import Vector2


pygame.init()
DISPLAY_WIDTH = 640
GREEN = pygame.Color('aquamarine1')
screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()
position = Vector2(300, 200)  # A pygame.math.Vector2 as the position.
done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    screen.fill((30, 30, 30))
    pygame.draw.circle(screen, GREEN, (int(position.x), int(position.y)), 7)
    # Calculate the vector to the target by subtracting pos from mouse pos.
    # Normalizing it gives you a unit vector which you can scale
    # by multiplying it with the DISPLAY_WIDTH.
    target_vec = (pygame.mouse.get_pos()-position).normalize() * DISPLAY_WIDTH
    pygame.draw.line(screen, GREEN, position, position+target_vec, 2)

    pygame.display.flip()
    clock.tick(30)

pygame.quit()

这篇关于在Pygame中以光标方向绘制无限长的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆