查看: 345  |  回复: 0
python写的贪食蛇小游戏打包成8.49MB的exe文件给别人用
楼主
发表于 2023年6月26日 21:16

新建p1.py,代码:

import turtle
import time
import random

DELAY = 0.1
WIDTH = 600
HEIGHT = 600
FOOD_SIZE = 10

# 初始化游戏窗口
wn = turtle.Screen()
wn.title("贪吃蛇")
wn.bgcolor("green")
wn.setup(WIDTH, HEIGHT)

# 创建蛇头
head = turtle.Turtle()
head.shape("square")
head.color("black")
head.penup()
head.goto(0,0)
head.direction = "stop"

# 创建食物
food = turtle.Turtle()
food.shape("circle")
food.color("red")
food.penup()
food.goto(0,100)

segments = []

# 记分牌
score = 0
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.hideturtle()
score_pen.goto(0, 260)
score_pen.write("得分: {}".format(score), align="center", font=("Courier", 24, "normal"))

# 移动函数
def move():
    if head.direction == "up":
        y = head.ycor()
        head.sety(y + 20)

    if head.direction == "down":
        y = head.ycor()
        head.sety(y - 20)

    if head.direction == "right":
        x = head.xcor()
        head.setx(x + 20)

    if head.direction == "left":
        x = head.xcor()
        head.setx(x - 20)

# 移动控制函数
def go_up():
    if head.direction != "down":
        head.direction = "up"

def go_down():
    if head.direction != "up":
        head.direction = "down"

def go_right():
    if head.direction != "left":
        head.direction = "right"

def go_left():
    if head.direction != "right":
        head.direction = "left"

# 绑定键盘输入与移动控制函数
wn.listen()
wn.onkeypress(go_up, "Up")
wn.onkeypress(go_down, "Down")
wn.onkeypress(go_left, "Left")
wn.onkeypress(go_right, "Right")

# 主循环
while True:
    wn.update()

    # 超出边界
    if head.xcor()>WIDTH/2 or head.xcor()<-WIDTH/2 or head.ycor()>HEIGHT/2 or head.ycor()<-HEIGHT/2:
        time.sleep(1)
        head.goto(0,0)
        head.direction = "stop"

        for segment in segments:
            segment.goto(1000, 1000)

        segments.clear()

        score = 0
        score_pen.clear()
        score_pen.write("得分: {}".format(score), align="center", font=("Courier", 24, "normal"))

    # 碰到自己
    for segment in segments:
        if segment.distance(head) < 20:
            time.sleep(1)
            head.goto(0,0)
            head.direction = "stop"

            for segment in segments:
                segment.goto(1000, 1000)

            segments.clear()

            score = 0
            score_pen.clear()
            score_pen.write("得分: {}".format(score), align="center", font=("Courier", 24, "normal"))

    # 吃到食物
    if head.distance(food) < 20:
        x = random.randint(-(WIDTH//2)+FOOD_SIZE, (WIDTH//2)-FOOD_SIZE)
        y = random.randint(-(HEIGHT//2)+FOOD_SIZE, (HEIGHT//2)-FOOD_SIZE)
        food.goto(x,y)

        new_segment = turtle.Turtle()
        new_segment.speed(0)
        new_segment.shape("square")
        new_segment.color("grey")
        new_segment.penup()
        segments.append(new_segment)

        score += 10
        score_pen.clear()
        score_pen.write("得分: {}".format(score), align="center", font=("Courier", 24, "normal"))

    # 移动身体
    for index in range(len(segments)-1, 0, -1):
        x = segments[index - 1].xcor()
        y = segments[index - 1].ycor()
        segments[index].goto(x, y)

    if len(segments) > 0:
        x = head.xcor()
        y = head.ycor()
        segments[0].goto(x,y)

    # 移动蛇头
    move()

    time.sleep(DELAY)

wn.mainloop()

可以安装一个pyinstaller,在命令窗口输入:

pip3 install pyinstaller

安装后检查:

pyinstaller

有命令提示usage出来证明成功。

在目录中,必须注意是大写F

pyinstaller -F p1.py

可以生成文件夹dist,拷贝里面的p1.exe给别人玩吧。

您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2024 MaNongKu.com