INST314 Coding

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/23

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

24 Terms

1
New cards

from typing import Dict, Tuple

Imports specific names from a module. Here it pulls Dict and Tuple from typing for type hints.

2
New cards

class GameCharacter:

Defines a class named GameCharacter.

3
New cards

def init( self, character_id: str, resources: Dict[str, Dict[str, float]] | None = None, x_pos: int = 0, y_pos: int = 0, ) -> None:

Defines the constructor for the class with type hints. It sets up character ID, resources, and position.

4
New cards

self.character_id: str = character_id

Creates/assigns the instance attribute character_id with a type hint.

5
New cards

if resources is None:

Checks whether a variable is None to decide whether to set default values.

6
New cards

resources = { "HP": {"current": 3500.0, "max": 3500.0}, "MP": {"current": 100.0, "max": 100.0}, "stamina": {"current": 100.0, "max": 100.0}, }

Creates a dictionary literal of default resources.

7
New cards

self.resources: Dict[str, Dict[str, float]] = {}

Initializes the self.resources dictionary attribute with a type hint.

8
New cards

for name, vals in resources.items():

Starts a for-loop iterating over all resource entries.

9
New cards

cur = float(vals.get("current", 0.0))

Fetches a value from a dictionary with a default and converts it to float.

10
New cards

mx = float(vals.get("max", cur))

Gets the maximum allowed value for the resource, defaulting to current if missing.

11
New cards

cur = max(0.0, min(cur, mx))

Clamps a value within a lower and upper bound using min/max.

12
New cards

self.resources[name] = {"current": cur, "max": mx}

Writes a normalized resource entry into the resources dictionary.

13
New cards

self.inventory: Dict[str, int] = {}

Initializes an empty inventory dictionary with type hints.

14
New cards

def move_up(self) -> None:

Defines a method to move the character up.

15
New cards

self.y_pos += 1

Moves up by increasing the y coordinate.

16
New cards

def move_down(self) -> None:

Defines a method to move the character down.

17
New cards

self.y_pos -= 1

Moves down by decreasing the y coordinate.

18
New cards

def move_left(self) -> None:

Defines a method to move the character left.

19
New cards

self.x_pos -= 1

Moves left by decreasing the x coordinate.

20
New cards

def move_right(self) -> None:

Defines a method to move the character right.

21
New cards

self.x_pos += 1

Moves right by increasing the x coordinate.

22
New cards

if num_items < 1:

Validates input; returns early if the quantity is less than 1.

23
New cards

self.inventory[item] = self.inventory.get(item, 0) + int(num_items)

Increments an item's count in the inventory, using 0 when the key is missing.

24
New cards

if name == "__main__":

Main-guard: runs the script’s main() only when executed directly.