猫史档案馆


pygame经典贪吃蛇

用户:程序侯程序侯查看:10 回复:5 评论:10 创建时间:2020-07-04T23:42:29


 

贪吃蛇,我们用kitten or Nemo or scratch都作过很多,但你能用python编写一个吗?当然,无疑是pygame:center_image

emotion_雷电猴_围观

首先我们要导入所有需要的第三方库——被刺编程的主题——pygame,只用到了一个停止程序运行的函数——exit的sys,还有用来检测按键的pygame.key;以及两个内置库——用来操作时间的time,和用爱生成随机数的rondom(是不是废话太多了。):

import pygame, sys, time, random
from pygame.key import *

接着,我们要初始化,并创建一个400×400“舞台”:

pygame.init()#说实话,开始我没加init,也不知道咋地,没问题!
sceen = pygame.display.set_mode((400, 400))

你会看到一个黑框一闪而过,那是我们的“舞台”创建成功了——紧接着程序结束了。若要让它一直保持,while True。

然,我们要的背景色是白色,不是黑色;(可选)还要加个标题:

sceen.fill((255, 255, 255))
pygame.display.flip()#该函数用来加画出的图,即刷新——pygame每个“舞台”都有一个副本!
pygame.display.set_caption('经典贪吃蛇游戏即将开始')

接下来是初始化and定义函数。

首先是一些函数

class draw:#用来画画的类 #20×20像素为一个格子
    def hand(x, y, direction):#画头
        pygame.draw.rect(sceen, (85, 85, 85), (x * 20 + 1, y * 20 + 1, 18, 18), 0)#画头
        if direction == 'up':#画眼
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 5, y * 20 + 5, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 12, y * 20 + 5, 3, 3))
        elif direction == 'down':
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 5, y * 20 + 12, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 12, y * 20 + 12, 3, 3))
        elif direction == 'left':
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 5, y * 20 + 5, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 5, y * 20 + 12, 3, 3))
        elif direction == 'right':
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 12, y * 20 + 5, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 12, y * 20 + 12, 3, 3))
    def body(x, y):#画蛇身
        pygame.draw.rect(sceen, (170, 170, 170), (x * 20 + 1, y * 20 + 1, 18, 18), 0)
    def food(x, y):#画食物
        pygame.draw.circle(sceen, (170, 170, 170), (x * 20 + 10, y * 20 + 10), 8, 0)
def mobile(direction):#移动整条蛇
    global snaks, revious#revious变量:用来存储那段“消失”的坐标,可以说是备份,或者电脑系统里的“回收站”
    if direction == 'up':
        snaks.insert(0, (snaks[0][0], snaks[0][1] - 1))
        revious = snaks.pop()
    elif direction == 'down':
        snaks.insert(0, (snaks[0][0], snaks[0][1] + 1))
        revious = snaks.pop()
    elif direction == 'left':
        snaks.insert(0, (snaks[0][0] - 1, snaks[0][1]))
        revious = snaks.pop()
    elif direction == 'right':
        snaks.insert(0, (snaks[0][0] + 1, snaks[0][1]))
        revious = snaks.pop()

技术专区

    类:创建一个柜子,里面可以存放变量、函数、……,还可以用来创建对象。

    pop:pop([i])删除并返回第i项,i的默认值为列表的长度-1

以及更多变量:

snaks = [(10, 9)]#蛇的坐标们 #第一项是头,然后是身子们
food = (10, 9)#食物的坐标
while food in snaks:#避免食物和蛇重合
    food = (random.randint(0, 19), random.randint(0, 19))
direction = 'left'#方向,用来画头和移动
score = 0#得分

接下来,我们的程序即将开始——

time.sleep(1)
pygame.display.set_caption('3')
time.sleep(1)
pygame.display.set_caption('2')
time.sleep(1)
pygame.display.set_caption('1')
time.sleep(1)
pygame.display.set_caption('Go!')
time.sleep(1)

正式开始!

while True:
    #接下来的代码均在while循环内

首先是每个程序必要的环节:如果事件里有“退出”,直接结束!

for event in pygame.event.get():
    if event == pygame.QUIT:
        sys.exit()

#另一种格式是这样的:

继续运行 = True
while 继续运行:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            继续运行 = False

刷新屏幕,以进行下一轮画图:

sceen.fill((255, 255, 255))

(有没有发现跟活该背景色和这个是一样的?嘿嘿……)

接着移动,调用我们之前定义的函数:

mobile(direction)

检查一下是否吃到了食物,也就是舌头的坐标是否和食物的相等。然后增加得分、刷新,还要将刚才“弄掉的”身子补上去:

if food == snaks[0]:
    score += 1
    snaks.append(revious)
    while food in snaks:
        food = (random.randint(0, 19), random.randint(0, 19))
    draw.food(food[0], food[1])

接着画头、画身子:

draw.hand(snaks[0][0], snaks[0][1], direction)
for body in snaks[1:]:
    draw.body(body[0], body[1])

当然,别忘了显示画出的图形,否则……

pygame.display.flip()

