不管学什么语言都要有学习的动力,凭着一腔的热情可能支持一段时间,最终还是要靠兴趣,兴趣是学习的最大动力:
看到有人分享的贪吃蛇小游戏源码,忍不住码了下,同时调试了下,成功运行,后续打算做持续改进。
1,贪吃蛇的逻辑图很清晰且简单(摘自图中博客)
2,准备条件:
python开发环境,安装pygame库,pip install pygame
3,上代码:
1),包含的库:
#************************调用的库*************************** import pygame import sys import time import random from pygame.locals import * #***********************************************************
2),初始化
#************************初始化****************************** pygame.init() fpsclock = pygame.time.Clock() #创建pygame显示层 playSurface = pygame.display.set_mode((640, 480)) #定义标题 pygame.display.set_caption('Snake Go') #加载资源图片!!!注意这里的路径为绝对路径,且注意用的"/"而不是"\",后者容易形成转义字符,支持jpg,png等多种图片格式 image = pygame.image.load(r'C:/Users/Administrator/Desktop/贪吃蛇/game.png') #设置图标 pygame.display.set_icon(image) #************************************************************
3),定义一些用到的颜色变量
#******************定义颜色变量******************************** redColour = pygame.Color(255,0,0) blackColour = pygame.Color(0,0,0) whiteColour = pygame.Color(255,255,255) greyColour = pygame.Color(150,150,150) LightGrey = pygame.Color(220,220,220) #**************************************************************
4),定义游戏结束函数GAMEOVER
#*****************gameover****************************** def gameOver(playSurface, score): #显示GAME OVER并定义字体以及大小 gameOverFont = pygame.font.Font('arial.ttf',72) gameOverSurf = gameOverFont.render('Game Over',True,greyColour) gameOverRect = gameOverSurf.get_rect() gameOverRect.midtop = (320,125) playSurface.blit(gameOverSurf,gameOverRect) #显示分数并定义字体大小 scoreFont = pygame.font.Fond('arial.ttf', 48) scoreSurf = scoreFont.render('SCORE:' + str(score), True, greyColour) scoreRect = scoreSurf.get_rect() scoreRect.midtop = (320,225) playSurface.blit(scoreSurf, scoreRect) pygame.display.flip() #刷新显示界面 #休眠5秒后自动关闭 time.sleep(5) pygame.quit() sys.exit() #********************************************************
5),初始化贪吃蛇与树莓
#**************初始化贪吃蛇与树莓******************************************* snakePosition = [100,100] #蛇头的位置 snakeSegments =[[100,100],[80,100],[60,100]]#初始长度为3个单位 raspberryPosition = [300, 300] #树莓的位置 raspberrySpawned = 1 #树莓个数 direction = 'right' #初始方向 changeDirection = direction score = 0 #初始分数 #**************************************************************************
6),大循环,组织的不好
#*****键盘输入判断蛇运动*************************************** while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_RIGHT or event.key == ord('d'): changeDirection = 'right' if event.key == K_LEFT or event.key == ord('a'): changeDirection = 'left' if event.key == K_UP or event.key == ord('w'): changeDirection = 'up' if event.key == K_DOWN or event.key == ord('s'): changeDirection = 'down' if event.key == K_ESCAPE: pygame.event.post(pygame.event.Event(QUIT)) #**********由于贪吃蛇不能反向运动,需加入限制条件*********** if changeDirection == 'right' and not direction =='left': direction = changeDirection if changeDirection == 'left' and not direction =='right': direction = changeDirection if changeDirection == 'up' and not direction =='down': direction = changeDirection if changeDirection == 'down' and not direction =='up': direction = changeDirection #***接下来就是蛇头按照键盘方向进行转弯操作************ if direction == 'right': snakePosition[0] += 20 if direction == 'left': snakePosition[0] -= 20 if direction == 'up': snakePosition[1] -= 20 if direction == 'down': snakePosition[1] += 20 #将蛇头的位置加入列表之中 snakeSegments.insert(0,list(snakePosition)) #*************************判断是否吃到树莓************* if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]: raspberrySpawned = 0 print("test7") else: snakeSegments.pop() print("test20") #****当树莓数量为0时,重新生成树莓**************** if raspberrySpawned == 0: x = random.randrange(1,32) y = random.randrange(1,24) raspberryPosition = [int(x*20),int(y*20)] raspberrySpawned = 1 score += 1 print("test8") #****刷新显示层*************************** playSurface.fill(blackColour) for position in snakeSegments[1:]: pygame.draw.rect(playSurface, whiteColour, Rect(position[0], position[1], 20, 20)) print("test9") pygame.draw.rect(playSurface,LightGrey, Rect(snakePosition[0],snakePosition[1], 20, 20)) print("test15") pygame.draw.rect(playSurface,redColour, Rect(raspberryPosition[0],raspberryPosition[1], 20, 20)) print("test16") #刷新pygame显示层 pygame.display.flip() #*******判断是否死亡****************** if snakePosition[0] > 620 or snakePosition[0] < 0: print("test10") gameOver(playSurface, score) if snakePosition[0] > 620 or snakePosition[0] < 0: print("test11") gameOver(playSurface, score) for snakeBody in snakeSegments[1:]: if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]: print("test12") gameOver(playSurface, score) #********控制游戏进度,长度越长速度越快********** if len(snakeSegments) < 40: speed = 6 + len(snakeSegments) print("test13") else: speed = 16 fpsclock.tick(speed)
总结:程序架构组织的不好,因为程序比较简单,也没有去花心思组织,望批评指导
全部代码汇总一下:
#************************调用的库*************************** import pygame import sys import time import random from pygame.locals import * #*********************************************************** #************************初始化****************************** pygame.init() fpsclock = pygame.time.Clock() #创建pygame显示层 playSurface = pygame.display.set_mode((640, 480)) #定义标题 pygame.display.set_caption('Snake Go') #加载资源图片!!!注意这里的路径为绝对路径,且注意用的"/"而不是"\",后者容易形成转义字符,支持jpg,png等多种图片格式 image = pygame.image.load(r'C:/Users/Administrator/Desktop/贪吃蛇/game.png') #设置图标 pygame.display.set_icon(image) #************************************************************ #******************定义颜色变量******************************** redColour = pygame.Color(255,0,0) blackColour = pygame.Color(0,0,0) whiteColour = pygame.Color(255,255,255) greyColour = pygame.Color(150,150,150) LightGrey = pygame.Color(220,220,220) #************************************************************** #*****************gameover****************************** def gameOver(playSurface, score): #显示GAME OVER并定义字体以及大小 gameOverFont = pygame.font.Font('arial.ttf',72) gameOverSurf = gameOverFont.render('Game Over',True,greyColour) gameOverRect = gameOverSurf.get_rect() gameOverRect.midtop = (320,125) playSurface.blit(gameOverSurf,gameOverRect) #显示分数并定义字体大小 scoreFont = pygame.font.Fond('arial.ttf', 48) scoreSurf = scoreFont.render('SCORE:' + str(score), True, greyColour) scoreRect = scoreSurf.get_rect() scoreRect.midtop = (320,225) playSurface.blit(scoreSurf, scoreRect) pygame.display.flip() #刷新显示界面 #休眠5秒后自动关闭 time.sleep(5) pygame.quit() sys.exit() #******************************************************** #**************初始化贪吃蛇与树莓******************************************* snakePosition = [100,100] #蛇头的位置 snakeSegments =[[100,100],[80,100],[60,100]]#初始长度为3个单位 raspberryPosition = [300, 300] #树莓的位置 raspberrySpawned = 1 #树莓个数 direction = 'right' #初始方向 changeDirection = direction score = 0 #初始分数 #************************************************************************** #*****键盘输入判断蛇运动*************************************** while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_RIGHT or event.key == ord('d'): changeDirection = 'right' if event.key == K_LEFT or event.key == ord('a'): changeDirection = 'left' if event.key == K_UP or event.key == ord('w'): changeDirection = 'up' if event.key == K_DOWN or event.key == ord('s'): changeDirection = 'down' if event.key == K_ESCAPE: pygame.event.post(pygame.event.Event(QUIT)) #**********由于贪吃蛇不能反向运动,需加入限制条件*********** if changeDirection == 'right' and not direction =='left': direction = changeDirection if changeDirection == 'left' and not direction =='right': direction = changeDirection if changeDirection == 'up' and not direction =='down': direction = changeDirection if changeDirection == 'down' and not direction =='up': direction = changeDirection #***接下来就是蛇头按照键盘方向进行转弯操作************ if direction == 'right': snakePosition[0] += 20 if direction == 'left': snakePosition[0] -= 20 if direction == 'up': snakePosition[1] -= 20 if direction == 'down': snakePosition[1] += 20 #将蛇头的位置加入列表之中 snakeSegments.insert(0,list(snakePosition)) #*************************判断是否吃到树莓************* if snakePosition[0] == raspberryPosition[0] and snakePosition[1] == raspberryPosition[1]: raspberrySpawned = 0 print("test7") else: snakeSegments.pop() print("test20") #****当树莓数量为0时,重新生成树莓**************** if raspberrySpawned == 0: x = random.randrange(1,32) y = random.randrange(1,24) raspberryPosition = [int(x*20),int(y*20)] raspberrySpawned = 1 score += 1 print("test8") #****刷新显示层*************************** playSurface.fill(blackColour) for position in snakeSegments[1:]: pygame.draw.rect(playSurface, whiteColour, Rect(position[0], position[1], 20, 20)) print("test9") pygame.draw.rect(playSurface,LightGrey, Rect(snakePosition[0],snakePosition[1], 20, 20)) print("test15") pygame.draw.rect(playSurface,redColour, Rect(raspberryPosition[0],raspberryPosition[1], 20, 20)) print("test16") #刷新pygame显示层 pygame.display.flip() #*******判断是否死亡****************** if snakePosition[0] > 620 or snakePosition[0] < 0: print("test10") gameOver(playSurface, score) if snakePosition[0] > 620 or snakePosition[0] < 0: print("test11") gameOver(playSurface, score) for snakeBody in snakeSegments[1:]: if snakePosition[0] == snakeBody[0] and snakePosition[1] == snakeBody[1]: print("test12") gameOver(playSurface, score) #********控制游戏进度,长度越长速度越快********** if len(snakeSegments) < 40: speed = 6 + len(snakeSegments) print("test13") else: speed = 16 fpsclock.tick(speed)