Python 蛇的部分是;“流动”;而不是",;snap";给蛇';头位
我正在尝试用python制作一个蛇游戏,我希望当用户按下WASD键时蛇的片段流动,而不是捕捉到用户想要的方向Python 蛇的部分是;“流动”;而不是",;snap";给蛇';头位,python,pygame,Python,Pygame,我正在尝试用python制作一个蛇游戏,我希望当用户按下WASD键时蛇的片段流动,而不是捕捉到用户想要的方向 import pygame import random import time pygame.init() win = pygame.display.set_mode((800,600)) pygame.display.set_caption("Pygame") clock = pygame.time.Clock() x = 30 y = 30 x2 = x y2 = random.ra
import pygame
import random
import time
pygame.init()
win = pygame.display.set_mode((800,600))
pygame.display.set_caption("Pygame")
clock = pygame.time.Clock()
x = 30
y = 30
x2 = x
y2 = random.randrange(1,601-30)
vel = 2
run = True
facing = 0
direction = 0
text = pygame.font.SysFont('Times New Roman',30)
score = 0
segments = []
green = ((0,128,0))
white = ((255,255,255))
counting = 0
segmentTime = time.time()
class segmentClass():
def __init__(self,x,y,pos,color):
self.x = x
self.y = y
self.pos = pos
self.color = color
def draw(self,win):
pygame.draw.rect(win,(self.color),(self.x,self.y,30,30))
def gameOver():
global run
run = False
def segmentGrowth():
global x2
global y2
global score
global vel
global ammount
segments.append(segmentClass(x,y,len(segments)+1,green))
ammount = 0
x2 = random.randrange(1,801-30)
y2 = random.randrange(1,601-30)
score += 1
print(vel)
while run:
currentTime = time.time()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
vel += (score*0.0001)
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
if direction != 1:
direction = 1
facing = -1
if keys[pygame.K_s]:
if direction != 1:
direction = 1
facing = 1
if keys[pygame.K_a]:
if direction != 0:
direction = 0
facing = -1
if keys[pygame.K_d]:
if direction != 0:
direction = 0
facing = 1
if direction == 1:
y += (vel*facing)
else:
x += (vel*facing)
if x > x2 and x < x2 + 30 or x + 30 > x2 and x + 30 < x2 + 30:
if y == y2:
segmentGrowth()
if y > y2 and y < y2 + 30 or y + 30 > y2 and y + 30 < y2 + 30:
segmentGrowth()
if y > y2 and y < y2 + 30 or y + 30 > y2 and y + 30 < y2 + 30:
if x == x2:
segmentGrowth()
if x > x2 and x < x2 + 30 or x + 30 > x2 and x + 30 < x2 + 30:
segmentGrowth()
if x > 800-30 or y > 600-30 or x < 0 or y < 0:
gameOver()
win.fill((0,0,0))
for segment in segments:
if direction == 0: #X value
if facing == 1: #Right
segment.x = x - (35 * segment.pos)
segment.y = y
else: #Left
segment.x = x + (35 * segment.pos)
segment.y = y
else: #Y value
if facing == -1: #Up
segment.y = y + (35 * segment.pos)
segment.x = x
else:#Down
segment.y = y - (35 * segment.pos)
segment.x = x
for segment in segments:
segment.draw(win)
scoreDisplay = text.render(str(score),1,(255,255,255))
win.blit(scoreDisplay,(760,0))
pygame.draw.rect(win,(0,128,0),(x,y,30,30))
pygame.draw.rect(win,(255,0,0),(x2,y2,30,30))
pygame.display.update()
pygame.quit()
当玩家决定蛇移动的方向时,这将同时移动蛇的所有部分。但是,分段会立即捕捉到头部的x位置,而不是平滑地一次移动一个。如果有人能帮我解决这个问题,那就太好了。谢谢 不错的游戏。我建议创建一个点列表,它是蛇头位置的元组列表(
(x,y)
)。将每个职位添加到列表中:
pts=[]
运行时:
# [...]
附加说明((x,y))
创建一个函数,该函数通过计算到蛇头的索引(i
)来计算蛇的一部分的位置。到头部的距离必须为lenToI=i*35
到点之间的距离可以通过(
math.sqrt((px-pnx)*(px-pnx)+(py-pny)*(py-pny))
计算,其中点是(px,py)
和(pnx,pny)
。如果点之间的距离之和(lenAct
)超过到点的长度(lenToI
),然后找到零件i
的位置:
def getPos(i):
全球临时秘书处
lenToI=i*35
lenAct=0
px,py=pts[-1]
对于反向(范围(len(pts)-1)中的j:
px,py=pts[j]
pnx,pny=pts[j+1]
lenAct+=math.sqrt((px-pnx)*(px-pnx)+(py-pny)*(py-pny))
如果lenAct>=lenToI:
打破
返回(px,py)
编写另一个函数cutPts
,从列表中删除不需要的点:
def cutPts(i):
全球临时秘书处
lenToI=i*35
lenAct=0
切口i=0
px,py=pts[0]
对于反向(范围(len(pts)-1)中的j:
px,py=pts[j]
pnx,pny=pts[j+1]
lenAct+=math.sqrt((px-pnx)*(px-pnx)+(py-pny)*(py-pny))
如果lenAct>=lenToI:
打破
切口i=j
del pts[:cut_i]
更新循环中线段的位置:
pts.append((x,y))
对于范围内的i(len(段)):
段[i].x,段[i].y=getPos(len(段)-i)
切口(长度(分段)+1)
关于评论: 如果蛇头碰到它的任何一段,我将如何调用
gameOver()
函数?我尝试使用if语句来检查碰撞(与我在苹果上做的一样),使用segment.x和segment.y,但这不起作用,因为蛇移动时,蛇的第二段总是与蛇头重叠
注意,头部永远不能“接触”第一段,除非方向改变为相反方向,但这种情况可以通过额外的测试轻松处理。检查头部是否“击中”除第一个与头部相连的部分外的任何部分就足够了。
用于检查矩形线段的交点:
def selfCollide():
对于范围内的i(长度(段)-1):
s=段[i]
如果pygame.Rect(x,y,30,30).collide Rect(pygame.Rect(s.x,s.y,30,30)):
返回真值
返回错误
如果selfCollide():
gameOver()
现在我已经完成了这项工作,如果蛇头碰到它的任何一段,我该如何调用gameOver()函数呢?我尝试使用if语句检查碰撞(与我在苹果上做的一样)使用段.x和段.y,但这不起作用,因为蛇移动时,蛇的第二段始终与头部重叠。@Hydra注意,头部永远不能“接触”第一段,除非方向改变为相反方向,但这种情况可以通过额外的测试来处理。检查头部是否“击中”就足够了除第一个连接头部的部分外的任何部分。参见答案的扩展部分。
for segment in segments:
if direction == 0: #X value
if facing == 1: #Right
segment.x = x - (35 * segment.pos)
segment.y = y
else: #Left
segment.x = x + (35 * segment.pos)
segment.y = y
else: #Y value
if facing == -1: #Up
segment.y = y + (35 * segment.pos)
segment.x = x
else:#Down
segment.y = y - (35 * segment.pos)
segment.x = x