您的位置:首页 > 编程语言 > Python开发

python实现贪吃蛇(1)

2013-06-05 00:12 363 查看
这几天学了下pygame,终于要做人生中第一个游戏了⊙﹏⊙

贪吃蛇的算法还是比较简单的,蛇的移动我是通过不停添加一个head方块,然后判断应该加到蛇头的哪个方向,加完后删掉蛇尾就行了,如果吃到食物就不删蛇尾。

只是一个贪吃蛇只需要70行代码左右就可以了,后来又加了计分,失败后重新游戏,暂停功能····结果现在代码乱成渣了。。

重新游戏部分肯定有更好的方法,我写的太乱了。。求大神指教。由于没用网格,判断吃到的时候是用范围判断的,有时候有些偏差···

代码:

#-*- coding: utf-8 -*-
import pygame, sys, random, time
from pygame.locals import *

pygame.init()
font = pygame.font.Font(u"c:\\windows\\fonts\\MSYH.ttf",30)
mainClock = pygame.time.Clock()
wsurface = pygame.display.set_mode((800,600),0,32)
pygame.display.set_caption("My_Snake~")

Len = 20
snakeRect = []
for i in range(10,13):
snakeRect.append(pygame.Rect(i * (Len) , 50 , Len, Len))
food = pygame.Rect(10 * (Len), 10 * (Len), Len, Len)
ml    = False
mr    = True
mu    = False
md    = False
score = 0
black = (0, 0, 0)
green = (0, 255, 0)
white = (255, 255, 255)
global FPSCLOCK
FPSCLOCK = pygame.time.Clock()
#####################################################
def judge():
if snakeRect[0].left - 15 <= food.left <= snakeRect[0].left + 15 and snakeRect[0].top - 15 <= food.top <= snakeRect[0].top + 15:
return True
def judge2(a, b):
if a.left - 15 <= b.left <= a.left + 15 and a.top - 15 <= b.top <= a.top + 15:
return True
def checkForKeyPress():
#checkForQuit()
for event in pygame.event.get([KEYDOWN, KEYUP]):
if event.type == KEYUP:
continue
return event.key
return None
#######################################################
flagg = True
speed = 8
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.QUIT()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_r:
snakeRect = []
for i in range(10,13):
snakeRect.append(pygame.Rect(i * (Len) , 50 , Len, Len))
food = pygame.Rect(10 * (Len), 10 * (Len), Len, Len)
ml    = False
mr    = True
mu    = False
md    = False
score = 0
flagg = True
speed = 8
if event.key == K_p:
wsurface.fill(black)
text_surface1 = font.render('Pause!' , True, (0, 0, 255))
wsurface.blit(text_surface1, (10, 50))
while checkForKeyPress() == None:
pygame.display.update()
FPSCLOCK.tick()
if event.key == K_LEFT and mr == False:
ml = True
mr = False
mu = False
md = False
if event.key == K_RIGHT and ml == False:
ml = False
mr = True
mu = False
md = False
if event.key == K_UP and md == False:
ml = False
mr = False
mu = True
md = False
if event.key == K_DOWN and mu == False:
ml = False
mr = False
mu = False
md = True
head = pygame.Rect(snakeRect[0].left,snakeRect[0].top,snakeRect[0].width,snakeRect[0].height)
if flagg == False:
continue
if ml == True:
head.right = head.left - 1
if mr == True:
head.left = head.right + 1
if mu == True:
head.bottom = head.top - 1
if md == True:
head.top = head.bottom + 1
snakeRect.insert(0, head)
#判断失败和重新游戏
if head.right < 0 or head.left > 800 or head.bottom < 0 or head.top > 600 or snakeRect[0] in snakeRect[1:]:
wsurface.fill(white)
text_surface2 = font.render('Press R to restart!' , True, (0, 0, 255))
wsurface.blit(text_surface2, (50, 80))
pygame.display.update()
while checkForKeyPress() == None:
pygame.display.update()
FPSCLOCK.tick()
break
flagg = False
continue
flagg = True
if judge():
score = score + 10
speed = speed + 1
while True:
flag = True
food.left = random.randrange(10,800)
food.top = random.randrange(10,600)
for temp in snakeRect:
if judge2(food, temp):
flag = False
if flag == True:
break
else:
snakeRect.pop()
wsurface.fill(black)
for i in range(len(snakeRect)):
pygame.draw.rect(wsurface,green,snakeRect[i])
pygame.draw.rect(wsurface,white,food)
text_surface = font.render(u"分数: " + str(score), True, (0, 0, 255))
wsurface.blit(text_surface, (10, 50))
pygame.display.update()
mainClock.tick(speed)


截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: