贪吃蛇Python实现(需安装pygame包)(丐版)

之前Python课上老师提了一下可以闲的没事写个贪吃蛇,无所事事的w某就~~无所事事的~~ 写了个~~无所事事的~~ 贪吃蛇(雾)

直接进入正题
先分析一下一个贪吃蛇游戏的基本流程:

 - 蛇破壳出生  
- 出现食物 
- 蛇向食物爬 
- 蛇吃到食物——身体变长
-  蛇闯入其他野兽的地盘/因为太饿吃了自己(die)
- 蛇:我又回来了!!!    

翻译一下就是

- 游戏初始设定
- 食物刷新
- 蛇身体参数的改变
- 蛇身体参数的增加
- 蛇是否死亡的判定
- 初始化/重启游戏

因为个人比较懒,没有增加除游戏界面外的其他界面,一切提示都是最简单的print输出,~~能玩就行(不是)~~ ,所以简陋之处还请各位客官见谅

步骤1:安装pygame包

[pygame官网](https://www.pygame.org/)有详细的安装说明
 这里简单列出Windows下pygame的安装

 py -m pip install -U pygame --user

步骤2:一些需要的函数

列出部分需要的函数
各函数详细使用方法可自行前往官网观摩,QWQ

pygame.init()                    #pygame初始化
screen = pygame.display.set_mode((800,600))    #设定界面大小,个人设定的800*600大小,可根据自身需要调整
pygame.display.set_caption('Snake')        #界面名称设定,根据个人喜好设定
fpsClock = pygame.time.Clock()            #这个函数是用来控制游戏FPS的,在此程序中起到控制游戏速度的作用,调试过程中还写了一个用来控制游戏速度的函数,下文中会一并给出

**步骤3:rua小蛇**

 我们先来在屏幕上rua一条小蛇,初始化这条蛇的长度为3,出现在屏幕中央
身体数据与预设颜色:
 

Snake_body_x = [360,380,400]
Snake_body_y = [300,300,300]
Snake_head = [400,300]
Snake_len = 3
BLACK = (0,0,0)
WHITE = (255,255,255)
CYAN = (0,255,255)

rua:

screen.fill(BLACK)        #填充屏幕
for i in range(Snake_len):    #rua小蛇(依次在小蛇身体坐标处画出矩形)
    pygame.draw.rect(screen,WHITE,Snake_body_x[i],Snake_body_y[i],19,19])

为什么画的长和宽是19呢?因为E某把界面在脑子里分成了20x20的小格,如果绘画时将20x20填充满我觉得不太好看,所以留了个小边将蛇的每部分身体勾勒出来,根据个人喜好本程序中你可以填1~20任一数字

此时一只小蛇已经rua好了,接下来我们来为小蛇创造食物

-----分割线------
由于学业繁忙~~(划掉)~~ 下面直接给出源码(未来没准更不更新,先欠着,逃~XD):

#by wzh
import pygame,sys,random,time
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('Snake')

FPS = 5
fpsClock = pygame.time.Clock()

Snake_body_x = [360,380,400]
Snake_body_y = [300,300,300]
Snake_head = [400,300]
Snake_len = 3

direction = 'RIGHT'

BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
CYAN = (0,255,255)

ram_x = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39]
ram_y = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]
food_x = random.choice(ram_x)
food_y = random.choice(ram_y)
food_flag = 1

score = 0
highest_score = 0

#随机生成食物函数1
def New_food():
    global food_flag
    global food_x
    global food_y
    if food_flag:
        pass
    else:
        ticks = time.time()
        print("ticks:",ticks)
        food_flag = 1
        food_x = random.choice(ram_x)
        food_y = random.choice(ram_y)
        #print(food_x,food_y)

#随机生成食物函数2
def New_food2():
    global food_flag
    global food_x
    global food_y
    global Snake_len
    global Snake_body_x
    global Snake_body_y

    f = 0
    
    if food_flag:
        pass
    else:
        food_flag = 1
        food_x = random.choice(ram_x)
        food_y = random.choice(ram_y)
        #print(food_x,food_y)
    for i in range(Snake_len):
        if(food_x == Snake_body_x[i] and food_y == Snake_body_y[i]):
            f = 1
            break
    if f:
        food_flag = 0
        New_food2()
        print("re_food")