接着,判断一下你喵了没:

if snaks[0] in snaks[1:]:
    pygame.display.set_caption('你撞到了自己,失败了。')
    time.sleep(1)
    pygame.display.set_caption('本次游戏你得到了' + str(score) + '分。')
    time.sleep(1)
    sys.exit()
if snaks[0][0] < 0 or snaks[0][0] > 19 or snaks[0][1] < 0 or snaks[0][1] > 19:
    pygame.display.set_caption('你撞到了墙,失败了。')
    time.sleep(1)
    pygame.display.set_caption('本次游戏你得到了' + str(score) + '分。')
    time.sleep(1)
    sys.exit()

接着,是判断按键:

if get_pressed()[pygame.K_UP]:
    direction = 'up'
if get_pressed()[pygame.K_DOWN]:
    direction = 'down'
if get_pressed()[pygame.K_LEFT]:
    direction = 'left'
if get_pressed()[pygame.K_RIGHT]:
    direction = 'right'

(记住,千万不要只点一下就完事了,按住!因为某种不可抗力因素…………)

刷新一下显示在标题上的得分:

pygame.display.set_caption('得分:' + str(score))

以及等待一会会。

time.sleep(0.2)

大功告成!

emotion_雷电猴_哇噻

now,让我分享一下源码!

import pygame, sys, time, random
from pygame.key import *

pygame.init()
sceen = pygame.display.set_mode((400, 400))
sceen.fi喵))
pygame.display.flip()
pygame.display.set_caption('经典贪吃蛇游戏即将开始')

class draw:#用来画画的类 #20×20像素为一个格子
    def hand(x, y, direction):#画头
        pygame.draw.rect(sceen, (85, 85, 85), (x * 20 + 1, y * 20 + 1, 18, 18), 0)#画头
        if direction == 'up':#画眼
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 5, y * 20 + 5, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 12, y * 20 + 5, 3, 3))
        elif direction == 'down':
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 5, y * 20 + 12, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 12, y * 20 + 12, 3, 3))
        elif direction == 'left':
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 5, y * 20 + 5, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 5, y * 20 + 12, 3, 3))
        elif direction == 'right':
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 12, y * 20 + 5, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 12, y * 20 + 12, 3, 3))
    def body(x, y):#画蛇身
        pygame.draw.rect(sceen, (170, 170, 170), (x * 20 + 1, y * 20 + 1, 18, 18), 0)
    def food(x, y):#画食物
        pygame.draw.circle(sceen, (170, 170, 170), (x * 20 + 10, y * 20 + 10), 8, 0)
def mobile(direction):#移动整条蛇
    global snaks, revious#revious变量:用来存储那段“消失”的坐标,可以说是备份,或者电脑系统里的“回收站”
    if direction == 'up':
        snaks.insert(0, (snaks[0][0], snaks[0][1] - 1))
        revious = snaks.pop()
    elif direction == 'down':
        snaks.insert(0, (snaks[0][0], snaks[0][1] + 1))
        revious = snaks.pop()
    elif direction == 'left':
        snaks.insert(0, (snaks[0][0] - 1, snaks[0][1]))
        revious = snaks.pop()
    elif direction == 'right':
        snaks.insert(0, (snaks[0][0] + 1, snaks[0][1]))
        revious = snaks.pop()
snaks = [(10, 9)]#蛇的坐标们 #第一项是头,然后是身子们
food = (10, 9)#食物的坐标
while food in snaks:#避免食物和蛇重合
    food = (random.randint(0, 19), random.randint(0, 19))
direction = 'left'#方向,用来画头和移动
score = 0#得分

time.sleep(1)
pygame.display.set_caption('3')
time.sleep(1)
pygame.display.set_caption('2')
time.sleep(1)
pygame.display.set_caption('1')
time.sleep(1)
pygame.display.set_caption('Go!')
time.sleep(1)
while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit()
    sceen.fi喵))
    mobile(direction)
    if food == snaks[0]:
        score += 1
        snaks.append(revious)
        while food in snaks:
            food = (random.randint(0, 19), random.randint(0, 19))
    draw.food(food[0], food[1])
    draw.hand(snaks[0][0], snaks[0][1], direction)
    for body in snaks[1:]:
        draw.body(body[0], body[1])
    pygame.display.flip()
    if snaks[0] in snaks[1:]:
        pygame.display.set_caption('你撞到了自己,失败了。')
        time.sleep(1)
        pygame.display.set_caption('本次游戏你得到了' + str(score) + '分。')
        time.sleep(1)
        sys.exit()
    if snaks[0][0] < 0 or snaks[0][0] > 19 or snaks[0][1] < 0 or snaks[0][1] > 19:
        pygame.display.set_caption('你撞到了墙,失败了。')
        time.sleep(1)
        pygame.display.set_caption('本次游戏你得到了' + str(score) + '分。')
        time.sleep(1)
        sys.exit()
    if get_pressed()[pygame.K_UP]:
        direction = 'up'
    if get_pressed()[pygame.K_DOWN]:
        direction = 'down'
    if get_pressed()[pygame.K_LEFT]:
        direction = 'left'
    if get_pressed()[pygame.K_RIGHT]:
        direction = 'right'
    pygame.display.set_caption('得分:' + str(score))
    time.sleep(0.2)


