0% found this document useful (0 votes)
36 views8 pages

Python Notes For Text Game Creation Leading To PyGame

This document provides a comprehensive overview of Python programming concepts, including data types such as integers, floats, booleans, strings, lists, tuples, and dictionaries. It covers control flow structures like if statements and loops, as well as the implementation of functions and classes, including inheritance. Key takeaways highlight the importance of collections, control flow, functions, and object-oriented programming in Python.

Uploaded by

dimitriluger
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views8 pages

Python Notes For Text Game Creation Leading To PyGame

This document provides a comprehensive overview of Python programming concepts, including data types such as integers, floats, booleans, strings, lists, tuples, and dictionaries. It covers control flow structures like if statements and loops, as well as the implementation of functions and classes, including inheritance. Key takeaways highlight the importance of collections, control flow, functions, and object-oriented programming in Python.

Uploaded by

dimitriluger
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

# Python Notes

Alison
# Integers and floats

x_positon = 10
print(x_positon)

x_positon = 15
print(x_positon)

pi = 3.14
pi = 3.14159
print(pi)

x_positon = 15.0
print(x_positon)
print(type(x_positon))

# Booleans and strings

is_game_over = False
is_game_over = True

print(is_game_over)
print(type(is_game_over))

is_game_over = 1
print(is_game_over)
print(type(is_game_over))

name = "Nimish"
is_game_over_text = "False"
age_as_a_string = "5"
print(name)
print(type(name))

age = 27
name_and_age = "Nimish: {}".format(age)
print(name_and_age)

# Arithmetic Operators
# + - * / % // ** =

x_position = 10
forward = x_position + 1
print(forward)
backward = x_position - 1
print(backward)

remainder = 5 % 2
print(remainder)
floor_division = 5 // 2
print(floor_division)

five_squared = 5 ** 2
print(five_squared)
#x_position = x_position + 1
x_position += 1
print(x_position)

first_name = "Nimish "


last_name = "Narang"
print(first_name + last_name)

# Operators conditional and logical


# > >= < <= == != not or and

x_position = 1
end_position = 10

is_at_end = x_position == end_position


print(is_at_end)
is_at_halfway = x_position >= end_position / 2
print(is_at_halfway)

print(not is_at_end )
not_is_at_end = not is_at_end

score = 10
is_game_over = score >= 10 and is_at_end
print(is_game_over)

score = 9
is_game_over = score >= 10 or is_at_end
print(is_game_over); print("Hello") # one line can contain multiple statements with
semicolon

#Collections - Lists

name = [5, True, "string"]


enemy_positions = [5, 10, 15]
enemy_positions = [5, 10, 15, 20]
print(enemy_positions)

total_enemies = len(enemy_positions)
print(enemy_positions[0])

enemy_positions[0] = 6
print(enemy_positions)
print(enemy_positions[0:(total_enemies-1)]) #colon used to provide range

enemy_positions.append(25)
print(enemy_positions)

enemy_positions.insert(1,9)
print(enemy_positions)

enemy_positions.remove(6) #removes the first occurence of the value


print(enemy_positions)

del(enemy_positions[2]) #removes the value at the index


print(enemy_positions)
# Collections - Tuples ()

high_score = ("Nimish", 120)


print(high_score)

high_score = ("Holly", 150)


print(high_score)

# high_score[0] = "fadsfsd" #immutable


name = high_score[0]
print(name)

print(len(high_score))

print("Holly" in high_score) #bool element on left is in the collection on right

print(name[0]) #strings are technically immutable collections


print(name[0:2])
print("Hol" in name)
print(len(name))

# Collections - Dictionaries

#dict = {key: value, key: value}


actions = {"r":1, "l":-1}
print(actions)

print(actions["r"]) #dictionaries are categorized by keys


print(actions.get("g")) #.get() is used to retrieve a value from a dictionary

actions["r"] = 2 #update the value of a key


actions["u"] = 1 #add a new key-value pair
print(actions)