#重置参数函数
def Re():
    global food_x
    global food_y
    global food_flag
    global Snake_body_x
    global Snake_body_y
    global Snake_head
    global Snake_len
    global direction
    global score

    food_x = random.choice(ram_x)
    food_y = random.choice(ram_y)
    food_flag = 1
    Snake_body_x = [360,380,400]
    Snake_body_y = [300,300,300]
    Snake_head = [400,300]
    Snake_len = 3
    direction = 'RIGHT'
    score = 0

#死亡与记录与重启函数
def GG():
    global score
    global highest_score
    print("GAME OVER")
    print("Your score is ",score," !")
    if highest_score < score:
        highest_score = score
        print("Congratulations,you break the record!")
    print("press 'R' to restart")
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if(event.key ==K_r):
                    print("Restart")
                    Re()
                    main()

#备用帧数控制函数
def Clock_contral(m):
    tick_1 = time.time()
    tick_2 = time.time()

    print("tick_1:",tick_1)
    
    while tick_2 - tick_1 < 0.2:
        tick_2 = time.time()

    print("tick_2:",tick_2)
    
    return 1;

#主函数
def main():
    global food_x
    global food_y
    global food_flag
    global Snake_body_x
    global Snake_body_y
    global Snake_head
    global Snake_len
    global direction
    global score
    while True:
        #time_contral = Clock_contral(1)
        #键盘字符获取
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == KEYDOWN:
                if(event.key == K_UP or event.key ==K_w) and direction != 'DOWN':
                    print("UP")
                    direction = 'UP'
                    break
                if(event.key == K_DOWN or event.key ==K_s) and direction != 'UP':
                    print("DOWN")
                    direction = 'DOWN'
                    break
                if(event.key == K_LEFT or event.key ==K_a) and direction != 'RIGHT':
                    print("LEFT")
                    direction = 'LEFT'
                    break
                if(event.key == K_RIGHT or event.key ==K_d) and direction != 'LEFT':
                    print("RIGHT")
                    direction = 'RIGHT'
                    break
        
        #print("1")
                
        #头部坐标位移
        if direction == 'UP':
            Snake_head[1] = Snake_head[1]-20
        elif direction == 'DOWN':
            Snake_head[1] = Snake_head[1]+20
        elif direction == 'LEFT':
            Snake_head[0] = Snake_head[0]-20
        elif direction == 'RIGHT':
            Snake_head[0] = Snake_head[0]+20

        #print("2")
        
        #身体及边界触碰判定
        if(Snake_head[0] >780 or Snake_head[0] < 0 or Snake_head[1] > 580 or Snake_head[1] < 0):
            print("OUT RANGE")
            GG()
        
        for i in range(Snake_len):
            if i == 0:
                pass
            elif(Snake_head[0] == Snake_body_x[i] and Snake_head[1] == Snake_body_y[i]):
                print("YOU EAT YOURSELF")
                GG()

        #print("3")

        #身体列表更新判定
        if (Snake_head[0] == food_x*20 and Snake_head[1] == food_y*20):
            food_flag = 0
            score = score +1
            New_food2()
            New_x = Snake_head[0]
            New_y = Snake_head[1]
            Snake_body_x = Snake_body_x + [New_x]
            Snake_body_y = Snake_body_y + [New_y]
            Snake_len = Snake_len + 1
        else:
            New_x = Snake_head[0]
            New_y = Snake_head[1]
            Snake_body_x = Snake_body_x + [New_x]
            Snake_body_y = Snake_body_y + [New_y]
            del Snake_body_x[0]
            del Snake_body_y[0]

        #New_food()

        #调试用数值记录
        print("Snake_head:",Snake_head[0]//20,Snake_head[1]//20)
        print("food flag:",food_flag)
        print("food:",food_x," ",food_y)

        #更新屏幕
        screen.fill(BLACK)
        if food_flag:
            pygame.draw.rect(screen,CYAN,[food_x*20,food_y*20,19,19])
        for i in range(Snake_len):
            pygame.draw.rect(screen,WHITE,[Snake_body_x[i],Snake_body_y[i],19,19])
            
        #pygame.draw.rect(screen,WHITE,[Snake_head[0],Snake_head[1],19,19])
        pygame.display.update()
        fpsClock.tick(FPS)
        #time.sleep(0.15)
main()

 

匿名

发表评论

匿名网友