汇智学堂-phthon小游戏(贪吃蛇游戏之十-完成上下左吃到食物后蛇身变长)

4.10完成上下左吃到食物后蛇身变长
我们结合上一节的分析,修改代码,完成上下左右吃到食物后蛇身变长。下面是我们要做的事情。
1、完成左侧吃到食物后蛇身变长。
2、完成上侧吃到食物后蛇身变长。
3、完成下侧吃到食物后蛇身变长。

实现代码如下:

class Snake:

def __init__(self):        
    
    self.y=0
         
def draw(self,g,f):
    if(g.x==0 and g.y==0):
        a=8
   
    elif g.x==1 and g.y==0:
        print("已进入右侧")
        a=g.body[0][0]+1              
        g.body.insert(0,(a,g.body[0][1]))

        if g.eatFood(f)==0:          
           del g.body[g.delindex]
        elif g.eatFood(f)==1:
           g.newfood=1
           g.delindex+=1
           print('eatFood')
        
    elif g.x==-1 and g.y==0:
        print("已进入左侧")
        a=g.body[0][0]-1              
        g.body.insert(0,(a,g.body[0][1]))
        
        if g.eatFood(f)==0:          
           del g.body[g.delindex]
        elif g.eatFood(f)==1:
           g.newfood=1
           g.delindex+=1
           print('eatFood')
        
    elif g.x==0 and g.y==-1:
        print("已进入上侧")
        a=g.body[0][1]-1              
        g.body.insert(0,(g.body[0][0],a))

        if g.eatFood(f)==0:          
           del g.body[g.delindex]
        elif g.eatFood(f)==1:
           g.newfood=1
           g.delindex+=1
           print('eatFood')

    elif g.x==0 and g.y==1:
        print("已进入下侧")
        a=g.body[0][1]+1              
        g.body.insert(0,(g.body[0][0],a))

        if g.eatFood(f)==0:          
           del g.body[g.delindex]
        elif g.eatFood(f)==1:
           g.newfood=1
           g.delindex+=1
           print('eatFood')

    #print(g.body[0][0]*20)
    #print(f.x11rec)
    print(g.newfood)

    #tkinter.messagebox.showinfo(len(self.body))            
    
    for i in g.body:

         x1=i[0]*20+g.x*20
         y1=i[1]*20+g.y*20                        
         g.canvas.create_rectangle(x1,y1,x1+20,y1+20,fill='green')    

通过修改g.delindex的值,我们保证正确删除蛇身的位置。

将代码整合起来,整合后完整代码如下:

from tkinter import*
import random
import time
import tkinter.messagebox #messagebox

‘’‘上下左右吃到食物后,蛇身变长’’’

class Game:
def init(self):

    self.tk=Tk()
    self.canvas=Canvas(self.tk,width=600,height=600)
    self.canvas.pack()

    #横坐标增量
    self.x=0
    self.y=0

    #newfood=0代表食物没被吃掉
    #delindex=3代表从第三节开始删除蛇身

    self.newfood=0
    self.delindex=3

    self.body = [(8, 11), (8, 12), (8, 13)]

    self.canvas.bind_all('<KeyPress-Left>',self.turn_left)
    self.canvas.bind_all('<KeyPress-Right>',self.turn_right)
    self.canvas.bind_all('<KeyPress-Up>',self.turn_up)
    self.canvas.bind_all('<KeyPress-Down>',self.turn_down)
   
def turn_left(self,evt):
    self.x=-1
    self.y=0
   
def turn_right(self,evt):
    self.x=1
    self.y=0
    
def turn_up(self,evt):
    self.x=0
    self.y=-1   
    
def turn_down(self,evt):
    self.x=0
    self.y=1

def eatFood(self,f):
    if self.body[0][0]*20==f.x11rec and self.body[0][1]*20==f.y12rec:
        return 1
    else:
        return 0

class Snake:

def __init__(self):        
    
    self.y=0
         
def draw(self,g,f):
    if(g.x==0 and g.y==0):
        a=8
   
    elif g.x==1 and g.y==0:
        print("已进入右侧")
        a=g.body[0][0]+1              
        g.body.insert(0,(a,g.body[0][1]))

        if g.eatFood(f)==0:          
           del g.body[g.delindex]
        elif g.eatFood(f)==1:
           g.newfood=1
           g.delindex+=1
           print('eatFood')
           
        
    elif g.x==-1 and g.y==0:
        print("已进入左侧")
        a=g.body[0][0]-1              
        g.body.insert(0,(a,g.body[0][1]))
        
        if g.eatFood(f)==0:          
           del g.body[g.delindex]
        elif g.eatFood(f)==1:
           g.newfood=1
           g.delindex+=1
           print('eatFood')
        
    elif g.x==0 and g.y==-1:
        print("已进入上侧")
        a=g.body[0][1]-1              
        g.body.insert(0,(g.body[0][0],a))

        if g.eatFood(f)==0:          
           del g.body[g.delindex]
        elif g.eatFood(f)==1:
           g.newfood=1
           g.delindex+=1
           print('eatFood')

    elif g.x==0 and g.y==1:
        print("已进入下侧")
        a=g.body[0][1]+1              
        g.body.insert(0,(g.body[0][0],a))

        if g.eatFood(f)==0:          
           del g.body[g.delindex]
        elif g.eatFood(f)==1:
           g.newfood=1
           g.delindex+=1
           print('eatFood')

    #print(g.body[0][0]*20)
    #print(f.x11rec)
    print(g.newfood)

    #tkinter.messagebox.showinfo(len(self.body))            
    
    for i in g.body:

         x1=i[0]*20+g.x*20
         y1=i[1]*20+g.y*20                        
         g.canvas.create_rectangle(x1,y1,x1+20,y1+20,fill='green')    

class Food:
def init(self):

    self.a=random.randint(1,29)
    self.b=random.randint(1,29)

    self.x11rec=self.a*20
    self.y12rec=self.b*20
    self.x21rec=self.a*20+20
    self.y22rec=self.b*20+20     
    
def drawfood(self,g):
    g.canvas.create_rectangle(self.x11rec,self.y12rec,self.x21rec,self.y22rec,fill='red')    

g=Game()
f=Food()
s=Snake()
while 1:

g.canvas.delete("all") #clear canvas       

if g.newfood==1:
    f=Food()
    print('food new')
    g.newfood=0
    
f.drawfood(g)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
s.draw(g,f)   

g.tk.update_idletasks()
g.tk.update()
v=0.3
time.sleep(v)

运行这段代码,在画布上,我们看到蛇按我们的按键方向上下左右运动时,接触吃到食物后,蛇身变长一节。见下图4-19。
在这里插入图片描述

图4-19

猜你喜欢

转载自blog.csdn.net/weixin_39593940/article/details/88401894