Python贪吃蛇小游戏教程

Python贪吃蛇小游戏教程,第1张

今天教大家使用pygame编一个简单贪吃蛇小游戏。

1.导库

import pygame, sys, time, random, zt
"""
zt库是用来搞游戏暂停的
"""

前面几个库是自带库,最后一个zt暂停库源代码:

def game_stop():
    global flag
    flag = False
def game_go():
    global flag
    flag = True

2.游戏初始化。

speed2 = 0.3
speed = speed2
color_food = pygame.Color(random.randint(0,240), random.randint(0,240), random.randint(0,240))
color_white = pygame.Color(240, 240, 240)
color_green = pygame.Color(50, 50, 120)
pygame.init()
SIZE = GAMEWIDTH,GAMEHIGHT = 600,400
screen = pygame.display.set_mode(SIZE)
screen.fill(color_white)
pygame.display.set_caption("贪吃蛇小游戏")
arr = [([0] * 41) for i in range(61)]  # 创建一个二维数组
x = 10  # 蛇的初始x坐标
y = 10  # 蛇的初始y坐标
foodx = random.randint(1, 60)  # 食物随机生成的x坐标
foody = random.randint(1, 40)  # 食物随机生成的y坐标
arr[foodx][foody] = -1
snake_lon = random.randint(3,7)  # 蛇的长度
snake_tired = 0
way = 1  # 蛇的运动方向
font = pygame.font.SysFont(None,30)
font2 = pygame.font.SysFont(None,100)

speed2 = 蛇初始速度

speed = 蛇改变后的速度

color_food = 食物颜色

SIZE = 游戏窗口大小

3.游戏主循环。

while True:
    screen.fill(color_white)
    time.sleep(speed)
    for event in pygame.event.get():
        if snake_tired > 0:
            speed = speed2 - 0.2
        else:
            speed = speed2
        if snake_tired > 25:
            if event.type == pygame.MOUSEBUTTONDOWN:
                speed = speed2 - 0.25
                snake_tired -= 25
            elif event.type == pygame.MOUSEBUTTONUP:
                speed = speed2 - 0.02
        # 监听器
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if (event.key == pygame.K_RIGHT) and (way != 2):  # 向右移动且避免反向移动
                way = 1
            if (event.key == pygame.K_LEFT) and (way != 1):  # 向左移动且避免反向移动
                way = 2
            if (event.key == pygame.K_UP) and (way != 4):  # 向上移动且避免反向移动
                way = 3
            if (event.key == pygame.K_DOWN) and (way != 3):  # 向下移动且避免反向移动
                way = 4
    if way == 1:
        x += 1
    if way == 2:
        x -= 1
    if way == 3:
        y -= 1
    if way == 4:
        y += 1
    if (x > 120) or (y > 80) or (x < 1) or (y < 1) or (arr[x][y] > 0):  # 判断死亡(撞墙或自食)
        zt.game_stop()
        time.sleep(2)
        sys.exit()
    arr[x][y] = snake_lon
    for a, b in enumerate(arr, 1):
        for c, d in enumerate(b, 1):
            if (d > 0):
                # print(a,c) #输出蛇的当前坐标
                arr[a - 1][c - 1] = arr[a - 1][c - 1] - 1
                pygame.draw.rect(screen, color_green, ((a - 1) * 10, (c - 1) * 10, 10, 10))
            if (d < 0):
                pygame.draw.rect(screen, color_food, ((a - 1) * 10, (c - 1) * 10, 10, 10))
    if (x == foodx) and (y == foody):
        snake_lon += random.randint(1,3)
        snake_tired += 100
        while (arr[foodx][foody] != 0):
            foodx = random.randint(1, 60)
            foody = random.randint(1, 20)
        arr[foodx][foody] = -1

    text1 = font.render(str(snake_lon),True,(0,0,0),(200,200,200))
    screen.blit(text1,(GAMEWIDTH * .01, 5))
    text2 = font.render(str(snake_tired), True, (0, 0, 0), (200, 200, 200))
    screen.blit(text2, (GAMEWIDTH * .93, 5))
    pygame.display.update()
重点来了!!!
for event in pygame.event.get():
    if snake_tired > 0:
            speed = speed2 - 0.2
        else:
            speed = speed2
        if snake_tired > 25:
            if event.type == pygame.MOUSEBUTTONDOWN:
                speed = speed2 - 0.25
                snake_tired -= 25
            elif event.type == pygame.MOUSEBUTTONUP:
                speed = speed2 - 0.02

主循环中这一段是设置蛇速度与能量的。

