[python] 开发小游戏 豆豆吃花瓣
今天来做一个python游戏
使用python的游戏框架pygame开开发一个自创的豆豆吃花瓣的游戏【还没有取名字?】
首先看下游戏主目录下有些什么文件
就一个img的文件夹,和一个py的文件
img文件夹下有这几个用photoshop做好的图片资源,还有中文字体
打开sublime文本开发软件,写入最开始的游戏框架
#-*- coding:utf-8 -*-
import pygame
from pygame.locals import *
import random
from random import randint
import time
from sys import exit
pygame.init()
def main():SCREEN_SIZE = [640,480]screen=pygame.display.set_mode(SCREEN_SIZE,0,32)title=pygame.display.set_caption('Delicious')pygame.display.flip() #刷新界面while True:for event in pygame.event.get():if event.type == pygame.QUIT:exit()screen.fill([0,0,0]) #填充平面为黑色背景pygame.display.update()#游戏更新if __name__ == '__main__':main()
输出结果如下图:
出现了一个黑色背景的框框
这就证明游戏搭建成功了!
现在普及下pygame游戏中的各种方法:
screen = pygame.display.set_mode([640,480]) #创建一个窗口 框640,高480
bg = pygame.image.load(‘bg.png’) #载入为bg的图片转换为平面变量bg
screen.blit(bg,(0,0)) #绘制一个为bg的图片平面
mysecond_font=pygame.font.Font(“img/poster.ttf”,35) #载入字体,字号为35px
screen.fill([0,0,0]) #将屏幕填充为黑色[三个255也就是白色,使用的rgb填充]
#这里是循环轮播事件检测 有按键 鼠标 等等事件
for event in pygame.event.get():if event.type == pygame.QUIT:exit()if event.type == KEYDOWN:if event.key == K_w:myman[1]-=10if event.key == K_s:myman[1]+=10if event.key == K_a:mydirection='left'myman[0]-=10if event.key == K_d:mydirection='right'myman[0]+=10if event.type == KEYUP:pass
这里是引用
#用来左右方向的壁纸切换
def screenblit():#吃货左右状态绘制if mydirection=='left':screen.blit(man1a,myman)if mydirection=='right':screen.blit(man1a_back,myman)#这是角色上下左右的位置越墙检测
def overwall():if myman[0]<10:myman[0]=10if myman[0]>595:myman[0]=595if myman[1]<10:myman[1]=10if myman[1]>435:myman[1]=435
但是在pygame框架中,很多动画都需要自己写,设置刷新都需要自己做,不过难不倒作者啦我做了一个切换角色样貌的demo如下函数
def blit_animation(mybgflag,myman):#1.嘴巴动的动画if mybgflag[1]==0:mybgflag[1]=50if mydirection=='left':if mybgflag[0]==1:screen.blit(man1b,myman)mybgflag[0]=0else:screen.blit(man1a,myman)mybgflag[0]=1if mydirection=='right':if mybgflag[0]==1:screen.blit(man1b_back,myman)mybgflag[0]=0else:screen.blit(man1a_back,myman)mybgflag[0]=1mybgflag[1]-=5#2.上下跳动的动画if mybgflag[3]==0:mybgflag[3]=50if mybgflag[2]==1:myman[0]-=1.5myman[1]+=1.5mybgflag[2]=0else:myman[0]+=1.5myman[1]-=1.5mybgflag[2]=1mybgflag[3]-=5return mybgflag,myman#最后返回
#接下来就需要创建怪物了哦!
def createenemys(enemytimers,enemysa,enemyseveral):#创建怪物if enemytimers==0 and enemyseveral>0: #之创建5个怪物print 'create-enemy'enemyrand=random.randint(1, 8)if enemyrand==1 or enemyrand == 3:#topenemysa.append([random.randint(30, 610),-35,40])elif enemyrand==2 or enemyrand == 4:#downenemysa.append([random.randint(30, 610),515,40])elif enemyrand==5 or enemyrand == 7:#leftenemysa.append([-35,random.randint(30, 610),40])elif enemyrand==6 or enemyrand == 8:#rightenemysa.append([675,random.randint(30, 610),40])enemytimers=100enemyseveral-=1enemytimers-=5for enemy in enemysa:#怪物随机移动if enemy[1]<0:enemy[1]+=1else:if enemy[2]==0:enemy[2]=40enemydirection=random.randint(1, 45000)if enemydirection>0 and enemydirection<10000:#topenemy[1]-=2for i in range(random.randint(5, 10)):enemy[1]-=3elif enemydirection>=11000 and enemydirection<20000:#downenemy[1]+=3for i in range(random.randint(5, 10)):enemy[1]+=3elif enemydirection>=21000 and enemydirection<30000:#leftenemy[0]-=3for i in range(random.randint(5, 10)):enemy[0]-=3elif enemydirection>=31000 and enemydirection<=40000:#rightenemy[0]+=3for i in range(random.randint(5, 10)):enemy[0]+=3elif enemydirection>40000 and enemydirection<=45000:#nonepassenemy[2]-=5for enemy in enemysa:#怪物的越界if enemy[0]<10:enemy[0]=10if enemy[0]>595:enemy[0]=595if enemy[1]<10:enemy[1]=10if enemy[1]>435:enemy[1]=435for enemy in enemysa: #绘制怪物screen.blit(enemysaimg,(enemy[0],enemy[1]))return enemytimers,enemysa,enemyseveral#最后返回
def createfoods(food1,food1s,foodserverl):if foodserverl:print 'food'food1s.append((random.randint(10, 615),random.randint(10, 445)))foodserverl-=1if foodserverl==0 and len(food1s)==0:foodserverl=random.randint(1, 5)for food in food1s:screen.blit(food1,food)return food1,food1s,foodserverl#最后返回
#我是否吃到了食物def eachcheck(myman,enemysa,food1s,myblood,score):#我和食物的碰撞foodn=0for food in food1s:if myman[0]+30>food[0] and myman[0]<food[0]+25:if myman[1]+30>food[1] and myman[1]<food[1]+25:print 'MY AND FOOD'food1s.pop(foodn) if score>=0:score+=random.randint(1, 5)#吃到一个小花加分1-5 foodn+=1for enemy in enemysa:#怪物和我的碰撞if myman[0]+30>enemy[0] and myman[0]<enemy[0]+30:if myman[1]+30>enemy[1] and myman[1]<enemy[1]+30:print 'I AND ENEMY'if myblood>=0:myblood-=1#我的血扣for food in food1s:#我是吃到到了食物if myman[0]+30>food[0] and myman[0]<food[0]+25:if myman[1]+30>food[1] and myman[1]<food[1]+25:print 'I AND FOOD'for enemy in enemysa:#怪物和食物的碰撞foodn=0for food in food1s:if enemy[0]+30>food[0] and enemy[0]<food[0]+25:if enemy[1]+30>food[1] and enemy[1]<food[1]+25:print 'ENEMY AND FOOD'food1s.pop(foodn)myblood-=5foodn+=1return myman,enemysa,food1s,myblood,score
#接下来就是绘制角色的血量 游戏的输赢界面 和 游戏中有60秒倒计时的功能 如下:
def blitmyblood(myblood,myblood_surface,score):screen.blit(myblood_surface,[5,0])screen.blit(myscore_surface,[450,0])bloodbordor=pygame.draw.rect(screen,[0,0,0],[60,5,200,15],2)bloodvalue=pygame.draw.rect(screen,[255,255,0],[62,7,myblood,12])myscoretts_surface=myscore_font.render('%s'%score,True,(0,0,255))screen.blit(myscoretts_surface,[535,0])second_surface=mysecond_font.render('%s'%gamesecond,True,(255,255,0))screen.blit(second_surface,[0,440])return myblood,myblood_surface,scoredef GameOver(myblood,gameoverwindows,myman):if myblood<=0:screen.blit(gameoverbg2,[0,0])gameoverwindows=1return gameoverwindows,mymandef GameVictory(myblood,score,gamesecond):if myblood>0 and gamesecond<=0 and score>=100:screen.blit(victorybg,[0,0])return myblood,score,gameseconddef gametimes(timeflag,gamesecond):if timeflag==20:if gamesecond>0:gamesecond-=1timeflag=0timeflag+=1return timeflag,gamesecond
现在提供来源代码
在这里插入代码片
#-*- coding:utf-8 -*-
import pygame
from pygame.locals import *
import random
from random import randint
import time
from sys import exit
pygame.init()
#主函数
def main():SCREEN_SIZE=[640,480]FULLFLAG=0systemtime=0.05timeflag=0gamesecond=120#秒的绘画mysecond_font=pygame.font.Font("img/poster.ttf",35)second_surface=mysecond_font.render('%s'%gamesecond,True,(0,0,255))screen=pygame.display.set_mode(SCREEN_SIZE,0,32)title=pygame.display.set_caption('Delicious')screen.fill([0,0,0])bg=pygame.image.load('img/bg.png').convert_alpha()wall=pygame.image.load('img/wall.png').convert_alpha()man1a=pygame.image.load('img/man1a.png').convert_alpha()man1a_back=pygame.image.load('img/man1a_back.png').convert_alpha()man1b=pygame.image.load('img/man1b.png').convert_alpha()man1b_back=pygame.image.load('img/man1b_back.png').convert_alpha()myman=[SCREEN_SIZE[0]/2-35,SCREEN_SIZE[1]/2-35]mybgflag=[1,50,1,30]#第一个为吃货的嘴巴打开的状态,第二个为多久打开一次50个时间后,第三个为跳跃画面mydirection='right'myblood=196#吃货的血条#食物food1=pygame.image.load('img/food1.png').convert_alpha()food1s=[[]]food1s.pop(0)foodserverl=random.randint(3, 6)#随机产生食物个数
#分数
score=0
myblood_font=pygame.font.Font("img/poster.ttf",25)
myblood_surface=myblood_font.render(u'体力',True, (255,255,0))
myscore_font=pygame.font.Font("img/poster.ttf",35)
myscore_surface=myscore_font.render(u'分数',True,(0,0,255))
myscoretts_surface=myscore_font.render('%s'%score,True,(0,0,255))
#怪物1
enemysa=[[]]#怪物列表
enemysa.pop(0)
enemytimers=100#怪物产生时间个数
enemyseveral=random.randint(10,15)#怪物个数
enemysaimg=pygame.image.load('img/enemy1.png').convert_alpha()#怪物图片gameoverwindows=0#游戏结束为假的
gameoverbg2=pygame.image.load('img/GameOverbg2.png').convert_alpha()victorybg=pygame.image.load('img/victory.png').convert_alpha()pygame.key.set_repeat(1,100)#重复一个键
pygame.display.flip()#帧刷新def screenblit():#吃货左右状态绘制if mydirection=='left':screen.blit(man1a,myman)if mydirection=='right':screen.blit(man1a_back,myman)def pygame_event(mydirection):for event in pygame.event.get():if event.type == pygame.QUIT:exit()if event.type == KEYDOWN:if event.key == K_w:myman[1]-=10if event.key == K_s:myman[1]+=10if event.key == K_a:mydirection='left'myman[0]-=10if event.key == K_d:mydirection='right'myman[0]+=10if event.type == KEYUP:passreturn mydirectiondef overwall():if myman[0]<10:myman[0]=10if myman[0]>595:myman[0]=595if myman[1]<10:myman[1]=10if myman[1]>435:myman[1]=435def blit_animation(mybgflag,myman):#1.嘴巴动的动画if mybgflag[1]==0:mybgflag[1]=50if mydirection=='left':if mybgflag[0]==1:screen.blit(man1b,myman)mybgflag[0]=0else:screen.blit(man1a,myman)mybgflag[0]=1if mydirection=='right':if mybgflag[0]==1:screen.blit(man1b_back,myman)mybgflag[0]=0else:screen.blit(man1a_back,myman)mybgflag[0]=1mybgflag[1]-=5#2.上下跳动的动画if mybgflag[3]==0:mybgflag[3]=50if mybgflag[2]==1:myman[0]-=1.5myman[1]+=1.5mybgflag[2]=0else:myman[0]+=1.5myman[1]-=1.5mybgflag[2]=1mybgflag[3]-=5return mybgflag,myman#最后返回def createenemys(enemytimers,enemysa,enemyseveral):#创建怪物if enemytimers==0 and enemyseveral>0: #之创建5个怪物print 'create-enemy'enemyrand=random.randint(1, 8)if enemyrand==1 or enemyrand == 3:#topenemysa.append([random.randint(30, 610),-35,40])elif enemyrand==2 or enemyrand == 4:#downenemysa.append([random.randint(30, 610),515,40])elif enemyrand==5 or enemyrand == 7:#leftenemysa.append([-35,random.randint(30, 610),40])elif enemyrand==6 or enemyrand == 8:#rightenemysa.append([675,random.randint(30, 610),40])enemytimers=100enemyseveral-=1enemytimers-=5for enemy in enemysa:#怪物随机移动if enemy[1]<0:enemy[1]+=1else:if enemy[2]==0:enemy[2]=40enemydirection=random.randint(1, 45000)if enemydirection>0 and enemydirection<10000:#topenemy[1]-=2for i in range(random.randint(5, 10)):enemy[1]-=3elif enemydirection>=11000 and enemydirection<20000:#downenemy[1]+=3for i in range(random.randint(5, 10)):enemy[1]+=3elif enemydirection>=21000 and enemydirection<30000:#leftenemy[0]-=3for i in range(random.randint(5, 10)):enemy[0]-=3elif enemydirection>=31000 and enemydirection<=40000:#rightenemy[0]+=3for i in range(random.randint(5, 10)):enemy[0]+=3elif enemydirection>40000 and enemydirection<=45000:#nonepassenemy[2]-=5for enemy in enemysa:#怪物的越界if enemy[0]<10:enemy[0]=10if enemy[0]>595:enemy[0]=595if enemy[1]<10:enemy[1]=10if enemy[1]>435:enemy[1]=435for enemy in enemysa: #绘制怪物screen.blit(enemysaimg,(enemy[0],enemy[1]))return enemytimers,enemysa,enemyseveral#最后返回def createfoods(food1,food1s,foodserverl):if foodserverl:print 'food'food1s.append((random.randint(10, 615),random.randint(10, 445)))foodserverl-=1if foodserverl==0 and len(food1s)==0:foodserverl=random.randint(1, 5)for food in food1s:screen.blit(food1,food)return food1,food1s,foodserverl#最后返回def eachcheck(myman,enemysa,food1s,myblood,score):#我和食物的碰撞foodn=0for food in food1s:if myman[0]+30>food[0] and myman[0]<food[0]+25:if myman[1]+30>food[1] and myman[1]<food[1]+25:print 'MY AND FOOD'food1s.pop(foodn) if score>=0:score+=random.randint(1, 5)#吃到一个小花加分1-5 foodn+=1for enemy in enemysa:#怪物和我的碰撞if myman[0]+30>enemy[0] and myman[0]<enemy[0]+30:if myman[1]+30>enemy[1] and myman[1]<enemy[1]+30:print 'I AND ENEMY'if myblood>=0:myblood-=1#我的血扣for food in food1s:#我是吃到到了食物if myman[0]+30>food[0] and myman[0]<food[0]+25:if myman[1]+30>food[1] and myman[1]<food[1]+25:print 'I AND FOOD'for enemy in enemysa:#怪物和食物的碰撞foodn=0for food in food1s:if enemy[0]+30>food[0] and enemy[0]<food[0]+25:if enemy[1]+30>food[1] and enemy[1]<food[1]+25:print 'ENEMY AND FOOD'food1s.pop(foodn)myblood-=5foodn+=1return myman,enemysa,food1s,myblood,scoredef blitmyblood(myblood,myblood_surface,score):screen.blit(myblood_surface,[5,0])screen.blit(myscore_surface,[450,0])bloodbordor=pygame.draw.rect(screen,[0,0,0],[60,5,200,15],2)bloodvalue=pygame.draw.rect(screen,[255,255,0],[62,7,myblood,12])myscoretts_surface=myscore_font.render('%s'%score,True,(0,0,255))screen.blit(myscoretts_surface,[535,0])second_surface=mysecond_font.render('%s'%gamesecond,True,(255,255,0))screen.blit(second_surface,[0,440])return myblood,myblood_surface,scoredef GameOver(myblood,gameoverwindows,myman):if myblood<=0:screen.blit(gameoverbg2,[0,0])gameoverwindows=1return gameoverwindows,mymandef GameVictory(myblood,score,gamesecond):if myblood>0 and gamesecond<=0 and score>=100:screen.blit(victorybg,[0,0])return myblood,score,gameseconddef gametimes(timeflag,gamesecond):if timeflag==20:if gamesecond>0:gamesecond-=1timeflag=0timeflag+=1return timeflag,gamesecondwhile True:#游戏主体screen.blit(bg,[0,0]) #地图绘制mydirection=pygame_event(mydirection) #按键事件overwall()#出货超出界面判断if myblood>0 and gamesecond>0:screenblit()#吃货绘制 mybgflag,myman=blit_animation(mybgflag,myman)#吃货动画enemytimers,enemysa,enemyseveral=createenemys(enemytimers,enemysa,enemyseveral)#怪物绘制food1,food1s,foodserverl=createfoods(food1,food1s,foodserverl)#产生食物myblood,myblood_surface,score=blitmyblood(myblood,myblood_surface,score)#绘制血条分数等myman,enemysa,food1s,myblood,score=eachcheck(myman,enemysa,food1s,myblood,score)#碰撞检测gameoverwindows,myman=GameOver(myblood,gameoverwindows,myman)#游戏结束myblood,score,gamesecond=GameVictory(myblood,score,gamesecond)#胜利timeflag,gamesecond=gametimes(timeflag,gamesecond)time.sleep(systemtime)#延迟多少秒pygame.display.update()#游戏更新if __name__ == "__main__":main()
哈哈最终看一下游戏编译结果吧!有趣
哈哈,还不错哦,游戏就分享到这,需要源码的跟作者说一声!一家专门开发小游戏的苦逼作者
[python] 开发小游戏 豆豆吃花瓣相关推荐
- 学习Python开发小游戏(三)-----勇闯地下一百层
前提条件: 需要安装pygame,pgzero,numpy(windows:1.19.3) 功能: 1.初始化界面显示5个砖块和角色图片 2.键盘控制角色的左右移动,当角色不在砖块上时显示角色下降 3 ...
- 【Python开发小游戏】安装 pygame
pygame下载地址: https://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame 在对应文件夹中cmd,pip进行安装: pip install pygam ...
- 10个python入门小游戏,零基础打通关,就能掌握编程基础
前言 不会python就不能用python开发入门级的小游戏? 当然不是,我收集了十个python入门小游戏的源码和教程,并且即使你没有python基础,只要跟着这十个小游戏的开发详细教程去做,以及有 ...
- python经典小游戏五子棋,适合python编程的小游戏
python入门可以做的小游戏 1.Python入门拼图小游戏简单介绍:将图像分为m×n个矩形块,并将图像右下角的矩形块替换为空白块后,将这些矩形块随机摆放成原图像的形状. 2.Python入门推箱子 ...
- python简单小游戏代码教程,Python简单小游戏代码
球球各位大神怎么用python写一个猜词小游戏的代码? key = input('请输入一个单词:')description = input('输入单词描述:')chance = 5mark = 5p ...
- python入门小游戏代码20行,python入门小游戏代码
python入门可以做的小游戏 1.Python入门拼图小游戏简单介绍:将图像分为m×n个矩形块,并将图像右下角的矩形块替换为空白块后,将这些矩形块随机摆放成原图像的形状. 2.Python入门推箱子 ...
- 第一次用python写小游戏
记一次用python开发小游戏的经历 在学习if语句和循环时,突然想起初中时玩过一款叫<黑道圣徒3>的游戏,里面有一个文字冒险的小游戏,看样子能用if实现,就想着把它做出来. 游戏玩法 开 ...
- Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇)
Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第二篇) 代码实现 窗口类 小车类 玩家类 电脑类 赛道类 小树类 打字类 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) ...
- Python 打字小游戏开发,来体验不一样的打字游戏乐趣(完结篇)
Python 打字小游戏开发,来体验不一样的打字游戏乐趣(完结篇) 资源下载 完整代码 Python 打字小游戏开发,来体验不一样的打字游戏乐趣(第一篇) Python 打字小游戏开发,来体验不一样的 ...
最新文章
- Cocos2d中从场景切换到UIViewController视图方法总结
- 谷歌宣布在北京成立AI中国中心:李飞飞和李佳共同领导
- 图片来源html,HTML图片(Images)
- Class.getResource()、ClassLoader.getResource()和this.class.getClassLoader()解析
- centos 宝塔面板 mongodb 设置用户账号密码登录
- OpenShift 4.6 新特性 - 用 Windows MachineConfig Operator 管理 Windows Container
- kali 运行java_kali linux运行java程序
- 【招聘内推】百度地图招聘推荐推送算法工程师
- 蓝桥杯每日真题之砝码称重(01背包)
- Netron可视化网络结构
- cisco anyConnect 不用每次输入密码的办法
- xaxis python_Python中的分组Xaxis可变性图
- 远程公司内网服务器【内网穿透】
- JNA 中 GetProcAddress(HMODULE hmodule, int ordinal) 的正确使用方式。LoadLibrary
- 前端工程师需要学习ps 吗_前端人员一定要掌握的PS技巧
- 【毕业设计】STM32单片机的智能手环 - 蓝牙手环 物联网
- NC | 西湖大学鞠峰组在聚氯乙烯塑料微生物降解方向取得新突破
- 区块链知识系列 - 区块链大事记
- android组件悬浮,Andorid 任意界面悬浮窗,实现悬浮窗如此简单
- http: TLS handshake error from xxx.xxx.xxx.xxx:xxxx : read tcp xxx.xxx.xxx.xxx:6443->xxx.xxx.xxx.xxx