pygame让射线距离更密集

发布于:2024-04-12 ⋅ 阅读:(193) ⋅ 点赞:(0)

在这里插入图片描述

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

pygame.init()

width, height = 900, 900
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
#j=0
class zidan:
    #global xiansz
    def __init__(self):
        self.x = self
        self.y = self
        self.endx=self
        self.endy=self
        self.xiansz=self
        self.xuanzhuanjiao=self

    def up(self):
        self.x = self.x + math.cos(math.radians(self.xuanzhuanjiao)) * 70
        self.y = self.y - math.sin(math.radians(self.xuanzhuanjiao)) * 70
    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))#使用对象外的数组,要是对象自己的变量,在碰撞后会覆盖碰撞位置的x,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))


def shexian():

    p = pygame.math.Vector2(px, py)  # 角色位置
    p1 = pygame.math.Vector2(xiansz[0])  # 碰撞点x,y
    juli = p.distance_to(p1)  # 计算距离,其实有距离后不用再画射线,后面画墙,画射线是为了理解
    # if j < 14:
    #     j = j + 0.1
    j=0
    for k in range(10):

        if j<9:
            j=j+0.1
        end_point_x = px + int(juli) * math.cos(math.radians(j))
        end_point_y = py - int(juli) * math.sin(math.radians(j))
        end_point = (end_point_x, end_point_y)
        pygame.draw.line(screen, (255, 0, 0), (px, py), end_point, 2)

while True:
    screen.fill((255, 255, 255))
    #c.tick(70)
    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
            #xuanzhuanjiao=xuanzhuanjiao+1
            px=px+10
            #print(sz)
            #在这直接写会变成子弹,写成函数qiang(ci)就能画成一次,不会移动的墙
        if keys[pygame.K_a]:
            xuanzhuanjiao=xuanzhuanjiao+10

            right=True
        if event.type == pygame.KEYUP:
            right = False
        if keys[pygame.K_l]:
            xuanzhuanjiao=xuanzhuanjiao-10
        #if keys[pygame.K_SPACE]:


    zi = zidan()  # 一直创建,超过屏幕会回收的
    zi.x = px
    zi.y = py
    zi.xuanzhuanjiao = xuanzhuanjiao
    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), (100+h*60, 100+g*60, 60, 60))
                qsz.append(pz)

    player(px,py,img)
    dl(px,py)
    #print(right)


    for e in (sz):#遍历子弹数组
        #print(i)


        e.up()




        e.chuangjian()
        if(e.x>700 or e.x<100 or e.y>700 or e.y<70):
            sz.remove(e)




        for i in (sz):#画射线
            j=0
            j1=0
            pygame.draw.line(screen, (40, 140, 40), (px, py), (xiansz[0]))
            #shexian()
            p=pygame.math.Vector2(px,py)#角色位置
            p1=pygame.math.Vector2(xiansz[0])#碰撞点x,y
            juli=p.distance_to(p1)#计算距离,其实有距离后不用再画射线,后面画墙,画射线是为了理解

            # end_point_x = px + int(juli) * math.cos(math.radians(0))
            # end_point_y = py - int(juli) * math.sin(math.radians(0))
            # end_point = (end_point_x, end_point_y)
            #
            # pygame.draw.line(screen, (255, 0, 0), (px, py), end_point, 2)
            for c in range(16):

                if j<16:
                    j = j +0.3  # 每条射线间距离
                #print(c)
                    end_point_x = px + int(juli) * math.cos(math.radians(j+xuanzhuanjiao))
                    end_point_y = py - int(juli) * math.sin(math.radians(j+xuanzhuanjiao))
                    end_point = (end_point_x, end_point_y)

                    pygame.draw.line(screen, (255, 0, 0), (px, py), end_point, 2)
                    print(end_point, "点", xiansz,j,juli)

            for c1 in range(16):

                if j1<16:
                    j1 = j1 +0.3  # 每条射线间距离
                #print(c)
                    end_point_x = px + int(juli) * math.cos(math.radians(-j1+xuanzhuanjiao))
                    end_point_y = py - int(juli) * math.sin(math.radians(-j1+xuanzhuanjiao))
                    end_point = (end_point_x, end_point_y)

                    pygame.draw.line(screen, (255, 0, 0), (px, py), end_point, 2)
                    print(end_point, "点", xiansz,j1,juli)
            #if (i.x > 500):
                #sz.remove(i)

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

    print(px,py,len(sz))

    pygame.display.flip()

网站公告

今日签到

点亮在社区的每一天
去签到