猫史档案馆


【Python作品分享】贪吃蛇

用户:糖醋龙酱=w=糖醋龙酱=w=查看:8 回复:4 评论:8 创建时间:2022-01-25T16:57:12


【作品展示】

center_image

 

【作品介绍】

for somebody

*_*

你腻害A!

 

【作品源代码】

import random  # 导入随机库
import time  
import pgzrun  


TILE_SIZE = 20  
WIDTH = 40*TILE_SIZE  
HEIGHT = 40*TILE_SIZE  

snkaeHead = Actor('snake1')  
snkaeHead.x = WIDTH/2   
snkaeHead.y = HEIGHT/2  

cookie = Actor('cookie')  
cookie.x = random.randint(10, 30)*TILE_SIZE  
cookie.y = random.randint(10, 30)*TILE_SIZE  

Snake = []  
Snake.append(snkaeHead)  

direction = 'up'  
isLoose = False  
score = 0  

for i in range(4):  
    snakebody = Actor('snake1')  
    snakebody.x = Snake[i].x - TILE_SIZE  
    snakebody.y = Snake[i].y  
    Snake.append(snakebody)  


def draw():  
    screen.clear()  
    for snkaebody in Snake:  
        snkaebody.draw()
    cookie.draw()  
    screen.draw.text("得分:"+str(score), (360, 20), fontsize=25,
                     fontname='s', color='white')
    if isLoose:
        screen.clear()
        screen.draw.text("游戏失败!", (180, HEIGHT/2-100),
                         fontsize=100, fontname='s', color='blue')


def update():  
    global direction
    if keyboard.left: 
        direction = 'left' 
    if keyboard.right:  
        direction = 'right' 
    if keyboard.up:  
        direction = 'up'  
    if keyboard.down:  
        direction = 'down'  


def moveSnake():  
    global direction, isLoose, score

    newSnakeHead = Actor('snake1') 

    newSnakeHead = Actor('snake1')
    if direction == 'right':  
        newSnakeHead.x = Snake[0].x + TILE_SIZE
        newSnakeHead.y = Snake[0].y
    if direction == 'left':  
        newSnakeHead.x = Snake[0].x - TILE_SIZE
        newSnakeHead.y = Snake[0].y
    if direction == 'up':  
        newSnakeHead.x = Snake[0].x
        newSnakeHead.y = Snake[0].y - TILE_SIZE
    if direction == 'down':  
        newSnakeHead.x = Snake[0].x
        newSnakeHead.y = Snake[0].y + TILE_SIZE

    if newSnakeHead.y < 0 or newSnakeHead.y > HEIGHT \
            or newSnakeHead.x < 0 or newSnakeHead.x > WIDTH:
        isLoose = True

    for snakebody in Snake:  
        if newSnakeHead.x == snakebody.x and newSnakeHead.y == snakebody.y:
            isLoose = True
            break

    if newSnakeHead.x == cookie.x and newSnakeHead.y == cookie.y:
        cookie.x = random.randint(5, 35)*TILE_SIZE
        cookie.y = random.randint(5, 25)*TILE_SIZE
        score = score + 1  
    else:  
        del Snake[len(Snake)-1]

    Snake.insert(0, newSnakeHead)  
    clock.schedule_unique(moveSnake, 0.2)  


moveSnake()

pgzrun.go()  

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
糖醋龙酱=w=糖醋龙酱=w=

文件:

点赞0


评论


糖醋龙酱=w=糖醋龙酱=w=

center_image

                                                                                       |   

                                                                                       |

                                                                                       |                          

                                                                                      Vcenter_image

点赞0


评论


糖醋龙酱=w=糖醋龙酱=w=

center_imagecenter_image

点赞0


评论


SLC_KinGMonKeYSLC_KinGMonKeY

大兄弟:

center_image

……

点赞0


评论