print(actions.keys()) #returns a list of keys


print(actions.values()) #returns a list of values
print(actions.items()) #returns a list of tuples

del(actions["u"]) #deletes a key-value pair


print(actions)

actions.pop("r") #deletes a key-value pair


print(actions)

print("l" in actions) #checks if a key exists in a dictionary

# Control Flow - If Statements


# Control flow is checking a condition and executing code if that condition is
true.

# if condition = True:
# execute some code
# execute some other code
# execute some more code

key = "r"
if key == "r": # can also use logic and / or operators or any other comparison
operators
print("move right")
elif key == "l":
print("move left")
else:
print("invalid key")

print("done")

# Control Flow - While Loops


# Similar to if statements, while loops execute a block of code while a condition
is true.

# while condition == true:


# execute some code multiple times

position = 0
end_position = 10
enemy_position = 13

while position < end_position: #while loop will continue to execute until the
condition is false
position += 1 #position = position + 1
print("You weigh {}".format(position))
if enemy_position - position <= 5:
print("An enemy lurks at {}".format(enemy_position))
if position == enemy_position:
print("The enemy has already eaten all the food!")
break #break statement will exit the loop
elif position == end_position and enemy_position != end_position:
print("Your feast awaits!")
break
if position % 2 != 1 and enemy_position != end_position: #modulo operator checks
for remainder
enemy_position -= 1
# continue - skips to the next iteration of the loop

# Control Flow - For In Loops


position = 0
end_position = 10
enemy_position = 14
food_remaining = 10
enemy_food = 0
your_food = 0

