Skip to content

Commit 178a199

Browse files
authored
Merge pull request #1760 from FoamyGuy/tilemap_game_cp7_odb
cp7 version of OnDiskBitmap usage for tilemap game
2 parents 37dffc1 + 1d52ec0 commit 178a199

File tree

1 file changed

+183
-183
lines changed
  • Tilemap_Game_With_CircuitPython

1 file changed

+183
-183
lines changed

Tilemap_Game_With_CircuitPython/code.py

Lines changed: 183 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -436,201 +436,201 @@ def show_splash(new_text, color, vertical_offset=18):
436436
group.append(splash)
437437

438438

439+
# Make the splash context
440+
splash = displayio.Group()
441+
442+
# CircuitPython 6 & 7 compatible
443+
439444
# game message background bmp file
440-
with open(
441-
"tilegame_assets/game_message_background.bmp", "rb"
442-
) as game_message_background:
443-
# Make the splash context
444-
splash = displayio.Group()
445-
446-
# CircuitPython 6 & 7 compatible
447-
odb = displayio.OnDiskBitmap(game_message_background)
448-
bg_grid = displayio.TileGrid(odb, pixel_shader=getattr(odb, 'pixel_shader', displayio.ColorConverter()))
449-
450-
# # CircuitPython 7+ compatible
451-
# odb = displayio.OnDiskBitmap("tilegame_assets/game_message_background.bmp")
452-
# bg_grid = displayio.TileGrid(odb, pixel_shader=odb.pixel_shader)
453-
454-
splash.append(bg_grid)
455-
456-
# Text for the message
457-
text_group = displayio.Group(x=14, y=8)
458-
text_area = label.Label(terminalio.FONT, text=" " * 180, color=0xD39AE5)
459-
text_group.append(text_area)
460-
splash.append(text_group)
461-
462-
# main loop
463-
while True:
464-
# set the current button values into variables
465-
cur_btn_vals = ugame.buttons.get_pressed()
466-
cur_up = cur_btn_vals & ugame.K_UP
467-
cur_down = cur_btn_vals & ugame.K_DOWN
468-
cur_right = cur_btn_vals & ugame.K_RIGHT
469-
cur_left = cur_btn_vals & ugame.K_LEFT
470-
cur_a = cur_btn_vals & ugame.K_O or cur_btn_vals & ugame.K_X
471-
472-
if GAME_STATE["STATE"] == STATE_WAITING:
473-
print(cur_a)
474-
if cur_a:
475-
GAME_STATE["STATE"] = STATE_PLAYING
476-
group.remove(splash)
477-
478-
if GAME_STATE["STATE"] == STATE_PLAYING:
479-
# check for up button press / release
480-
if not cur_up and prev_up:
481-
if can_player_move(UP):
482-
x_offset = 0
483-
y_offset = -1
484-
485-
# check for down button press / release
486-
if not cur_down and prev_down:
487-
if can_player_move(DOWN):
488-
x_offset = 0
489-
y_offset = 1
490-
491-
# check for right button press / release
492-
if not cur_right and prev_right:
493-
if can_player_move(RIGHT):
494-
x_offset = 1
495-
y_offset = 0
496-
497-
# check for left button press / release
498-
if not cur_left and prev_left:
499-
if can_player_move(LEFT):
500-
x_offset = -1
501-
y_offset = 0
502-
503-
# if any offset is not zero then we need to process player movement
504-
if x_offset != 0 or y_offset != 0:
505-
# variable to store if player is allowed to move
506-
can_move = False
507-
508-
# coordinates the player is moving to
509-
moving_to_coords = (
510-
GAME_STATE["PLAYER_LOC"][0] + x_offset,
511-
GAME_STATE["PLAYER_LOC"][1] + y_offset,
512-
)
445+
game_message_background = open("tilegame_assets/game_message_background.bmp", "rb")
446+
odb = displayio.OnDiskBitmap(game_message_background)
447+
bg_grid = displayio.TileGrid(odb, pixel_shader=getattr(odb, 'pixel_shader', displayio.ColorConverter()))
513448

514-
# tile name of the spot player is moving to
515-
moving_to_tile_name = GAME_STATE["CURRENT_MAP"][
516-
moving_to_coords[0], moving_to_coords[1]
517-
]
449+
# # CircuitPython 7+ compatible
450+
# game message background bmp file
451+
# odb = displayio.OnDiskBitmap("tilegame_assets/game_message_background.bmp")
452+
# bg_grid = displayio.TileGrid(odb, pixel_shader=odb.pixel_shader)
453+
454+
splash.append(bg_grid)
455+
456+
# Text for the message
457+
text_group = displayio.Group(x=14, y=8)
458+
text_area = label.Label(terminalio.FONT, text=" " * 180, color=0xD39AE5)
459+
text_group.append(text_area)
460+
splash.append(text_group)
461+
462+
# main loop
463+
while True:
464+
# set the current button values into variables
465+
cur_btn_vals = ugame.buttons.get_pressed()
466+
cur_up = cur_btn_vals & ugame.K_UP
467+
cur_down = cur_btn_vals & ugame.K_DOWN
468+
cur_right = cur_btn_vals & ugame.K_RIGHT
469+
cur_left = cur_btn_vals & ugame.K_LEFT
470+
cur_a = cur_btn_vals & ugame.K_O or cur_btn_vals & ugame.K_X
471+
472+
if GAME_STATE["STATE"] == STATE_WAITING:
473+
print(cur_a)
474+
if cur_a:
475+
GAME_STATE["STATE"] = STATE_PLAYING
476+
group.remove(splash)
477+
478+
if GAME_STATE["STATE"] == STATE_PLAYING:
479+
# check for up button press / release
480+
if not cur_up and prev_up:
481+
if can_player_move(UP):
482+
x_offset = 0
483+
y_offset = -1
484+
485+
# check for down button press / release
486+
if not cur_down and prev_down:
487+
if can_player_move(DOWN):
488+
x_offset = 0
489+
y_offset = 1
490+
491+
# check for right button press / release
492+
if not cur_right and prev_right:
493+
if can_player_move(RIGHT):
494+
x_offset = 1
495+
y_offset = 0
496+
497+
# check for left button press / release
498+
if not cur_left and prev_left:
499+
if can_player_move(LEFT):
500+
x_offset = -1
501+
y_offset = 0
502+
503+
# if any offset is not zero then we need to process player movement
504+
if x_offset != 0 or y_offset != 0:
505+
# variable to store if player is allowed to move
506+
can_move = False
507+
508+
# coordinates the player is moving to
509+
moving_to_coords = (
510+
GAME_STATE["PLAYER_LOC"][0] + x_offset,
511+
GAME_STATE["PLAYER_LOC"][1] + y_offset,
512+
)
518513

