今天来做一个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]-=10
if event.key == K_s:
myman[1]+=10
if event.key == K_a:
mydirection='left'
myman[0]-=10
if event.key == K_d:
mydirection='right'
myman[0]+=10
if 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]=10
if myman[0]>595:
myman[0]=595
if myman[1]<10:
myman[1]=10
if myman[1]>435:
myman[1]=435
但是在pygame框架中,很多动画都需要自己写,设置刷新都需要自己做,不过难不倒作者啦我做了一个切换角色样貌的demo如下函数
def blit_animation(mybgflag,myman):
#1.嘴巴动的动画
if mybgflag[1]==0:
mybgflag[1]=50
if mydirection=='left':
if mybgflag[0]==1:
screen.blit(man1b,myman)
mybgflag[0]=0
else:
screen.blit(man1a,myman)
mybgflag[0]=1
if mydirection=='right':
if mybgflag[0]==1:
screen.blit(man1b_back,myman)
mybgflag[0]=0
else:
screen.blit(man1a_back,myman)
mybgflag[0]=1
mybgflag[1]-=5
#2.上下跳动的动画
if mybgflag[3]==0:
mybgflag[3]=50
if mybgflag[2]==1:
myman[0]-=1.5
myman[1]+=1.5
mybgflag[2]=0
else:
myman[0]+=1.5
myman[1]-=1.5
mybgflag[2]=1
mybgflag[3]-=5
return 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:#top
enemysa.append([random.randint(30, 610),-35,40])
elif enemyrand==2 or enemyrand == 4:#down
enemysa.append([random.randint(30, 610),515,40])
elif enemyrand==5 or enemyrand == 7:#left
enemysa.append([-35,random.randint(30, 610),40])
elif enemyrand==6 or enemyrand == 8:#right
enemysa.append([675,random.randint(30, 610),40])
enemytimers=100
enemyseveral-=1
enemytimers-=5
for enemy in enemysa:#怪物随机移动
if enemy[1]<0:
enemy[1]+=1
else:
if enemy[2]==0:
enemy[2]=40
enemydirection=random.randint(1, 45000)
if enemydirection>0 and enemydirection<10000:#top
enemy[1]-=2
for i in range(random.randint(5, 10)):
enemy[1]-=3
elif enemydirection>=11000 and enemydirection<20000:#down
enemy[1]+=3
for i in range(random.randint(5, 10)):
enemy[1]+=3
elif enemydirection>=21000 and enemydirection<30000:#left
enemy[0]-=3
for i in range(random.randint(5, 10)):
enemy[0]-=3
elif enemydirection>=31000 and enemydirection<=40000:#right
enemy[0]+=3
for i in range(random.randint(5, 10)):
enemy[0]+=3
elif enemydirection>40000 and enemydirection<=45000:#none
pass
enemy[2]-=5
for enemy in enemysa:#怪物的越界
if enemy[0]<10:
enemy[0]=10
if enemy[0]>595:
enemy[0]=595
if enemy[1]<10:
enemy[1]=10
if enemy[1]>435:
enemy[1]=435
for 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-=1
if 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=0
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 'MY AND FOOD'
food1s.pop(foodn)
if score>=0:
score+=random.randint(1, 5)#吃到一个小花加分1-5
foodn+=1
for 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=0
for 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-=5
foodn+=1
return 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,score
def GameOver(myblood,gameoverwindows,myman):
if myblood<=0:
screen.blit(gameoverbg2,[0,0])
gameoverwindows=1
return gameoverwindows,myman
def GameVictory(myblood,score,gamesecond):
if myblood>0 and gamesecond<=0 and score>=100:
screen.blit(victorybg,[0,0])
return myblood,score,gamesecond
def gametimes(timeflag,gamesecond):
if timeflag==20:
if gamesecond>0:
gamesecond-=1
timeflag=0
timeflag+=1
return 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=0
systemtime=0.05
timeflag=0
gamesecond=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]-=10
if event.key == K_s:
myman[1]+=10
if event.key == K_a:
mydirection='left'
myman[0]-=10
if event.key == K_d:
mydirection='right'
myman[0]+=10
if event.type == KEYUP:
pass
return mydirection
def overwall():
if myman[0]<10:
myman[0]=10
if myman[0]>595:
myman[0]=595
if myman[1]<10:
myman[1]=10
if myman[1]>435:
myman[1]=435
def blit_animation(mybgflag,myman):
#1.嘴巴动的动画
if mybgflag[1]==0:
mybgflag[1]=50
if mydirection=='left':
if mybgflag[0]==1:
screen.blit(man1b,myman)
mybgflag[0]=0
else:
screen.blit(man1a,myman)
mybgflag[0]=1
if mydirection=='right':
if mybgflag[0]==1:
screen.blit(man1b_back,myman)
mybgflag[0]=0
else:
screen.blit(man1a_back,myman)
mybgflag[0]=1
mybgflag[1]-=5
#2.上下跳动的动画
if mybgflag[3]==0:
mybgflag[3]=50
if mybgflag[2]==1:
myman[0]-=1.5
myman[1]+=1.5
mybgflag[2]=0
else:
myman[0]+=1.5
myman[1]-=1.5
mybgflag[2]=1
mybgflag[3]-=5
return 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:#top
enemysa.append([random.randint(30, 610),-35,40])
elif enemyrand==2 or enemyrand == 4:#down
enemysa.append([random.randint(30, 610),515,40])
elif enemyrand==5 or enemyrand == 7:#left
enemysa.append([-35,random.randint(30, 610),40])
elif enemyrand==6 or enemyrand == 8:#right
enemysa.append([675,random.randint(30, 610),40])
enemytimers=100
enemyseveral-=1
enemytimers-=5
for enemy in enemysa:#怪物随机移动
if enemy[1]<0:
enemy[1]+=1
else:
if enemy[2]==0:
enemy[2]=40
enemydirection=random.randint(1, 45000)
if enemydirection>0 and enemydirection<10000:#top
enemy[1]-=2
for i in range(random.randint(5, 10)):
enemy[1]-=3
elif enemydirection>=11000 and enemydirection<20000:#down
enemy[1]+=3
for i in range(random.randint(5, 10)):
enemy[1]+=3
elif enemydirection>=21000 and enemydirection<30000:#left
enemy[0]-=3
for i in range(random.randint(5, 10)):
enemy[0]-=3
elif enemydirection>=31000 and enemydirection<=40000:#right
enemy[0]+=3
for i in range(random.randint(5, 10)):
enemy[0]+=3
elif enemydirection>40000 and enemydirection<=45000:#none
pass
enemy[2]-=5
for enemy in enemysa:#怪物的越界
if enemy[0]<10:
enemy[0]=10
if enemy[0]>595:
enemy[0]=595
if enemy[1]<10:
enemy[1]=10
if enemy[1]>435:
enemy[1]=435
for 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-=1
if 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=0
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 'MY AND FOOD'
food1s.pop(foodn)
if score>=0:
score+=random.randint(1, 5)#吃到一个小花加分1-5
foodn+=1
for 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=0
for 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-=5
foodn+=1
return myman,enemysa,food1s,myblood,score
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,score
def GameOver(myblood,gameoverwindows,myman):
if myblood<=0:
screen.blit(gameoverbg2,[0,0])
gameoverwindows=1
return gameoverwindows,myman
def GameVictory(myblood,score,gamesecond):
if myblood>0 and gamesecond<=0 and score>=100:
screen.blit(victorybg,[0,0])
return myblood,score,gamesecond
def gametimes(timeflag,gamesecond):
if timeflag==20:
if gamesecond>0:
gamesecond-=1
timeflag=0
timeflag+=1
return timeflag,gamesecond
while 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()
哈哈最终看一下游戏编译结果吧!有趣
哈哈,还不错哦,游戏就分享到这,需要源码的跟作者说一声!一家专门开发小游戏的苦逼作者