标签:self height idt tle etc black 大小 经典 import
贪吃蛇是Nokia手机上面的经典游戏,我们可以利用Turle模块实现一个自己的贪吃蛇游戏。效果图如下所示
贪吃蛇的程序里面,简单的说,有几个问题需要解决:
上面几个问题一个个解决了,最后的程序也就搞定了。
解决方案:
除了上面几个问题,还有一个需要注意点就是,需要关闭默认的动画效果,不然身子的每一个方块的移动慢动作他都会显示出来,看起来就像一个诡异的毛毛虫在移动。解决方法是关闭动画效果,然后每次在所有的方格都移动之后,我们手动的刷新一下界面,这样连续效果看起来就像是一条整蛇在动。
说了这么多,最后看看源代码吧。
就4个py文件,main文件里面调用其他三个类的文件
main.py
from turtle import Screen
import time
from snake import Snake
from food import Food
from scoreboard import Scoreboard
#初始化画布,设置长度,宽度和标题
screen = Screen()
screen.setup(width=600, height=600)
screen.bgcolor("black")
screen.title("Snake Game")
#tracer设置为0的作用是关闭动画效果,我们通过timer设置延时,然后通过update手动刷新界面,否则默认的动画效果看起来就是每个方块的移动效果
#想象一下GIF或者CRT显示器的原理,多个画面连续刷新,看起来就像动起来一样
screen.tracer(0)
#实例化三个对象
snake_segments = Snake()
food = Food()
scoreboard = Scoreboard()
#监听上下左右的键盘操作
screen.listen()
screen.onkey(snake_segments.up, "Up")
screen.onkey(snake_segments.down, "Down")
screen.onkey(snake_segments.left, "Left")
screen.onkey(snake_segments.right, "Right")
#布尔值判断是否结束游戏
game_is_on = True
while game_is_on:
#每次停顿0.1秒后刷新一下界面,然后蛇移动一下
screen.update()
time.sleep(0.1)
snake_segments.move()
# 如果蛇头碰见食物了,那么食物刷新随机生成一下,分数加一,蛇身长度加一
if snake_segments.head.distance(food) < 15:
print("yum yum yum")
food.refresh()
scoreboard.addscore()
snake_segments.add_segment()
# 如果蛇头撞墙了,那么Game over
if snake_segments.head.xcor() > 280 or snake_segments.head.xcor() < -280 or snake_segments.head.ycor() > 280 or snake_segments.head.ycor() < -280:
game_is_on = False
scoreboard.gameover()
# 如果蛇头撞到身子了,那么Game over,注意列表是从第二节开始的,排除蛇头
for seg in snake_segments.segments[1:]:
if snake_segments.head.distance(seg) < 10:
game_is_on = False
scoreboard.gameover()
screen.exitonclick()
scoreboard.py
from turtle import Turtle
ALIGNMENT="center"
FONT=("Arial",20,"normal")
#显示分数和Game over等标记
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.color(‘white‘)
self.penup()
self.hideturtle()
self.score=0
self.updatescore()
def updatescore(self):
self.goto(0, 270)
self.write(f"SCORE = {self.score}",True, align=ALIGNMENT,font=FONT)
self.goto(0,250)
# self.write("-"*300,True, align=ALIGNMENT,font=FONT)
def addscore(self):
self.score+=1
self.clear()
self.updatescore()
def gameover(self):
#self.clear()
self.goto(0,0)
self.write(f"GAME OVER",True, align=ALIGNMENT, font=FONT)
snake.py
from turtle import Turtle
MOVE_DISTANCE=20
class Snake:
#初始化的时候,蛇有三节,一字排开,放到一个列表里
def __init__(self):
self.segments = []
for i in range(3):
seg = Turtle(shape="square")
seg.color(‘white‘)
seg.penup()
seg.goto(0 - 20 * i, 0)
self.segments.append(seg)
self.head=self.segments[0]
#这个是最核心的部分,每次移动的时候,从蛇尾巴往前循环,每个方块往上一个方块的坐标移动,蛇头自动往前跑
def move(self):
for seg_num in range(len(self.segments) - 1, 0, -1):
new_x = self.segments[seg_num - 1].xcor()
new_y = self.segments[seg_num - 1].ycor()
self.segments[seg_num].goto(new_x, new_y)
self.segments[0].forward(MOVE_DISTANCE)
#蛇头往上跑
def up(self):
if self.head.heading() !=270:
self.head.setheading(to_angle=90)
#蛇头往下跑
def down(self):
if self.head.heading() != 90:
self.head.setheading(to_angle=270)
#蛇头往左跑
def left(self):
if self.head.heading() !=0:
self.head.setheading(to_angle=180)
#蛇头往右跑
def right(self):
if self.head.heading() !=180:
self.head.setheading(to_angle=0)
#蛇的身子加1,原理是新创建一个实例,然后放到蛇尾巴的位置
def add_segment(self):
seg = Turtle(shape="square")
seg.color(‘white‘)
seg.penup()
tail = self.segments[-1]
seg.goto(tail.xcor(),tail.ycor())
self.segments.append(seg)
food.py
from turtle import Turtle
import random
class Food(Turtle):
def __init__(self):
super().__init__()
self.shape(‘circle‘)
self.penup()
#self.shapesize(stretch_len=0.5,stretch_wid=0.5)
self.color(‘blue‘)
self.speed(‘fastest‘)
# self.goto(random.randint(-280,280),random.randint(-280,280))
self.refresh()
def refresh(self):
self.goto(random.randint(-280, 280), random.randint(-280, 260))
很简单吧
用 Python Turtle 模块做小游戏 (3) - 贪吃蛇
标签:self height idt tle etc black 大小 经典 import
原文地址:https://blog.51cto.com/beanxyz/2551882