貪吃蛇Python小遊戲(原始碼註解貼上即用) 這款貪吃蛇遊戲十分簡便,規避使用難以載包的pygame,以下是運行圖:
#遊戲程式碼實作1.繪製圖像在繪製遊戲圖像仿麥呢,我們採用的是資料庫中較之pygame更為輕巧的Turtle Graphics建構資料庫(gamebase.py)from turtle import * # "*"是引用所有函数 def square(x, y, size, color_name): up() goto(x, y) down() color(color_name) begin_fill() forward(size) left(90) forward(size) left(90) forward(size) left(90) forward(size) left(90) end_fill()上面是透過烏龜製圖,前進單位距離後旋轉90度,然後再旋轉90度直到繪製出一個小方塊繪製蘋果(snake.py)引用剛剛我們繪圖的資料庫
from turtle import * from gamebase import square from random import randrange然後定義畫布的大小
setup(420,420,0,0) //隐藏乌龟头 emoj.emoj. hideturtle //隐藏轨迹 tracer(False) //绘制 done()
def gameLoop(): //随机生成苹果 apple_x = randrange(-200, 200) apple_y = randrange(-200, 200) //绘制苹果 square(apple_x, apple_y, 10, "red") //刷新画布 update()繪製貪吃蛇(snake.py)#注意,我們可以把貪吃蛇看作一個隊列,而這個隊列之中的每一個元素都包含兩個變數(該元素的橫縱座標)
def gameLoop(): //随机生成苹果 apple_x = randrange(-200, 200) apple_y = randrange(-200, 200) //绘制蛇 for n in range(len(sanke)): square(snake[n][0],snake[n][1[],10,"black) //绘制苹果 square(apple_x, apple_y, 10, "red") //刷新画布 update()繪製貪吃蛇的運動貪吃蛇運動原理:為了方便使蛇移動,我們要把蛇倒裝到佇列中,在它移動的時候,我們會拋出蛇隊列的第一個元素(pop()),然後,在蛇尾新增一個元素(append())
global apple_x, apple_y, snake, aim_x, aim_y #全局变量申请snake.append([ snake[-1][0] + aim_x, snake[-1][1] + aim_y ])snake.pop(0)global apple_x, apple_y, snake, aim_x, aim_y #全局变量申请 snake.append([ snake[-1][0] + aim_x, snake[-1][1] + aim_y ]) snake.pop(0)
ontimer(gameLoop, 100) # 每100毫秒运行一下gameLoop函数
listen() #监听 onkey(lambda: change(0, 10), "w") onkey(lambda: change(0, -10), "s") onkey(lambda: change(-10, 0), "a") onkey(lambda: change(10, 0), "d") gameLoop()###判定蛇是否吃到蘋果######這個也是十分容易,我們只用比較蛇頭的橫縱座標是否都等於蘋果的橫縱座標(蛇頭與蘋果重合)###
if snake[-1][0] != apple_x or snake[-1][1] != apple_y: snake.pop(0) else: apple_x = randrange(-20, 18) * 10 apple_y = randrange(-19, 19) * 10###判定蛇是否咬到自己######這個原理和上面蛇是否吃到蘋果原理類似###
def bite(): for n in range(len(snake)-1): if snake[-1][0] == snake[n][0] and snake[-1][1] == snake[n][1]: return True return False###判定蛇是否在界內###
def inside(): if -200 <= snake[-1][0] <= 180 and -190 <= snake[-1][1]<=190: return True else : return False###遊戲源碼######gamebase.py# ##
from turtle import * # "*"是引用所有函数 def square(x, y, size, color_name): up() goto(x, y) down() color(color_name) begin_fill() forward(size) left(90) forward(size) left(90) forward(size) left(90) forward(size) left(90) end_fill()###snake.py###
from time import sleep apple_x = randrange(-20, 18) * 10 apple_y = randrange(-19, 19) * 10 snake = [[0, 0], [10, 0], [20, 0], [30, 0], [40, 0], [50, 0]] aim_x = 10 aim_y = 0 def inside(): if -200 <= snake[-1][0] <= 180 and -190 <= snake[-1][1]<=190: return True else : return False def change(x, y): global aim_x, aim_y aim_x = x aim_y = y def bite(): for n in range(len(snake)-1): if snake[-1][0] == snake[n][0] and snake[-1][1] == snake[n][1]: return True return False def gameLoop(): global apple_x, apple_y, snake, aim_x, aim_y #全局变量申请 snake.append([ snake[-1][0] + aim_x, snake[-1][1] + aim_y ]) if snake[-1][0] != apple_x or snake[-1][1] != apple_y: snake.pop(0) else: apple_x = randrange(-20, 18) * 10 apple_y = randrange(-19, 19) * 10 if(not inside()) or bite(): square(snake[-1][0], snake[-1][1], 10,"hotpink") update() sleep(2)# 暂停2秒 apple_x = randrange(-20, 18) * 10 apple_y = randrange(-19, 19) * 10 snake = [[0, 0], [10, 0], [20, 0], [30, 0], [40, 0], [50, 0]] aim_x = 10 aim_y = 0 n = 0 clear() square(-210,-200,410,"black") square(-200,-190,390,"white") square(apple_x, apple_y, 10, "red") for n in range(len(snake)): square(snake[n][0], snake[n][1], 10, 'black') ontimer(gameLoop, 100) # 每300毫秒运行一下gameLoop函数 update() #注意:代码的前后顺序会给游戏带来不同的体感 setup(420, 420, 0, 0) hideturtle() tracer(False) listen() #监听 onkey(lambda: change(0, 10), "w") onkey(lambda: change(0, -10), "s") onkey(lambda: change(-10, 0), "a") onkey(lambda: change(10, 0), "d") gameLoop() done()
以上是如何在Python中寫出貪吃蛇遊戲?的詳細內容。更多資訊請關注PHP中文網其他相關文章!