519-
# if there are entity(s) at spot the player is moving to
520-
if moving_to_coords in GAME_STATE["ENTITY_SPRITES_DICT"]:
521-
print("found entity(s) where we are moving to")
522-
523-
# loop over all entities at the location player is moving to
524-
for entity_obj in GAME_STATE["ENTITY_SPRITES_DICT"][
525-
moving_to_coords
526-
]:
527-
print("checking entity %s" % entity_obj["map_tile_name"])
528-
# if the entity has a before_move behavior function
529-
if "before_move" in TILES[entity_obj["map_tile_name"]].keys():
530-
print(
531-
"calling before_move %s, %s, %s"
532-
% (
533-
moving_to_coords,
534-
GAME_STATE["PLAYER_LOC"],
535-
entity_obj,
536-
)
537-
)
538-
# call the before_move behavior function act upon it's result
539-
if TILES[entity_obj["map_tile_name"]]["before_move"](
514+
# tile name of the spot player is moving to
515+
moving_to_tile_name = GAME_STATE["CURRENT_MAP"][
516+
moving_to_coords[0], moving_to_coords[1]
517+
]
518+
519+
# if there are entity(s) at spot the player is moving to
520+
if moving_to_coords in GAME_STATE["ENTITY_SPRITES_DICT"]:
521+
print("found entity(s) where we are moving to")
522+
523+
# loop over all entities at the location player is moving to
524+
for entity_obj in GAME_STATE["ENTITY_SPRITES_DICT"][
525+
moving_to_coords
526+
]:
527+
print("checking entity %s" % entity_obj["map_tile_name"])
528+
# if the entity has a before_move behavior function
529+
if "before_move" in TILES[entity_obj["map_tile_name"]].keys():
530+
print(
531+
"calling before_move %s, %s, %s"
532+
% (
540533
moving_to_coords,
541534
GAME_STATE["PLAYER_LOC"],
542535
entity_obj,
543-
GAME_STATE,
544-
):
545-
# all the movement if it returned true
546-
can_move = True
547-
else:
548-
# break and don't allow movement if it returned false
549-
break
550-
else: # entity does not have a before_move function
551-
# allow movement
536+
)
537+
)
538+
# call the before_move behavior function act upon it's result
539+
if TILES[entity_obj["map_tile_name"]]["before_move"](
540+
moving_to_coords,
541+
GAME_STATE["PLAYER_LOC"],
542+
entity_obj,
543+
GAME_STATE,
544+
):
545+
# all the movement if it returned true
552546
can_move = True
553-
if can_move:
554-
# set the player loc variable to the new coords
555-
GAME_STATE["PLAYER_LOC"] = moving_to_coords
556-
557-
else: # no entities at the location player is moving to
558-
# set player loc variable to new coords
547+
else:
548+
# break and don't allow movement if it returned false
549+
break
550+
else: # entity does not have a before_move function
551+
# allow movement
552+
can_move = True
553+
if can_move:
554+
# set the player loc variable to the new coords
559555
GAME_STATE["PLAYER_LOC"] = moving_to_coords
560556

