random to create hidden game elementsinput()if / else to give feedbackEvery video game has hidden logic โ secret levels, random enemies, or treasure locations. Learning to hide and reveal things with code is the first step to making your own games!
These same skills are used in apps, quizzes, and even smart home devices!
import random
ship_row = random.randint(0, 4)
ship_col = random.randint(0, 4)
print("Guess the ship!")
guess_row = int(input("Row (0-4): "))
guess_col = int(input("Col (0-4): "))
if guess_row == ship_row and guess_col == ship_col:
print("๐ฏ HIT! You sank the ship!")
else:
print("๐ฆ MISS!")
print("The ship was at", ship_row, ship_col)
How to create interactive programs that respond to user choices โ the heart of all games!