pygame旋转直线,计算角色到墙距离

在这里插入图片描述

pygame怎么计算距离
在Pygame中,计算两点之间的距离可以使用几何公式。给定两点P1(x1, y1)和P2(x2, y2),距离D可以用以下公式计算:

D = √((x2 - x1)² + (y2 - y1)²)

在Python中,你可以使用math库中的sqrt函数来计算平方根。以下是一个计算两点之间距离的Pygame示例代码:

import math
import pygame

定义两个点的坐标

point1 = pygame.math.Vector2(100, 100)
point2 = pygame.math.Vector2(200, 200)

计算两点之间的距离

distance = point1.distance_to(point2)

print(f"The distance between the two points is: {distance}")
在这个例子中,我们使用了pygame的math.Vector2类,它内置了一个distance_to方法来计算两个点之间的距离。这样可以简化计算过程,并提供更为直观和易于理解的代码。

计算旋转后的终点位置

end_point_x = start_point[0] + ray_length * math.cos(math.radians(rotate_angle))
end_point_y = start_point[1] - ray_length * math.sin(math.radians(rotate_angle))
end_point = (end_point_x, end_point_y)

# 绘制射线
pygame.draw.line(window_surface, (255, 0, 0), start_point, end_point, 2)

# 刷新屏幕
pygame.display.update()

在上面的示例中,我们在事件循环中检测键盘左键是否按下,并根据按键状态更新线条的旋转角度。您可以通过调整rotate_speed参数来改变旋转速度,从而控制角度的变化速度。这样就可以在Pygame中实现按下键盘左键时旋转线条角度的效果。

2024/4/7 19:02:10

end_point_x = start_point[0] + ray_length * math.cos(math.radians(rotate_angle))
end_point_y = start_point[1] - ray_length * math.sin(math.radians(rotate_angle))

2024/4/7 19:02:16

这两行代码是用来计算旋转后的射线终点坐标的。让我解释一下每个部分的作用:

math.cos(math.radians(rotate_angle)): 这部分计算了给定角度rotate_angle的余弦值,其中math.radians()函数将角度从度转换为弧度。余弦值表示终点在x轴上的偏移量。

math.sin(math.radians(rotate_angle)): 这部分计算了给定角度rotate_angle的正弦值,其中math.radians()函数将角度从度转换为弧度。正弦值表示终点在y轴上的偏移量。

start_point[0] + ray_length * …: 这部分将起点的x坐标与旋转后x轴上的偏移量相加,从而得到终点的x坐标。

start_point[1] - ray_length * …: 这部分将起点的y坐标减去旋转后y轴上的偏移量,从而得到终点的y坐标。

通过这些计算,您可以根据起点、长度和角度来确定旋转后的射线终点坐标。这样就能实现在Pygame中根据旋转角度绘制旋转的射线的效果。

import pygame
from pygame.locals import *
import sys
import math

pygame.init()

width, height = 800, 600
screen = pygame.display.set_mode((width, height))
c=pygame.time.Clock()
q = [
    [1, 1, 1, 1, 1, 1, 1, 1],
    [1, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 1],
    [1, 1, 1, 1, 1, 1, 1, 1]
     ]

px=70
py=70
du=0
left=False
right=False
down=False
up=False
img = pygame.image.load("1.png")  # 替换为您自己的图像文件路径
img=pygame.transform.scale(img,(30,30))
ci=0
sz=[]
qsz=[]
xiansz=[(0,0)]
xuanzhuanjiao=0
class zidan:
    global xiansz
    def __init__(self):
        self.x = self
        self.y = self
        self.endx=self
        self.endy=self
        self.xiansz=self


    def chuangjian(self):
        self.endx = px
        self.endy = py

        p=pygame.draw.rect(screen, (40, 140, 40), (self.x, self.y, 10,20), 0)
        print(self.x,self.y,"d没碰")

        if(p.collidelistall(qsz)):
            xiansz.clear()
            #self.endx, self.endy = self.x, self.y
            xiansz.append((self.x,self.y))

            print(self.x,self.y,"duixiang",xiansz)
def player(x,y,zhuan):

    #pygame.draw.line(screen, (40, 140, 40), (x, y), (74, 713))
    #print(du)
    screen.blit(zhuan,(x,y))

def dl(x,y):
    pygame.draw.line(screen, (40, 140, 40), (x, y), (x, 700))




while True:
    screen.fill((255, 255, 255))
    #c.tick(7)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP]:
            print("up")
            py=py -10
        if keys[pygame.K_LEFT]:#zuo
            px=px-10
        if keys[pygame.K_DOWN]:#xia
            print("up")
            py = py + 10
        if keys[pygame.K_RIGHT]:#you
            px=px+10
            #print(sz)
            #在这直接写会变成子弹,写成函数qiang(ci)就能画成一次,不会移动的墙

    zi = zidan()
    zi.x = px
    zi.y = py
    sz.append(zi)
    for h in range(len(q)):
        #print(q[h],"行",h)
        for g in range(len(q[h])):
            if q[h][g]==1:
                #print(q[h][g],"个",g,h)
                pz=pygame.draw.rect(screen, (140, 240, 40), (h*60, g*60, 60, 60))
                qsz.append(pz)

    player(px,py,img)
    dl(px,py)



    for i in (sz):
        #print(i)
        i.x=i.x+60
        i.chuangjian()

        #pygame.draw.line(screen, (40, 140, 40), (px, py), (i.endx, i.endy))
        #print(i.endx, i.endy, "点")
        if(i.x>500):
            sz.remove(i)

        for i in (sz):
            xuanzhuanjiao = xuanzhuanjiao + 1
            pygame.draw.line(screen, (40, 140, 40), (px, py), (xiansz[0]))

            p=pygame.math.Vector2(px,py)
            p1=pygame.math.Vector2(xiansz[0])
            juli=p.distance_to(p1)



            end_point_x = px +int(juli) * math.cos(math.radians(1))
            end_point_y = py - int(juli) * math.sin(math.radians(1))
            end_point = (end_point_x, end_point_y)
            pygame.draw.line(screen, (255, 0, 0), (px, py), end_point, 2)
            end_point_x = px + int(juli) * math.cos(math.radians(2))
            end_point_y = py - int(juli) * math.sin(math.radians(2))
            end_point = (end_point_x, end_point_y)
            pygame.draw.line(screen, (255, 0, 0), (px, py), end_point, 2)
            end_point_x = px + int(juli) * math.cos(math.radians(3))
            end_point_y = py - int(juli) * math.sin(math.radians(3))
            end_point = (end_point_x, end_point_y)
            pygame.draw.line(screen, (255, 0, 0), (px, py), end_point, 2)
            end_point_x = px + int(juli) * math.cos(math.radians(4))
            end_point_y = py - int(juli) * math.sin(math.radians(4))
            end_point = (end_point_x, end_point_y)
            pygame.draw.line(screen, (255, 0, 0), (px, py), end_point, 2)
            print(i.endx, i.endy, "点", (end_point_x, end_point_y), xiansz,xuanzhuanjiao)
            #if (i.x > 500):
                #sz.remove(i)

        #在这直接写会变成子弹,写成函数qiang(ci)就能画成一次,不会移动的墙



    pygame.display.flip()
  • 19
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值