Pyweek 29 entry. Theme: Butterfly Effect. Framework: Pygame Zero.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

89 lines
1.9 KiB

  1. import sys
  2. from util import *
  3. from tiles import *
  4. from maps import *
  5. from player import Player
  6. WIDTH = 800
  7. HEIGHT = 800
  8. tilemap = TileMap(images.tiles, 5, 6)
  9. tilemap.scale_up(2)
  10. tmplayer = TileMap(images.human.human_regular_hair, 9, 2)
  11. tmplayer.scale_up(2)
  12. player = Player(tmplayer)
  13. player.pos = (100, 40)
  14. player.size = tilemap.tilesize.reduced(-1, -1)
  15. map = Map.load("map0.json", tilemap)
  16. map.pos = (30, 30)
  17. mousepos = Pos()
  18. mousetup = mousepos.tuple()
  19. ctrl = Controls()
  20. #ctrl.load("ctrl.json")
  21. def set_controls():
  22. ctrl.set(keys.W, 'up')
  23. ctrl.set(keys.A, 'left')
  24. ctrl.set(keys.LEFT, 'left')
  25. ctrl.set(keys.S, 'down')
  26. ctrl.set(keys.D, 'right')
  27. ctrl.set(keys.RIGHT, 'right')
  28. ctrl.set(keys.SPACE, 'jump', on='down')
  29. ctrl.set(keys.ESCAPE, 'exit', on='up')
  30. set_controls()
  31. ctrl.save("ctrl.json")
  32. actions = {
  33. 'left': lambda: player.force(-1, 0),
  34. 'right': lambda: player.force(1, 0),
  35. 'jump': player.jump,
  36. 'exit': sys.exit
  37. }
  38. def draw():
  39. screen.clear()
  40. #tileindex.draw(screen)
  41. #screen.draw.text(f"{mousetup} = {tileindex.tile_at(*mousetup)}", bottomright=(200, 200), color="green")
  42. #screen.draw.text(f"{mousetup} = {map.tile_at(*mousetup)}", bottomright=(200, 220), color="green")
  43. map.draw(screen)
  44. player.draw(screen)
  45. player.debug_draw(screen)
  46. screen.draw.text(player.debug_text(map), (10, 10), color="yellow")
  47. def key_act(key, action: str):
  48. do = ctrl.act(key, action)
  49. if do in actions:
  50. actions[do]()
  51. def update():
  52. for key in ctrl.keymap.keys():
  53. if keyboard[keys[key]]:
  54. key_act(key, ctrl.HELD)
  55. player.update(map)
  56. def on_mouse_down(pos, button):
  57. if button == mouse.LEFT:
  58. player.pos = pos
  59. else:
  60. pass
  61. def on_mouse_move(pos):
  62. global mousetup
  63. global mousepos
  64. mousetup = pos
  65. mousepos = Pos(*pos)
  66. def on_key_down(key):
  67. key_act(key, ctrl.DOWN)
  68. def on_key_up(key):
  69. key_act(key, ctrl.UP)