561-
# reset movement offset variables
562-
y_offset = 0
563-
x_offset = 0
564-
565-
# set previous button values for next iteration
566-
prev_up = cur_up
567-
prev_down = cur_down
568-
prev_right = cur_right
569-
prev_left = cur_left
570-
571-
# current time
572-
now = time.monotonic()
573-
574-
# if it has been long enough based on FPS delay
575-
if now > last_update_time + FPS_DELAY:
576-
# Set camera to 10x8 centered on the player
577-
# Clamped to (0, MAP_WIDTH) and (0, MAP_HEIGHT)
578-
set_camera_view(
579-
max(
580-
min(
581-
GAME_STATE["PLAYER_LOC"][0] - 4,
582-
GAME_STATE["MAP_WIDTH"] - SCREEN_WIDTH_TILES,
583-
),
584-
0,
557+
else: # no entities at the location player is moving to
558+
# set player loc variable to new coords
559+
GAME_STATE["PLAYER_LOC"] = moving_to_coords
560+
561+
# reset movement offset variables
562+
y_offset = 0
563+
x_offset = 0
564+
565+
# set previous button values for next iteration
566+
prev_up = cur_up
567+
prev_down = cur_down
568+
prev_right = cur_right
569+
prev_left = cur_left
570+
571+
# current time
572+
now = time.monotonic()
573+
574+
# if it has been long enough based on FPS delay
575+
if now > last_update_time + FPS_DELAY:
576+
# Set camera to 10x8 centered on the player
577+
# Clamped to (0, MAP_WIDTH) and (0, MAP_HEIGHT)
578+
set_camera_view(
579+
max(
580+
min(
581+
GAME_STATE["PLAYER_LOC"][0] - 4,
582+
GAME_STATE["MAP_WIDTH"] - SCREEN_WIDTH_TILES,
585583
),
586-
max(
587-
min(
588-
GAME_STATE["PLAYER_LOC"][1] - 3,
589-
GAME_STATE["MAP_HEIGHT"] - SCREEN_HEIGHT_TILES,
590-
),
591-
0,
584+
0,
585+
),
586+
max(
587+
min(
588+
GAME_STATE["PLAYER_LOC"][1] - 3,
589+
GAME_STATE["MAP_HEIGHT"] - SCREEN_HEIGHT_TILES,
592590
),
593-
10,
594-
8,
595-
)
596-
# draw the camera
597-
draw_camera_view()
598-
# if player beat this map
599-
if GAME_STATE["STATE"] == STATE_MAPWIN:
600-
GAME_STATE["MAP_INDEX"] += 1
601-
# if player has beaten all maps
602-
if GAME_STATE["MAP_INDEX"] >= len(MAPS):
603-
GAME_STATE["MAP_INDEX"] = 0
604-
GAME_STATE["STATE"] = STATE_WAITING
605-
load_map(MAPS[GAME_STATE["MAP_INDEX"]])
606-
show_splash(
607-
"You Win \n =D \nCongratulations. \nStart Over?", 0x29C1CF
608-
)
609-
else:
610-
# prompt to start next
611-
GAME_STATE["STATE"] = STATE_WAITING
612-
load_map(MAPS[GAME_STATE["MAP_INDEX"]])
613-
show_splash(
614-
"You beat this level\n =D \nCongratulations. \nStart Next?",
615-
0x29C1CF,
616-
)
617-
# game over from sparky
618-
elif GAME_STATE["STATE"] == STATE_LOST_SPARKY:
591+
0,
592+
),
593+
10,
594+
8,
595+
)
596+
# draw the camera
597+
draw_camera_view()
598+
# if player beat this map
599+
if GAME_STATE["STATE"] == STATE_MAPWIN:
600+
GAME_STATE["MAP_INDEX"] += 1
601+
# if player has beaten all maps
602+
if GAME_STATE["MAP_INDEX"] >= len(MAPS):
619603
GAME_STATE["MAP_INDEX"] = 0
620604
GAME_STATE["STATE"] = STATE_WAITING
621-
game_over_text = (
622-
"Be careful not to \ntouch Sparky unless \n"
623-
"you've collected \nenough Mho's.\nStarting Over"
624-
)
625605
load_map(MAPS[GAME_STATE["MAP_INDEX"]])
626-
show_splash(game_over_text, 0x25AFBB)
627-
628-
# talking to minerva
629-
elif GAME_STATE["STATE"] == STATE_MINERVA:
606+
show_splash(
607+
"You Win \n =D \nCongratulations. \nStart Over?", 0x29C1CF
608+
)
609+
else:
610+
# prompt to start next
630611
GAME_STATE["STATE"] = STATE_WAITING
631-
random_fact = random.choice(FACTS)
632-
minerva_txt = wrap_nicely("Minerva: {}".format(random_fact), 23)
633-
show_splash(minerva_txt, 0xD39AE5, 0)
634-
635-
# store the last update time
636-
last_update_time = now
612+
load_map(MAPS[GAME_STATE["MAP_INDEX"]])
613+
show_splash(
614+
"You beat this level\n =D \nCongratulations. \nStart Next?",
615+
0x29C1CF,
616+
)
617+
# game over from sparky
618+
elif GAME_STATE["STATE"] == STATE_LOST_SPARKY:
619+
GAME_STATE["MAP_INDEX"] = 0
620+
GAME_STATE["STATE"] = STATE_WAITING
621+
game_over_text = (
622+
"Be careful not to \ntouch Sparky unless \n"
623+
"you've collected \nenough Mho's.\nStarting Over"
624+
)
625+
load_map(MAPS[GAME_STATE["MAP_INDEX"]])
626+
show_splash(game_over_text, 0x25AFBB)
627+
628+
# talking to minerva
629+
elif GAME_STATE["STATE"] == STATE_MINERVA:
630+
GAME_STATE["STATE"] = STATE_WAITING
631+
random_fact = random.choice(FACTS)
632+
minerva_txt = wrap_nicely("Minerva: {}".format(random_fact), 23)
633+
show_splash(minerva_txt, 0xD39AE5, 0)
634+
635+
# store the last update time
636+
last_update_time = now

0 commit comments

Comments
 (0)