回复

上一页1 页 / 共 1下一页
程序侯程序侯

赠送一个链接:

https://www.jianshu.com/p/5cc2a005961b

点赞0


评论


程序侯程序侯

2.0版本新出炉了!

import pygame, sys, time, random
from pygame.key import *

pygame.init()
sceen = pygame.display.set_mode((500, 500))
sce喵5, 255))
pygame.draw.rect(sceen, (0, 0, 0), (0, 0, 500, 500), 100)
pygame.display.flip()
pygame.display.set_caption('经典贪吃蛇游戏即将开始')

class draw:#用来画画的类 #20×20像素为一个格子
    def hand(x, y, direction):#画头
        pygame.draw.rect(sceen, (85, 85, 85), (x * 20 + 51, y * 20 + 51, 18, 18), 0)#画头
        if direction == 'up':#画眼
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 55, y * 20 + 55, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 62, y * 20 + 55, 3, 3))
        elif direction == 'down':
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 55, y * 20 + 62, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 62, y * 20 + 62, 3, 3))
        elif direction == 'left':
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 55, y * 20 + 55, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 55, y * 20 + 62, 3, 3))
        elif direction == 'right':
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 62, y * 20 + 55, 3, 3))
            pygame.draw.rect(sceen, (255, 255, 255), (x * 20 + 62, y * 20 + 62, 3, 3))
    def body(x, y):#画蛇身
        pygame.draw.rect(sceen, (170, 170, 170), (x * 20 + 51, y * 20 + 51, 18, 18), 0)
    def food(x, y):#画食物
        pygame.draw.circle(sceen, (170, 170, 170), (x * 20 + 60, y * 20 + 60), 8, 0)
def mobile(direction):#移动整条蛇
    global snaks, revious#revious变量:用来存储那段“消失”的坐标,可以说是备份,或者电脑系统里的“回收站”
    if direction == 'up':
        snaks.insert(0, (snaks[0][0], snaks[0][1] - 1))
        revious = snaks.pop()
    elif direction == 'down':
        snaks.insert(0, (snaks[0][0], snaks[0][1] + 1))
        revious = snaks.pop()
    elif direction == 'left':
        snaks.insert(0, (snaks[0][0] - 1, snaks[0][1]))
        revious = snaks.pop()
    elif direction == 'right':
        snaks.insert(0, (snaks[0][0] + 1, snaks[0][1]))
        revious = snaks.pop()
snaks = [(10, 9)]#蛇的坐标们 #第一项是头,然后是身子们
food = (10, 9)#食物的坐标
while food in snaks:#避免食物和蛇重合
    food = (random.randint(0, 19), random.randint(0, 19))
direction = 'left'#方向,用来画头和移动
score = 0#得分

time.sleep(1)
pygame.display.set_caption('3')
time.sleep(1)
pygame.display.set_caption('2')
time.sleep(1)
pygame.display.set_caption('1')
time.sleep(1)
pygame.display.set_caption('Go!')
time.sleep(1)
while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit()
    sce喵5, 255))
    pygame.draw.rect(sceen, (0, 0, 0), (0, 0, 500, 500), 100)
    mobile(direction)
    if food == snaks[0]:
        score += 1
        snaks.append(revious)
        while food in snaks:
            food = (random.randint(0, 19), random.randint(0, 19))
    draw.food(food[0], food[1])
    draw.hand(snaks[0][0], snaks[0][1], direction)
    for body in snaks[1:]:
        draw.body(body[0], body[1])
    pygame.display.flip()
    if snaks[0] in snaks[1:]:
        pygame.display.set_caption('你撞到了自己,失败了。')
        time.sleep(1)
        pygame.display.set_caption('本次游戏你得到了' + str(score) + '分。')
        time.sleep(1)
        sys.exit()
    if snaks[0][0] < 0 or snaks[0][0] > 19 or snaks[0][1] < 0 or snaks[0][1] > 19:
        pygame.display.set_caption('你撞到了墙,失败了。')
        time.sleep(1)
        pygame.display.set_caption('本次游戏你得到了' + str(score) + '分。')
        time.sleep(1)
        sys.exit()
    if get_pressed()[pygame.K_UP]:
        direction = 'up'
    if get_pressed()[pygame.K_DOWN]:
        direction = 'down'
    if get_pressed()[pygame.K_LEFT]:
        direction = 'left'
    if get_pressed()[pygame.K_RIGHT]:
        direction = 'right'
    pygame.display.set_caption('得分:' + str(score))
    time.sleep(0.2)

点赞0


评论


BEST一杯猹BEST一杯猹

nbnbnbnb

点赞0


评论


FHR丨萌新滑稽FHR丨萌新滑稽

emotion_编程猫_点赞

点赞0


评论


_王子佳__王子佳_

你会pygame了?

点赞0


评论