Skip to content

Instantly share code, notes, and snippets.

View Pinacolada64's full-sized avatar

Ryan Sherwood Pinacolada64

View GitHub Profile
@Pinacolada64
Pinacolada64 / scratch_19.py
Created November 7, 2022 22:19
try/catch, random.choice
import random
def ask_name():
is_ok = False
while is_ok is False:
my_name = input("What is your name? ")
if my_name.lower() == "michelle":
print("You answered correctly!")
return my_name.title()
else:
@Pinacolada64
Pinacolada64 / scratch_15.py
Last active May 18, 2022 05:01
doctest test
import doctest
import logging
def add_two_numbers(a, b):
"""
Add two numbers, a and b, together. Return the total.
:param a: first number to add
:param b: second number to add
C:\Users\ryan-\AppData\Local\Programs\Python\Python38\python.exe "D:\Program Files\JetBrains\PyCharm Community Edition 2020.3.2\plugins\python-ce\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 56514 --file D:/Documents/C64/TADA/server/client.py
Connected to pydev debugger (build 213.6777.50)
client connected (localhost:5000)
Welcome to:
Totally
Awesome
Dungeon
Adventure
Please log in.
user? >? a
@Pinacolada64
Pinacolada64 / map_file_2.py
Created February 19, 2022 22:58
Trying to read in items
#!/bin/env python3
# + encode map data as JSON. why:
# + don't need to write own parser,
# + flexible for adding/changing fields (including optional fields)
# + use 'dataclass' for Room. why:
# + convenient for class with many fields mostly stored as data
# + use 'textwrap' for formatting multiline text. why:
# + can store text without all the format
@Pinacolada64
Pinacolada64 / player.json
Created January 11, 2022 04:52
Player JSON data
{
"id": "x",
"name": "Mr. X",
"map_level": 1,
"room": 1,
"money": 1000,
"health": 100,
"xp": 100,
"flag": {
"room_descs": true,
@Pinacolada64
Pinacolada64 / print_age.py
Created December 15, 2021 22:05
is printing 'age' property with __str__ possible?
class Player(object):
def __init__(self, age: int):
self.age = age
def print_age(self):
# FIXME: can calling p.print_age() be simplified by instead using a __str__ method?
if self.age == 0:
return 'Unknown'
else:
return f'{self.age} years'
@Pinacolada64
Pinacolada64 / text-adventure-demo.py
Created November 28, 2021 21:11
Modification of Al Swiegart's adventure game demo
import cmd
import textwrap
import logging
import pyreadline3 # for tab-completion and command line editing
# PyCharm had me rename worldItems -> world_items, but camelCase variables are legal in Python
# there is an option in the IDE to ignore camelCase variables, so I set that
# class design resources:
# https://codereview.stackexchange.com/questions/91069/simple-text-rpg-in-python
@Pinacolada64
Pinacolada64 / net_server.py
Created November 24, 2021 22:25
Trying to update `net_server.py` to read json files
#!/bin/env python3
import socketserver
import json
from dataclasses import dataclass, field
import enum
import net_common as nc
@Pinacolada64
Pinacolada64 / text-based-rpg.py
Created November 21, 2021 01:30
Trying to fix someone's Python 2.x RPG
# A text based RPG
# from http://pythonfiddle.com/text-based-rpg-code-python/
# ryan made a few IDE-suggested tweaks:
# 'if IsDaggerEquipped == True:' -> 'if IsDaggerEquipped:'
# Import required modules
import jsonpickle # FIXME: this does not exist on python 3.x, so causes an error when used
import os
import sys
import time
@Pinacolada64
Pinacolada64 / read_map.py
Last active November 14, 2021 05:00
Reading map data from a file
class Exit(object):
def __init__(self, room_number: int, exit_list: list):
"""
Exits are contained in a list, exit_list
:param room_number: this Exit's room number
:param exit_list: list of 5 exits per room: north, east, south, west, room_connection, room_transport
"""
self.room_number = room_number
self.exits = exit_list
logging.info(f'Exit.__init__: created new exit: {room_number=} {exit_list=}')