Python 小试(二)

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

这是可贪食蛇的简化版游戏,用的也是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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航