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))
def shexian(px,py,juli,xuanzhuanjiao):
xuan=0
for c in range(10):
c=c+c*1.7#每条射线间距离
print(c)
end_point_x = px + int(juli) * math.cos(math.radians(c))
end_point_y = py - int(juli) * math.sin(math.radians(c))
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(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):
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)
for c in range(10):
c = c + c * 1.7 # 每条射线间距离
print(c)
end_point_x = px + int(juli) * math.cos(math.radians(c))
end_point_y = py - int(juli) * math.sin(math.radians(c))
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, "点", xiansz,xuanzhuanjiao)
#if (i.x > 500):
#sz.remove(i)
#在这直接写会变成子弹,写成函数qiang(ci)就能画成一次,不会移动的墙
pygame raycast 射线
于 2024-04-08 16:13:39 首次发布