写简单游戏,学编程语言-python篇:大鱼吃小鱼
2014-01-10 19:00
495 查看
很常见的游戏之一,实现原理并不复杂,并且参考了几个相关的代码。这边主要还是以学习编程语言和学习编程思路为重点记录一下吧。最近时间有点吃紧,只能匆忙记录一下。用pygame做的大鱼吃小鱼的游戏截图如下:(有些鱼的图片背景没有做透明处理,这块确实需要点美工时间,只能先凑合了)。
View Code
ps:最近越来越懒了,不知道当初订的每周至少写一篇随笔的目标能否坚持下去。另外鄙视下12306上刷票的黄牛,刷了一天没买到票,哎 不说了都是泪。
import pygame,sys,time,random from pygame.locals import * WINWIDTH=640 WINHEIGHT=480 CAMERASLACK=70 lfish_img=[] FISHSIZE=8 PLAYERSIZE=3 SCREEN=None rfish_img=[] lplayer_img=[] rplayer_img=[] backgroundimg=None rboss_img=None lboss_img=None FPS=30 WHITE=[255,255,255] MAXHEALTH=3 MINSPEED=3 MAXSPEED=9 MOVESPEED=7 HALF_WINWIDTH=int(WINWIDTH/2) HALF_WINHEIGHT=int(WINHEIGHT/2) WINSIZE=300 def main(): global FPSCLOCK,SCREEN,lfish_img,rfish_img,lplayer_img,rplayer_img,BASICFONT\ ,rboss_img,lboss_img,backgroundimg pygame.init() FPSCLOCK=pygame.time.Clock() SCREEN=pygame.display.set_mode((WINWIDTH,WINHEIGHT)) pygame.display.set_caption("fish eat fish") BASICFONT=pygame.font.Font("freesansbold.ttf",32) for i in range(FISHSIZE): fishimg=pygame.image.load("Fish%s.bmp" % i) fishimg.set_colorkey(WHITE) lfish_img.append(fishimg) rfishimg=pygame.transform.flip(fishimg,True,False) rfish_img.append(rfishimg) for i in range(PLAYERSIZE): playerimg=pygame.image.load("player%s.png" % i) playerimg.set_colorkey(WHITE) lplayer_img.append(playerimg) rplayerimg=pygame.transform.flip(playerimg,True,False) rplayerimg.set_colorkey(WHITE) rplayer_img.append(rplayerimg) bossimg=pygame.image.load("boss.png") # bossimg.set_colorkey([0,0,0]) # SCREEN.blit(bossimg,(0,0)) lboss_img=bossimg rbossimg=pygame.transform.flip(bossimg,True,False) rboss_img=rbossimg backgroundimg=pygame.image.load("background.jpg") while True: runGame() def runGame(): invulnerableMode=False invulnerableStartTime=0 gameOverMode=False gameOverStartTime=0 winMode = False gameOverSurf=BASICFONT.render('Game Over',True,WHITE) gameOverRect=gameOverSurf.get_rect() gameOverRect.center=(HALF_WINWIDTH,HALF_WINHEIGHT) winSurf=BASICFONT.render('you succeed',True,WHITE) winRect=winSurf.get_rect() winRect.center=(HALF_WINWIDTH,HALF_WINHEIGHT) winSurf2=BASICFONT.render('you succeed',True,WHITE) winRect2=winSurf2.get_rect() winRect2.center=(HALF_WINWIDTH,HALF_WINHEIGHT+30) camerax=0 cameray=0 LEFT=0 RIGHT=1 fishobjs=[] playerobj={'surface':pygame.transform.scale(lplayer_img[0],(25,25)), 'size':25, 'facing':LEFT, 'x':HALF_WINWIDTH, 'y':HALF_WINHEIGHT, 'health':MAXHEALTH} moveLeft = False moveRight = False moveUp = False moveDown = False while True: if invulnerableMode and time.time() - invulnerableStartTime>2: invulnerableMode =False for sobj in fishobjs: sobj['x']+=sobj['movx'] sobj['y']+=sobj['movy'] for i in range(len(fishobjs)-1,-1,-1): if isOutsideArea(camerax,cameray,fishobjs[i]): del fishobjs[i] while len(fishobjs)<5: fishobjs.append(makeNewfish(camerax,cameray)) playerCenterx=playerobj['x']+int(playerobj['size']/2) playerCentery=playerobj['y']+int(playerobj['size']/2) if (camerax+HALF_WINWIDTH)-playerCenterx>CAMERASLACK: camerax=playerCenterx+CAMERASLACK-HALF_WINWIDTH elif playerCenterx-(camerax+HALF_WINWIDTH)>CAMERASLACK: camerax=playerCenterx-CAMERASLACK-HALF_WINWIDTH if (cameray+HALF_WINHEIGHT)-playerCentery>CAMERASLACK: cameray=playerCentery+CAMERASLACK-HALF_WINHEIGHT elif playerCentery-(camerax+HALF_WINHEIGHT)>CAMERASLACK: cameray=playerCentery-CAMERASLACK-HALF_WINHEIGHT backrect=pygame.Rect((camerax-WINWIDTH,cameray-WINHEIGHT,WINWIDTH*3,WINHEIGHT*3)) newbackgroundimg=pygame.transform.scale(backgroundimg,(WINWIDTH*3,WINHEIGHT*3)) SCREEN.blit(newbackgroundimg,backrect) for obj in fishobjs: obj['rect']=pygame.Rect((obj['x']-camerax,obj['y']-cameray,obj['width'] ,obj['height'])) SCREEN.blit(obj['surface'],obj['rect']) flashIsOn=round(time.time(),1)*10%2==1 if not gameOverMode and not (invulnerableMode and flashIsOn): playerobj['rect']=pygame.Rect((playerobj['x']-camerax,playerobj['y'] -cameray,playerobj['size'],playerobj['size'])) SCREEN.blit(playerobj['surface'],playerobj['rect']) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type==KEYDOWN: if event.key in (K_UP,K_w): moveDown =False moveUp = True elif event.key in (K_DOWN,K_s): moveDown =True moveUp = False elif event.key in (K_LEFT,K_a): moveRight = False moveLeft= True if playerobj['facing']==RIGHT: playerobj['surface']=playersurfaceset(playerobj['size'], True) playerobj['facing']=LEFT elif event.key in (K_RIGHT,K_d): moveRight = True moveLeft= False if playerobj['facing']==LEFT: playerobj['surface']=playersurfaceset(playerobj['size'], False) playerobj['facing']=RIGHT elif winMode and event.key == K_r: return elif event.type==KEYUP: if event.key in (K_LEFT,K_a): moveLeft=False elif event.key in (K_RIGHT,K_d): moveRight=False elif event.key in (K_UP,K_w): moveRight=False elif event.key in (K_DOWN,K_s): moveRight=False if not gameOverMode: if moveLeft: playerobj['x']-=MOVESPEED if moveRight: playerobj['x']+=MOVESPEED if moveUp: playerobj['y']-=MOVESPEED if moveDown: playerobj['y']+=MOVESPEED for i in range(len(fishobjs)-1,-1,-1): obj=fishobjs[i] if 'rect' in obj and playerobj['rect'].colliderect(obj['rect']): if obj['width']*obj['height']<=playerobj['size']**2: playerobj['size']+=int((obj['width']*obj['height'])**0.2) del fishobjs[i] if playerobj['face']== LEFT: playerobj['surface']=playersurfaceset(playerobj['size'],True) if playerobj['face']== RIGHT: playerobj['surface']=playersurfaceset(playerobj['size'],False) if playerobj['size']>WINSIZE: winMode=True elif not invulnerableMode: invulnerableMode=True invulnerableStartTime=time.time() playerobj['health']-=1 if playerobj['health']==0: gameOverMode =True gameOverStartTime=time.time() if winMode: SCREEN.blit(winSurf,winRect) SCREEN.blit(winSurf2,winRect2) pygame.display.update() FPSCLOCK.tick(FPS) def isOutsideArea(camerax,cameray,fishobj): boundsLeftEdge =camerax-WINWIDTH boundsTopEdge=cameray-WINHEIGHT boundsRect=pygame.Rect(boundsLeftEdge,boundsTopEdge,WINWIDTH*3,WINHEIGHT*3) objRect=pygame.Rect(fishobj['x'],fishobj['y'],fishobj['width'],fishobj['height']) return not boundsRect.colliderect(objRect) def makeNewfish(camerax,cameray): sq={} generalSize=random.randint(5,25) multiplier = random.randint(1,3) sq['width'] = (generalSize +random.randint(0,10))*multiplier sq['height']= (generalSize +random.randint(0,10))*multiplier sq['x'],sq['y']=getRandomoffCameraPos(camerax,cameray,sq['width'],sq['height']) sq['movx'] = getRandomVelcocity() sq['movy'] = getRandomVelcocity() if sq['movx']<0: sq['surface']=surfaceset(sq['width'],sq['height'],True) else: sq['surface']=surfaceset(sq['width'],sq['height'],False) return sq def playersurfaceset(size,Face): surface=None if size<100: if Face ==True: surface= pygame.transform.scale(lplayer_img[0],(size,size)) else: surface= pygame.transform.scale(rplayer_img[0],(size,size)) elif size<200 and size>=100: if Face ==True: surface= pygame.transform.scale(lplayer_img[1],(size,size)) else: surface= pygame.transform.scale(rplayer_img[1],(size,size)) else: if Face ==True: surface= pygame.transform.scale(lplayer_img[2],(size,size)) else: surface= pygame.transform.scale(rplayer_img[2],(size,size)) return surface def surfaceset(width,height,Face): surface=None if width*height>200*200: if Face == True: surface= pygame.transform.scale(lboss_img,(width,height)) else: surface= pygame.transform.scale(rboss_img,(width,height)) else: index= int((width*height)%(FISHSIZE-1)) if Face == True: surface= pygame.transform.scale(lfish_img[index],(width,height)) else: surface= pygame.transform.scale(rfish_img[index],(width,height)) return surface def getRandomVelcocity(): speed=random.randint(MINSPEED,MAXSPEED) if random.randint(0,1): return speed else: return -speed def getRandomoffCameraPos(camerax,cameray,objwidth,objheight): cameraRect=pygame.Rect(camerax,cameray,WINWIDTH,WINHEIGHT) while True: x=random.randint(camerax-WINWIDTH,camerax+2*(WINWIDTH)) y=random.randint(camerax-WINHEIGHT,camerax+2*(WINHEIGHT)) objrect=pygame.Rect(x,y,objwidth,objheight) if not objrect.colliderect(cameraRect): return x,y if __name__ == '__main__': main()
View Code
ps:最近越来越懒了,不知道当初订的每周至少写一篇随笔的目标能否坚持下去。另外鄙视下12306上刷票的黄牛,刷了一天没买到票,哎 不说了都是泪。
相关文章推荐
- 写简单游戏,学编程语言-python篇
- 写简单游戏,学编程语言-python篇:传说哥大战剧毒术士
- 变态版大鱼吃小鱼-基于pixi.js 2D游戏引擎
- 游戏:大鱼吃小鱼
- cocos2dx游戏开发之利用多点触摸(实现缩放功能或者简单的手势识别)
- 简单游戏外挂,植物大战僵尸外挂,附cpp源代码
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(二十)
- 51Nod 1289 大鱼吃小鱼
- (NO.00003)iOS游戏简单的机器人投射游戏成形记(二十一)
- Html5游戏框架createJs的简单用法
- Unity 2D游戏开发快速入门第1章创建一个简单的2D游戏
- 如何用cocos2d-x3.0制作一款简单的游戏:第二部分(旋转炮塔)
- 001简单的游戏
- [Unity3D]手机3D游戏开发:使用UnityRemote开发一个简单的FPS案例
- 概论2--简单打字游戏
- vb做的类似打鼹鼠的游戏,简单,十分推荐初学vb 想做游戏的人学习
- 来自苹果的编程语言——Swift简单介绍
- 【Android游戏开发之一】设置全屏以及绘画简单的图形
- Cocos2d-x 2.0.4 如何制作一个简单的游戏
- 【一】用JavaSE编写简单的贪吃蛇游戏