一直想用pygame做一个小游戏的,可是因为拖延症的缘故一直没有动,结果那天看到了一个12岁的国际友人小盆友用pygame做的一款塔防游戏,突然感觉已经落后超级远了,所以心血来潮做小游戏了。高中陪伴我的游戏就是手机里的贪吃蛇,还记得我和老尹童鞋比拼分数的场景,所以就从贪吃蛇开始吧。
好吧,因为大学老师教导我们,用面向对象的语言写程序的时候,首先考虑建立类,于是乎,我就考虑建立了snake类和food类两个,但是我不准备在我的程序里添加图片,所以这两个类最终沦为贪吃蛇和食物它们各自的位置变换的实现了。
class snake: def __init__(self): """ init the snake """ self.poslist = [[10,10]] def position(self): """ return the all of the snake's point """ return self.poslist def gowhere(self,where): """ change the snake's point to control the snake's moving direction """ count = len(self.poslist) pos = count-1 while pos > 0: self.poslist[pos] = copy.deepcopy(self.poslist[pos-1]) pos -= 1 if where is 'U': self.poslist[pos][1] -= 10 if self.poslist[pos][1] < 0: self.poslist[pos][1] = 500 if where is 'D': self.poslist[pos][1] += 10 if self.poslist[pos][1] > 500: self.poslist[pos][1] = 0 if where is 'L': self.poslist[pos][0] -= 10 if self.poslist[pos][0] < 0: self.poslist[pos][0] = 500 if where is 'R': self.poslist[pos][0] += 10 if self.poslist[pos][0] > 500: self.poslist[pos][0] = 0 def eatfood(self,foodpoint): """ eat the food and add point to snake """ self.poslist.append(foodpoint)
在gowhere函数中,之所以与500比较大小,是因为我定义的窗口大小为宽500,高500
class food: def __init__(self): """ init the food's point """ self.x = random.randint(10,490) self.y = random.randint(10,490) def display(self): """ init the food's point and return the point """ self.x = random.randint(10,490) self.y = random.randint(10,490) return self.position() def position(self): """ return the food's point """ return [self.x,self.y]
food 的位置是使用随即函数随即出来的
def main(): moveup = False movedown = False moveleft = False moveright = True pygame.init() clock = pygame.time.Clock() width = 500 height = 500 screen = pygame.display.set_mode([width,height]) #1 restart = True while restart: sk = snake() fd = food() screentitle = pygame.display.set_caption("eat snake") #2 sk.gowhere('R') running = True while running: # fill the background is white screen.fill([255,255,255]) #3
程序开始主要是做了一些初始化的工作,moveup/movedown/moveleft/moveright这四个变量使用来标注贪吃蛇的运动方向的,#1标注的那句是初始化显示窗口的(因此所有pygame编写的程序中,这句话有且只能调用一次),#2标注的那句是初始化标题栏显示标题的,#3那句是用来填充整个背景为白色
for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit(0) # judge the down key if event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: moveup = True movedown = False moveleft = False moveright = False if event.key == pygame.K_DOWN: moveup = False movedown = True moveleft = False moveright = False if event.key == pygame.K_LEFT: moveup = False movedown = False moveleft = True moveright = False if event.key == pygame.K_RIGHT: moveup = False movedown = False moveleft = False moveright = True
当按下方向键时,设置相应的方向
# where the snake goes time_pass = clock.tick(40) if moveup: sk.gowhere('U') if movedown: sk.gowhere('D') if moveleft: sk.gowhere('L') if moveright: sk.gowhere('R')
设置视频帧数并设置贪吃蛇的移动方向
# draw the food poslist = sk.position() foodpoint = fd.position() fdrect = pygame.draw.circle(screen,[255,0,0],foodpoint,15,0) # draw the snafe snaferect = [] for pos in poslist: snaferect.append(pygame.draw.circle(screen,[255,0,0],pos,5,0))
在界面上画上食物和贪吃蛇,其中fdrect和snaferect的存储是后面碰撞检测需要用到的
# crash test if the snake eat food if fdrect.collidepoint(pos): foodpoint = fd.display() sk.eatfood(foodpoint) fdrect = pygame.draw.circle(screen,[255,0,0],foodpoint,15,0) break # crash test if the snake crash itsself headrect = snaferect[0] count = len(snaferect) while count > 1: if headrect.colliderect(snaferect[count-1]): running = False count -= 1 pygame.display.update()
碰撞检测贪吃蛇是否吃到了食物,以及是否撞到了自己,pygame.display.update()是更新整个界面的意思,前面的画图只是画到了区域里,但是没有更新到窗口,需要此句将其更新到显示窗口
# game over background pygame.font.init() screen.fill([100,0,0]) font = pygame.font.Font(None,48) text = font.render("Game Over !!!",True,(255,0,0)) textRect = text.get_rect() textRect.centerx = screen.get_rect().centerx textRect.centery = screen.get_rect().centery + 24 screen.blit(text,textRect) # keydown r restart,keydown n exit while 1: event = pygame.event.poll() if event.type == pygame.QUIT: pygame.quit() exit(0) if event.type == pygame.KEYDOWN: if event.key == pygame.K_r: restart = True del sk del fd break if event.key == pygame.K_n: restart = False break pygame.display.update()
当输了之后,界面上显示game over字样,此时按下“n”退出程序,按下“r"重新开始
自己写的这个贪吃蛇与那位国际友人写的程序比较一下,发现我因为懒所以没有添加图片和音乐,纯是画的图,所以不需要图片的各种操作,我觉得那个图片跟着鼠标转动的效果挺有意思的,以后再做别的游戏的时候再添加吧
源码:http://download.csdn.net/detail/vhghhd/6035283,嘿嘿