python贪吃蛇加障碍物

怎么给贪吃蛇加固定的障碍物?试过好多次,障碍物都是一闪而过和游戏分离开了,怎么让障碍物和游戏融合地较好?(解决后另有打赏)

img

import turtle as t
import copy as c
import random as r


            
#初始化,模拟蛇,就是一个方块列表
snake=[[0,0],[0,10],[0,20]]
aim=[0,10] #移动目标
food=[r.randrange(-25,25)*10,r.randrange(-25,25)*10]
time=300

   

#动态调整移动的方向                                                                                                                                                                  
def change(x,y):
    aim[0]=x
    aim[1]=y#画出方块
def square(x,y,size,color):
    t.penup()
    t.goto(x,y)
    t.pendown()
    t.color(color) #设置颜色
    t.begin_fill() #渲染
    for i in range(4):
        t.forward(size)
        t.left(90)
    t.end_fill()#渲染
 
#判断边界
def inside(head):
    if head[0]>250:
        head[0]-=250*2
    elif head[0]<-250:
        head[0]+=250*2
    if head[1]>250:
        head[1]-=250*2
    elif head[1]<-250:
        head[1]+=250*2
    return head
 
#移动--解析:头部添加方块,尾巴消除一个方块
# 最后画的最上面的方块--设定为头部
def move_snake():
    head=c.deepcopy(snake[-1]) #复制一份头部坐标
    #确定方向
    head=[head[0]+aim[0],head[1]+aim[1]] #确定新头的坐标
    if head in snake:
        print('game over')
        square(head[0],head[1],10,'yellow')
        
    #边界判断    
    inside(head)
    #移动蛇
    if head==food:
        food[0]=r.randrange(-24,24)*10
        food[1]=r.randrange(-24,24)*10
    else:
        snake.pop(0) #去尾巴
    snake.append(head) #添加头
 
    #删除之前的动画
    t.clear()
    
    #画食物
    square(food[0],food[1],10,'red')
    #障碍
    for i in snake:
        square(i[0],i[1],10,'black')
        
    t.update() #刷新
    t.ontimer(move_snake,time) #递归
                
t.tracer(False)#除去动画效果
t.hideturtle() #隐藏箭头
#监听键盘
t.listen()
t.onkey(lambda :change(0,10),'w')
t.onkey(lambda :change(0,-10),'s')
t.onkey(lambda :change(10,0),'d')
t.onkey(lambda :change(-10,0),'a')
#设定屏幕大小
t.setup(500,500)
#运行
move_snake()



t.done() #暂停屏幕

不是有红色方块障碍物吗?

这样的可以嘛,如果可以采纳一下,我发你

img

兄弟~把完整的代码发上来吧

你试试这个行不行

import pygame

import sys

import random

SCREEN_X=600

SCREEN_Y=600

class Snake(object):

def __init__(self):

self.direction=pygame.K_RIGHT

self.body = []

for i in range(5):

self.addnode()

def addnode(self):

left, top = (0, 0)

if self.body:

left, top = (self.body[0].left,self.body[0].top)

node = pygame.Rect(left,top,25,25)

if self.direction == pygame.K_RIGHT:

node.left += 25

elif self.direction == pygame.K_LEFT:

node.left -= 25

elif self.direction == pygame.K_UP:

node.top -= 25

elif self.direction == pygame.K_DOWN:

node.top += 25

self.body.insert(0,node)

def delnode(self):

self.body.pop()

def isdead(self):

if self.body[0].x not in range(SCREEN_X):

return True

if self.body[0].y not in range(SCREEN_Y):

return True

if self.body[0] in self.body[1:]:

print('in body')

return True

return False

def move(self):

self.addnode()

self.delnode()

def changeddirection(self,curkey):

LR = [pygame.K_LEFT,pygame.K_RIGHT]

UD = [pygame.K_DOWN,pygame.K_UP]

if curkey in LR+UD:

if (curkey in LR) and (self.direction in LR):

return

if (curkey in UD) and (self.direction in UD):

return

self.direction = curkey

class Food(object):

def __init__(self):

self.rect = pygame.Rect(-25, 0, 25, 25)

def remove(self):

self.rect.x = -25

def set(self):

if self.rect.x == -25:

allpos = []

for pos in range(25,SCREEN_X-25,25):

allpos.append(pos)

self.rect.left = random.choice(allpos)

self.rect.top = random.choice(allpos)

print(self.rect)

def show_text(screen,pos,text,color,font_bold = False,font_size = 60,font_italic = False):

cur_font=pygame.font.SysFont('宋体',font_size)

cur_font.set_bold(font_bold)

cur_font.set_italic(font_italic)

text_fmt = cur_font.render(text,1,color)

screen.blit(text_fmt,pos)

def main():

pygame.init()

screen_size = (SCREEN_X,SCREEN_Y)

screen = pygame.display.set_mode(screen_size)

pygame.display.set_caption('Snake')

clock=pygame.time.Clock()

scores=0

isdead=False

snake=Snake()

food=Food()

while True:

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

if event.type == pygame.KEYDOWN:

#print(event)

#pressed_keys = pygame.key.get_pressed()

#if pressed_keys.count(1)>1:

# continue

print(event.key)

snake.changeddirection(event.key)

if event.key == pygame.K_SPACE and isdead:

return main()

screen.fill((255,255,255))

if not isdead:

scores +=1

snake.move()

for rect in snake.body:

pygame.draw.rect(screen,(20,220,39),rect,0)

isdead=snake.isdead()

if isdead:

show_text(screen,(100,200),'you dead!',(227,29,18),False,100)

show_text(screen,(150,260),'press space to try again...',(0,0,22),False,30)

if food.rect == snake.body[0]:

scores +=50

food.remove()

snake.addnode()

food.set()

pygame.draw.rect(screen,(136,0,21),food.rect,0)

show_text(screen,(50,500),'Scores:'+str(scores),(223,223,223))

pygame.display.update()

clock.tick(5)

if __name__ == "__main__":

main()

泻药,不会