elif event.type == pygame.KEYDOWN:
            if (event.key == pygame.K_RIGHT) and (way != 2):  # 向右移动且避免反向移动
                way = 1
            if (event.key == pygame.K_LEFT) and (way != 1):  # 向左移动且避免反向移动
                way = 2
            if (event.key == pygame.K_UP) and (way != 4):  # 向上移动且避免反向移动
                way = 3
            if (event.key == pygame.K_DOWN) and (way != 3):  # 向下移动且避免反向移动
                way = 4
    if way == 1:
        x += 1
    if way == 2:
        x -= 1
    if way == 3:
        y -= 1
    if way == 4:
        y += 1
    if (x > 120) or (y > 80) or (x < 1) or (y < 1) or (arr[x][y] > 0):  # 判断死亡(撞墙或自食)
        zt.game_stop()
        time.sleep(2)
        sys.exit()

这一段是设置蛇移动的。

text1 = font.render(str(snake_lon),True,(0,0,0),(200,200,200))
screen.blit(text1,(GAMEWIDTH * .01, 5))
text2 = font.render(str(snake_tired), True, (0, 0, 0), (200, 200, 200))
screen.blit(text2, (GAMEWIDTH * .93, 5))
pygame.display.update()

这一段是显示分数与能量的。

源代码:

import pygame, sys, time, random, zt

speed2 = 0.3
speed = speed2
color_food = pygame.Color(random.randint(0,240), random.randint(0,240), random.randint(0,240))
color_white = pygame.Color(240, 240, 240)
color_green = pygame.Color(50, 50, 120)
pygame.init()
SIZE = GAMEWIDTH,GAMEHIGHT = 600,400
screen = pygame.display.set_mode(SIZE)
screen.fill(color_white)
pygame.display.set_caption("贪吃蛇小游戏")
arr = [([0] * 41) for i in range(61)]  # 创建一个二维数组
x = 10  # 蛇的初始x坐标
y = 10  # 蛇的初始y坐标
foodx = random.randint(1, 60)  # 食物随机生成的x坐标
foody = random.randint(1, 40)  # 食物随机生成的y坐标
arr[foodx][foody] = -1
snake_lon = random.randint(3,7)  # 蛇的长度
snake_tired = 0
way = 1  # 蛇的运动方向
font = pygame.font.SysFont(None,30)
font2 = pygame.font.SysFont(None,100)
while True:
    screen.fill(color_white)
    time.sleep(speed)
    for event in pygame.event.get():
        if snake_tired > 0:
            speed = speed2 - 0.2
        else:
            speed = speed2
        if snake_tired > 25:
            if event.type == pygame.MOUSEBUTTONDOWN:
                speed = speed2 - 0.25
                snake_tired -= 25
            elif event.type == pygame.MOUSEBUTTONUP:
                speed = speed2 - 0.02
        # 监听器
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if (event.key == pygame.K_RIGHT) and (way != 2):  # 向右移动且避免反向移动
                way = 1
            if (event.key == pygame.K_LEFT) and (way != 1):  # 向左移动且避免反向移动
                way = 2
            if (event.key == pygame.K_UP) and (way != 4):  # 向上移动且避免反向移动
                way = 3
            if (event.key == pygame.K_DOWN) and (way != 3):  # 向下移动且避免反向移动
                way = 4
    if way == 1:
        x += 1
    if way == 2:
        x -= 1
    if way == 3:
        y -= 1
    if way == 4:
        y += 1
    if (x > 120) or (y > 80) or (x < 1) or (y < 1) or (arr[x][y] > 0):  # 判断死亡(撞墙或自食)
        zt.game_stop()
        time.sleep(2)
        sys.exit()
    arr[x][y] = snake_lon
    for a, b in enumerate(arr, 1):
        for c, d in enumerate(b, 1):
            if (d > 0):
                # print(a,c) #输出蛇的当前坐标
                arr[a - 1][c - 1] = arr[a - 1][c - 1] - 1
                pygame.draw.rect(screen, color_green, ((a - 1) * 10, (c - 1) * 10, 10, 10))
            if (d < 0):
                pygame.draw.rect(screen, color_food, ((a - 1) * 10, (c - 1) * 10, 10, 10))
    if (x == foodx) and (y == foody):
        snake_lon += random.randint(1,3)
        snake_tired += 100
        while (arr[foodx][foody] != 0):
            foodx = random.randint(1, 60)
            foody = random.randint(1, 20)
        arr[foodx][foody] = -1

    text1 = font.render(str(snake_lon),True,(0,0,0),(200,200,200))
    screen.blit(text1,(GAMEWIDTH * .01, 5))
    text2 = font.render(str(snake_tired), True, (0, 0, 0), (200, 200, 200))
    screen.blit(text2, (GAMEWIDTH * .93, 5))
    pygame.display.update()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/794828.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存