汇智学堂-phthon小游戏(贪吃蛇游戏之九-右侧吃到食物后蛇身变长)

4.9右侧吃到食物后蛇身变长
我们在上一节中,蛇吃到食物后,蛇身没能变长,而生成的那一节蛇身,停留在食物原来的位置上。这一节我们解决这个问题。下面是我们要做的事情。
1、蛇身变长。
2、上节生成的一节蛇身消失。

实现代码如下:

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]))
        del g.body[3]
        
    elif g.x==0 and g.y==-1:
        print("已进入上侧")
        a=g.body[0][1]-1              
        g.body.insert(0,(g.body[0][0],a))
        del g.body[3]

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

    #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')   

我们通过if g.eatFood(f)==0: 来判断删除最后一节蛇身,如果等于1,就不删除。等于1的含义就是当蛇吃到食物时。

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

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]))
        del g.body[3]
        
    elif g.x==0 and g.y==-1:
        print("已进入上侧")
        a=g.body[0][1]-1              
        g.body.insert(0,(g.body[0][0],a))
        del g.body[3]

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

    #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-17、4-18。
在这里插入图片描述
图4-17
在这里插入图片描述
图4-18

猜你喜欢

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