最简滚动平台跳跃

python turtle platform jum game最简平台滚动游戏
python turtle platform jum game最简平台滚动游戏

用海龟画图模块制作的跳跃小游戏,方便学习此类游戏的基本原理

"""
   最简滚动平台跳跃
"""

from sprites import * 

width,height = 480,360
screen = Screen()    
screen.tracer(0,0)

screen.bgcolor('dodger blue')
screen.colormode(255)
screen.setup(width,height)
screen.title("最简滚动平台跳跃")
screen.bgpic("bg.png")

ground0 = Sprite(shape='square',visible=False)
ground0.goto(-100,20)
ground0.color('dodger blue')
ground0.shapesize(2,2)
ground0.show()

ground1 = Sprite(shape='square',visible=False)
ground1.goto(0,-150)
ground1.color('cyan')
ground1.shapesize(2,130)
ground1.show()

ground2 = Sprite(shape='square',visible=False)
ground2.goto(50,-50)
ground2.color('green')
ground2.shapesize(1,4)
ground2.show()

ground3 = Sprite(shape='square',visible=False)
ground3.goto(150,50)
ground3.color('blue')
ground3.shapesize(1,4)
ground3.show()

ground4 = Sprite(shape='square',visible=False)
ground4.goto(350,0)
ground4.color('brown')
ground4.shapesize(1,4)
ground4.show()

ground5 = Sprite(shape='square',visible=False)
ground5.penup()
ground5.goto(550,20)
ground5.color('magenta')
ground5.shapesize(1,4)
ground5.show()

grounds = [ground0,ground1,ground2,ground3,ground4,ground5]

akey = Key('a')                       # 新建a键实例 
dkey = Key('d')                       # 新建d键实例
wkey = Key('w')                       # 新建w键实例
images1 = ['cat1.png','cat2.png']     # 朝右造型表
images2 = ['cat1_l.png','cat2_l.png'] # 朝左造型表
sp = AnimatedSprite(images1,images2)
sp.up()                               # 抬笔
sp.dy = 0                             # 垂直速度
sp.da = -0.02                         # 加速度
screen.listen()

ground_dx = 0
m1 = Mouse()
while 1:
  
  # 按键检测
  ground_dx = 0
  if akey.down:     # 如果按了a键
    ground_dx = 0.5
    sp.nextcostume()
    sp.useshapes(1)
    
  if dkey.down:     # 如果按了d键
    ground_dx = -0.5
    sp.nextcostume()
    sp.useshapes(0) 
  
  # 按w键跳跃
  if wkey.down and sp.isground:
      sp.dy = 2
      sp.addy(sp.dy)     
  
  sp.isground = False   # 是否在地面  
  
  for g in grounds:     # 每个地面
    g.addx(ground_dx)   # 都移动
    if sp.collide(g):   # 碰到任意一个地面则把标志为在地面
       sp.isground = True

  if sp.isground :      # 如果在地面,那么垂直速度为0
     sp.dy = 0
  else:                 # 否则就自由落体 
     sp.addy(sp.dy)
     sp.dy = sp.dy + sp.da  
     
  screen.update()
扫码后加李兴球微信号:scratch8,获赠100Python创意程序及大量Scratch作品。 本站所有作品,教程等皆为原创,盗版必究。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。扫码付款后的朋友可以加QQ: 406273900,提供技术支持及发送作品图片与配音素材。亦可直接向微信号scratch8付款购买。
李兴球的博客_Python创意编程技术前沿 » 最简滚动平台跳跃

学本领,探索更大的世界!

李兴球博客 风火轮编程主页