科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道应用软件 Python 小试(二)

Python 小试(二)

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

这是可贪食蛇的简化版游戏,用的也是Pygame接口,我也不会写过多的介绍, 有兴趣的朋友可以看一下,另外还有一些资源文件,单下面的程序运行不了 游戏,请先安装pygame,然后下载下面的包看看,里面包括程序和资源。

作者:ColoR_Pig 来源:CSDN 2008年5月26日

关键字: 小试 python 软件

  • 评论
  • 分享微博
  • 分享邮件
 

这是可贪食蛇的简化版游戏,用的也是Pygame接口,我也不会写过多的介绍,
有兴趣的朋友可以看一下,另外还有一些资源文件,单下面的程序运行不了
游戏,请先安装pygame,然后下载下面的包看看,里面包括程序和资源。

http://sunyueming.91x.net/snake.tar

#filename snake.py
import pygame
from pygame.locals import *
from random import random

bg = 0,0,0
RH={(0,-1):0,(0,1):180,(-1,0):90,(1,0):-90}
RB={(1,1):0,(1,-1):90,(-1,-1):180,(-1,1):-90}

def random_place():
    return int(random()*30),int(random()*20)

def make_display():
    screen.blit(bG,(0,0))

    screen.blit(pygame.transform.rotate(heaD, RH[direction]),(snake[-1][0]*10,snake[-1][1]*10))

    for n in range(1,len(snake)-1):
        d1X,d1Y=snake[n+1][0]-snake[n][0],snake[n+1][1]-snake[n][1]
        d2X,d2Y=snake[n-1][0]-snake[n][0],snake[n-1][1]-snake[n][1]
        T=d1X+d2X,d1Y+d2Y
        if not T[0]:
            screen.blit(pygame.transform.rotate(bodY, d1Y*90),(snake[n][0]*10,snake[n][1]*10))
        else:
            screen.blit(pygame.transform.rotate(turN, RB[T]),(snake[n][0]*10,snake[n][1]*10))

    tail_direction=snake[0][0]-snake[1][0],snake[0][1]-snake[1][1]
    screen.blit(pygame.transform.rotate(taiL, RH[tail_direction]),(snake[0][0]*10,snake[0][1]*10))
    if scale[0] < 15:
        scale[0]=scale[0] + 2
        screen.blit(holE,(food_where[0]*10-5,food_where[1]*10-5))
    screen.blit(pygame.transform.scale(fooD,(scale[0]+1,scale[0])),(food_where[0]*10+12-scale[0],food_where[1]*10+11-scale[0]))
    pygame.display.update()

pygame.init()
screen=pygame.display.set_mode((300,200),NOFRAME|HWSURFACE|DOUBLEBUF,0)
pygame.display.set_caption('贪吃的蛇 sunyueming 2004-04-17')
bG=pygame.image.load('bg.gif').convert()
heaD=pygame.image.load('head.gif').convert()
taiL=pygame.image.load('tail.gif').convert()
bodY=pygame.image.load('body.gif').convert()
turN=pygame.image.load('turn.gif').convert()
fooD=pygame.image.load('food.gif').convert()
holE=pygame.image.load('hole.gif').convert()
oveR=pygame.image.load('over.gif').convert()
mixer=pygame.mixer
mixer.init(11025)
s1=mixer.Sound(('die.wav'))
s2=mixer.Sound(('eat.wav'))
s4=mixer.Sound(('start.wav'))

done = 0

while not done:
    snake = [(15,3),(15,4),(15,5)]
    score = 3
    direction = 0,1
    snake_alive = 1
    s4.play()

    while snake_alive and not done:
        see_food = 0

        while not see_food:
            food_where = random_place()
            see_food = 1
            for body_part in snake:
                if body_part == food_where:
                    see_food=0

        scale = [1]                   
        while see_food and not done:
           
            for e in pygame.event.get():
                if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
                    done = 1
                    break
                if e.type == KEYDOWN and e.key == K_UP and direction <> (0,1):
                    direction = 0,-1
                    break
                if e.type == KEYDOWN and e.key == K_LEFT and direction <> (1,0):
                    direction = -1,0
                    break
                if e.type == KEYDOWN and e.key == K_RIGHT and direction <> (-1,0):
                    direction = 1,0
                    break
                if e.type == KEYDOWN and e.key == K_DOWN and direction <> (0,-1):
                    direction = 0,1
                    break

            make_display()
            pygame.time.delay(120)

            ahead = snake[-1][0]+direction[0],snake[-1][1]+direction[1]

            if ahead == food_where:
                snake.append(ahead)
                score = score + 1
                s2.play()
                break

            if ahead[0] > 29 or ahead[0] < 0 or ahead[1] > 19 or ahead[1] < 0:
                snake_alive = 0
            for body_part in snake:
                if body_part == ahead:
                    snake_alive = 0
            if snake_alive == 0:
                s1.play()
                break
            snake.remove(snake[0])
            snake.append(ahead)
    if done:
        break
    screen.blit(oveR,(40,80))
    pygame.display.update()
    press_a_key = 0
    while not press_a_key:
        for e in pygame.event.get():
            if e.type == KEYDOWN and e.key == K_SPACE:
                press_a_key = 1
           

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章