while food_remaining > 0: #while loop will continue to execute until the condition
is false
if position != end_position:
position += 1 #position = position + 1
print("You are {} spaces away from your food".format(end_position-position))
if position % 2 != 1 and enemy_position != end_position: #modulo operator checks
for remainder
enemy_position -= 1
if food_remaining <= 0:
print("The enemy has already eaten all the food!")
if enemy_position - end_position <= 5:
print("An enemy is {} spaces away from your food!".format(enemy_position-
end_position))
if enemy_position == end_position:
food_remaining -= 1
enemy_food += 1
print("The enemy has eaten {} of your food. You have {}
remaining.".format(enemy_food, food_remaining))
#break statement will exit the loop
if position == end_position and enemy_position != end_position:
print("Your feast awaits!")
break
if food_remaining > 0 and position == end_position:
print("You have {} food remaining.".format(food_remaining))
food_remaining -= 1
your_food += 1
if your_food > 5:
your_food += 2
food_remaining -= 2
elif your_food > 10:
your_food += 3
food_remaining -= 3
elif your_food > 20:
your_food += 4
food_remaining -= 4
elif your_food > 40:
your_food += 5
food_remaining -= 5
elif your_food > 80:
your_food += 10
food_remaining -= 10
elif your_food > 100:
your_food += 20
food_remaining -= 20
elif your_food > 200:
your_food += 50
food_remaining -= 50
elif your_food > 500:
your_food += 100
food_remaining -= 100
print("You have consumed {} units of your food.".format(your_food))
if position == end_position or enemy_position == end_position:
print("You have eaten {} units of food and your enemy has eaten {} units of
your food.".format(your_food, enemy_food))
if food_remaining <= 0 and your_food >= 2 * enemy_food:
print("Eat them all!")
your_food = your_food + enemy_food * 2
print("You weigh {} units.".format(your_food*2))
# continue - skips to the next iteration of the loop

#Control Flow - For In Loops


#for item in collection:
# execute code using that item
enemy_positions = [5, 10, 15]
for enemy_position in enemy_positions:
print(enemy_position)

for i in range(0,5):
print("Hello")
#Functions - Implementing and Calling Functions
# Functions - A collection of code that performs a specific task.
# Functions - Can be called and executed at any time.
# Functions - Can be called multiple times.
# Functions - Can be called with different arguments.
# Functions - Can return data to be stored or output.
# Functions - Can return multiple values.

#def function_name():
# print("Hello World")

position = 0

def move_playert1():
global position
position += 1
print(position)

move_playert1()

#Functions - Parameters and Return Values

position = 0

def move_player(position, by_ammount):


position += by_ammount
return position

position = move_player(position, 5) #function call


print(position)

#Objects and Classes - Creating a Class and an Object


# objects are entities in code with related variables and functions
# classes are blueprints for objects - defining the variables and functions

# a player object would have:


# variables for health, damage, move speed, etc.
# functions for movement, attacking, etc.

#class ObjectName:
# variable = value
# def function_name(self):
# print("Hello World")
#
#object_name = ObjectName()
#object_name.function_name()

#Objects and Classes - Adding Functions and References

#class GameObject:
# def __init__(self, name, x_pos, y_pos): #initializer function, self refers to
the object
# self.name = name
# self.x_pos = x_pos
# self.y_pos = y_pos
# def move(self, by_x_amount, by_y_amount):
# self.x_pos += by_x_amount
# self.y_pos += by_y_amount

#game_object = GameObject("Enemy", 1, 2) #creates an object of type GameObject


#print(game_object.name)
#game_object.name = "Enemy 1"
#print(game_object.name)

#print(game_object.x_pos)
#print(game_object.y_pos)

#game_object.move(5, 10)
#print(game_object.x_pos, game_object.y_pos)

#other_game_object = GameObject("Player", 2, 0)
#print(other_game_object.name)
#print(other_game_object.x_pos)
#print(other_game_object.y_pos)

#other_game_object.move(5, 10)
#print(other_game_object.x_pos, other_game_object.y_pos)

#one_int = 5
#another_int = one_int
#print(one_int)
#print(another_int)

#another_int = 10
#print(one_int)
#print(another_int)

#other_game_object = game_object
#print(other_game_object.name)

#other_game_object.name = "new name"


#print(other_game_object.name)
#print(game_object.name)

#Objects and Classes - Inheritance

class GameObject:

def __init__(self, name, x_pos, y_pos): #initializer function, self refers to the
object
self.name = name
self.x_pos = x_pos
self.y_pos = y_pos

def move(self, by_x_amount, by_y_amount):


self.x_pos += by_x_amount
self.y_pos += by_y_amount

class Enemy(GameObject): #inherits from GameObject class


def __init__(self, name, x_pos, y_pos, health):
super().__init__(name, x_pos, y_pos) #super() refers to the parent class
self.health = health
def take_damage(self, amount):
self.health -= amount

game_object = GameObject("Game object", 1, 2)


enemy = Enemy("Enemy", 5, 10, 100)

print(game_object.name)
print(enemy.name)

enemy.move(5, 10)
print(enemy.x_pos, enemy.y_pos)

enemy.take_damage(20)
print(enemy.health)

#Lesson Summary
#Below you will find a summary of the key content covered during the module:

# Lists are used to store multiple items in a single variable. Lists are one of
4 built-in data types in Python used to store collections of data, the other 3 are
Tuples, Sets, and Dictionaries, all with different qualities and usage.
# Lists are created using square brackets.
# A list with strings, integers, and boolean values: list1 = ["abc", 34, True,
40, "male"].

# Here are four collection data types in the Python programming language: A list
is a collection that is ordered and changeable.
# A tuple is a collection that is ordered and unchangeable. It allows duplicate
members. A Set is a collection that is unordered, unchangeable, and unindexed. No
duplicate members. A dictionary is a collection that is ordered and changeable. No
duplicate members.
# A program's control flow is the order in which the program's code executes.
The control flow of a Python program is regulated by conditional statements, loops,
and function calls.

# A function is a block of code that only runs when it is called. You can pass
data, known as parameters, into a function. A function can return data as a result.

# Python is an object-oriented programming language. Almost everything in Python


is an object, with its properties and methods. A Class is like an object
constructor or a "blueprint" for creating objects.

You might also like