Pygame实现chrome浏览器恐龙小游戏

有很多同学找我要图片,对应的图片已经上传到我的CSDN下载中,大家有需要的自取——
//download.csdn.net/download/qq_42468130/12024020

说明:

第一次使用pygame,很多东西都是边查边用的;所以也没有进行很好的设计;

使用结构方法;代码思维稍微有些混乱;

最主要的时里边使用了大量的信号来控制动画;

pygame中的特性很少用到;

关于load函数使用时的路径问题解决方式在注释中

其中花费时间最多的是在随机加载障碍物的部分;

未完成的部分:

计分系统、速度提升——游戏难度的改变

游戏背景音乐

白天黑夜的交替效果

恐龙的行走效果动画——两张图片交替加载

代码如下:

import pygame
import turtle
import sys       
from random2 import randint
screen=pygame.display.set_mode((1150,300))
"""
代码思路:
    


"""
'''使用斜杠“/”: "c:/test.txt"… 不用反斜杠就没法产生歧义了 
将反斜杠符号转义: "c:\\test.txt"… 因为反斜杠是转义符,所以两个"\\"就表示一个反斜杠符号 
使用Python的raw string: r"c:\test.txt" … python下在字符串前面加上字母r,
表示后面是一个原始字符串raw string,不过raw string主要是为正则表达式而不是windows路径设计的,所以这种做法尽量少用,可能会出问题。'''
#初始化游戏背景 
bg_clound=pygame.image.load(r"C:\sourc\cloud1.png").convert_alpha()#云
bg_sun=pygame.image.load(r"C:\sourc\sun.png").convert_alpha()#太阳
bg_d1=pygame.image.load(r"C:\sourc\d1.png").convert_alpha()
bg_d2=pygame.image.load(r"C:\sourc\d2.png").convert_alpha()
bg_ground1=pygame.image.load(r"C:\sourc\ground1.png").convert_alpha()
bg_ground2=pygame.image.load(r"C:\sourc\ground2.png").convert_alpha()
#加载障碍物
tree1=pygame.image.load(r"C:\sourc\tree1.png").convert_alpha()
tree2=pygame.image.load(r"C:\sourc\tree2.png").convert_alpha()
tree3=pygame.image.load(r"C:\sourc\tree3.png").convert_alpha()
tree4=pygame.image.load(r"C:\sourc\tree4.png").convert_alpha()
trees=(tree1,tree2,tree3,tree4)
tree=[0,0,0,0]
trees_moved=[0,0,0,0]#记录障碍物的移动距离
trees_could=[False,False,False,False]#记录障碍物的位置状态
#设置背景颜色
bg_color=(255,255,255)
#填充背景
screen.fill(bg_color)
#显示初始场景
screen.blit(bg_ground1,(0,280))    
screen.blit(bg_ground2,(0,280))
screen.blit(bg_d1,(10,240))#恐龙

g1=True
gamestart=False
hight=0
instance=0
jumping =False
up=True
waiting=False

#地面移动
def bg1_move():
    global instance
    global g1
    if g1==True:
        instance+=0.3
        screen.blit(bg_ground1,(-instance,280))#地
        if instance>=1150:
            g1=False
    if g1==False:
        instance-=0.3
        screen.blit(bg_ground1,(instance,280))#地
        if instance<=0:
            g1=True
def bg2_move():
    global instance
    global g1
    if g1==True:
        screen.blit(bg_ground2,(1150-instance,280))#地
    else:
        screen.blit(bg_ground2,(instance-1150,280))#地
def draw_ground():
    #绘制云朵
    screen.blit(bg_sun,(1000,60))#太阳
    screen.blit(bg_clound,(900,180))
#云朵移动
'''
随机数作为云朵的高度

'''

def cloud_move(cloud,hight):
    global instance
    randnum=randint(40,60)
    screen.blit(cloud,(1150-instance,hight))   #地
    pass

#人物控制
def jump():
    global up
    global hight
    global jumping
    if jumping:
       
       if up==True:
           hight+=0.5
           screen.blit(bg_d1,(10,241-hight))
           if hight>=100:
               up=False
       if up==False:
           hight-=0.5
           screen.blit(bg_d1,(10,241-hight))
           if hight<=0:
              jumping=False
              up=True
    else:
        screen.blit(bg_d1,(10,240))
        
#按键判断
def event_check():
    global up
    global jumping
    global gamestart
    for event in pygame.event.get():
            if event.type == pygame.KEYDOWN: #键被按下
                if event.key == pygame.K_SPACE and gamestart==False:
                    gamestart=True
                elif event.key == pygame.K_SPACE and jumping==False:
                    jumping=True
                elif event.key == pygame.K_TAB:
                    pygame.quit()
                elif event.key==pygame.QUIT:
                    sys.exit()


#障碍物移动函数

def tree_move(tree,num):
    global trees_could
    global trees_moved
    
    if trees_moved[num]>=1150:
        #移出窗口之外的时候将移动距离和移动信号重置
        trees_could[num]=False
        trees_moved[num]=0
    else:
        #移动的过程中每次将移动距离加上速度值
        trees_moved[num]+=0.5
        screen.blit(tree,(1150-trees_moved[num],240))

#计数器构造函数
def put_singal(interval):
    global instance
    global waiting
    global t1
    if waiting==False:
        t1=instance
        
        waiting=True
    elif abs(abs(t1)-abs(instance))>=interval:
        waiting=False
        return True;
    else:
        
        return False

#随机间隔产生障碍物选择信号
def puttree(): 
    global trees_could
    global waiting
    global inte
    global tree
    if waiting==False:#计数未开始的时候产生随机间隔值
        inte=randint(100,300)
    #开始计数——put_siganl(),!!!!!!!!!!!!!计数完毕之后开始判断
    if put_singal(inte)==True:#随机间隔之后判断是否有可开始移动的障碍物
        #从数组第一个开始判断:
        if trees_could[0]==False:
            trees_could[0]=True
            tree[0]=trees[randint(0,3)]
        elif trees_could[1]==False:
            trees_could[1]=True
            tree[1]=trees[randint(0,3)]
        elif trees_could[2]==False:
            trees_could[2]=True
            tree[2]=trees[randint(0,3)]
        elif trees_could[3]==False:
            trees_could[3]=True
            tree[3]=trees[randint(0,3)]
#游戏主循环
def rungame():
    global trees_could
    global waiting
    global gamestart
    global tree
    while True:
        event_check()
        if gamestart:
            screen.fill(bg_color)

            draw_ground()
            #地面移动
            bg1_move()
            bg2_move()
            #起跳
            jump()
            #云朵移动
            cloud_move(bg_clound,30)
            #障碍物选择信号
            puttree()
            #障碍物移动
            if trees_could[0]:
                tree_move(tree[0],0)
            if trees_could[1]:     
                tree_move(tree[1],1)
            if trees_could[2]:        
                tree_move(tree[2],2)
            if trees_could[3]: 
                tree_move(tree[3],3)  
        pygame.display.flip()
rungame()


运行效果:

再附链接://download.csdn.net/download/qq_42468130/12024020

  • 6
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值