PYTHON
Welcome to my basic games!
We have learnt to code with python.
Input 1
This is a code for guess which is my brother's name using python:
# This order the computer to write the phrase that is in quotes.
print("Which is your brother's name:")
# This means that what you write has to be a word.
x = input()
# This order the computer that if you say the thing are in quotes the computer answer and say again Eric.
print("Eric", x)
# This say that if the thing that you write is the word that is in quotes.
if x == "Eric":
# The computer ask the sentence that is in quotes.
print("I know him, he's a very good boy")
This is a videotutorial showing how to create a basic game named input01.py:
Input 2
This is a code to know in which continents have you gone using python.
# This order the computer to write the phrase that is in quotes.
print("In where continents have you gone?:")
# This means that what you write has to be a word.
x = input()
# The part in quotes is the begginig of the sentence and the x is the answer that is added depending on the continent.
print("I have been", x)
# If the word entered by the user is the word that is in quotes.
if x == "America":
# The computer write the sentence that is in quotes.
print("I love America, which is your favorite city in America?")
# If the word entered by the user is the word that is in quotes.
elif x == "Asia":
# The computer write the sentence that is in quotes.
print("I gone there once, the food there is really diferent")
# If the word entered by the user is the word that is in quotes.
elif x == "Oceania":
# The computer write the sentence that is in quotes.
print("I want to go there, which part do you recommend I go to?")
# If the word entered by the user is the word that is in quotes.
elif x == "Africa":
# The computer write the sentence that is in quotes.
print("I would like to go to see the animals, which has been the animal that you liked the most there?")
# If the word entered by the user is the word that is in quotes.
elif x == "Europa":
# The computer write the sentence that is in quotes.
print("I live in Europa, the next time call me and you come to my house")
# The computer say another thing if you ask another option.
else:
# The computer ask this sentence that is in quotes.
print("You need o know that the continents are", x)
This is a videotutorial showing how to create a basic game named input02.py:
Math 1
This is a code to make a math operation using python.
# Prepares the computer to understand and generate random numbers
import random
# Creates 2 integer random variables (int is integer = nº integer)
#17.66 is the random maximum, 12.98 is the minimum of the first number that appears in the operation
n = round(random.uniform(17.66, 12.98), 2)
# 9.53 is the random maximum, and 3.45 is the random minimum of the second number that appears in the operation
m = round(random.uniform(3.45, 9.53), 2)
# Appears in quotation marks and variables
print("What is", n, "minus", m, "?")
# Input is keyboard input-> int integer #str means string (word) # float = would be decimals
x = float(input())
# If (if) what the user writes is equal to the rest of the two
if x == n - m:
# Random numbers (n-m) type the word "Correct ..." on the screen
print("Correct, you are genious")
# Else = otherwise incorrect
else:
# The computer ask this sentence that is in quotes.
print("Wrong, try again")
This is a videotutorial showing how to create a basic game named math1.py:
Math 2
This is a code to makes maths operations using python.
# Prepares the computer to understand and generate random numbers.
import random
# The initial puntuation is zero.
score = 0
# Show in the screen this question.
print("What is 13+6 ?")
# If (if) what the user write need to be an integrer number.
g = int(input())
# If the user write the number 19.
if g == 19:
# Show in the screen this answer if is correct.
print("Correct, you are good addend")
# The computer add one point to the puntuation.
score = score + 1
# Show in the screen this question.
print("What is 72-68 ?")
# If (if) what the user write need to be an integrer number.
g = int(input())
# If the user write the number 4.
if g == 4:
# Show in the screen this answer if is correct.
print("Correct, you are smart ")
# The computer add one point to the puntuation.
score = score + 1
# Show in the screen this question.
print("What is 15·3 ?")
# If (if) what the user write need to be an integrer number.
g = int(input())
# If the user write the number 45.
if g == 45:
# Show in the screen this answer if is correct.
print("Correct, you are on a roll")
# The computer add one point to the puntuation.
score = score + 1
# Show in the screen this question.
print("What is 90:9")
# If (if) what the user write need to be an integrer number.
g = int(input())
# If the user write the number 10.
if g == 10:
# Show in the screen this answer if is correct.
print("Correct, you are very inteligent")
# The computer add one point to the puntuation.
score = score + 1
# Show the final puntuation.
print("Your score:",score)
This is a videotutorial showing how to create a basic game named math2.py:
Guess 1
This is a code for guess which number I'm thinking using python.
# Prepares the computer to understand and generate random numbers.
import random
# n is a integer number from 48 to 58.
n = random.randint (48, 58)
# Do the following code all the time counting up guesses until break condition.
while True:
# Show in the screen this question.
print("I am thinking of a number, between 48 and 58 can you guess what it is?")
# g is a number entered by the user
g = int(input())
# If the number entered by the user is equal to the random number the code stops.
if g == n:
# Break means stop the code when the previous condition is met.
break
# If the user doesn't match the number.
else:
# The computer show the sentence in quotes if the user doensn't match the number.
print("Wrong, think another number and try again")
# The computer show the sentence in quotes if the user match the number.
print("Correct, that is the number that I think")
This is a videotutorial showing how to create a basic game named guess1.py:
Guess 2
This is a code to guess the number I'm thinking using python
# Random is a python library to create random numbers.
import random
# n is a integer number from 15 to 150.
n = random.randint(15, 150)
# Guesses is a variable with initial value 0.
guesses = 0
# Do the following code all the time counting up guesses until break condition.
while True:
# Add a guess every time I try to answer.
guesses = guesses + 1
# Show in the screen this question.
print("I am thinking of a number, between 15 and 150 can you guess what it is?")
# g is a number entered by the user (input().
g = int(input())
# If the number entered by the user is equal to the random number the code stops.
if g == n:
# Break means stop the code when the previous condition is met.
break
# Elif means else if means if also happens this.
elif g < n:
# If the number entered by user is minor than the random number tell the user "this number is too low, try a high number".
print("This number is too low, try a high number")
elif g > n:
# If the number ntered by user is more than the random number tell the user "this number is too high, try to say a low number".
print("This number is too high, try to say a low number")
# If the number entered by the user is correct tell the user "Correct!...".
# Tell the user also the number of attempts.
print("Correct! That's the number I was thinking about", guesses, "attempts you try.")
This is a videotutorial showing how to create a basic game named guess2.py:
Pong
This a pygame zero game to ensure that the ball doesn't fall, moving apart underneath.
# This is for know the width of the screen.
WIDTH = 800
# This is for know the height of the screen.
HEIGHT = 500
# This is for know the measurements of the ball.
ball = Rect ((150, 400), (17, 15))
# This is for know the measurements of the bat.
bat = Rect((200, 480), (100, 30))
# This controls the velocity of the ball.
vx = 8
# This controls the velocity of the bat
vy = 8
# Def is a function. This is a function and appears when you put parentheses.
def draw():
# This number is the screen background color.
screen.fill ((128,0,0))
# This is for the color of the ball and you put the color in quotes.
screen.draw.filled_rect(ball, "blue")
# This is for the color of the bat and you put the color in quotes.
screen.draw.filled_rect(bat, "pink")
# Def is a function. This is a function and appears when you put parentheses.
def update():
# It's a global variable.
global vx, vy
# You add in x the velocity to the x ball position to move it.
ball.x += vx
# You add in y the velocity to the y ball position to move it.
ball.y +=vy
# If the ball surpass the width of the screen or exits on the left side.
if ball.right > WIDTH or ball.left < 0:
# Changes the sing of the velocity and changes the direction too.
vx = -vx
# If the ball collides with the bat or the ball exits up because it's less than 0.
if ball.colliderect(bat) or ball.top < 0:
# hanges the sing of the velocity and changes the direction too.
vy = -vy
# If the ball doesn't touch the bat.
if ball.bottom > HEIGHT:
# The game ends.
exit()
# If in the keyboard I type the arrow in the right.
if(keyboard.right):
# The bat moves 10 pixles right.
bat.x += 10
# If in the keyboard I type the arrow in the left.
elif(keyboard.left):
# The bat moves 10 pixles left.
bat.x -= 10
This is a videotutorial showing how to create a basic game named pong.py:
Game 1
This is the python code using pygame zero:
# Playa is a sprite, that is, a moving image in a game.
# Playa is the name of a Class sprite (Alicia is an example of the class Homo sapiens), this means create an object from a class.
# 'playa' is a name of a image in the folder images.
playa = Actor('playa')
# Topright means to locate the image in the top right corner.
playa.topright = 0,10
# Screen width based on the width of the playa.
WIDTH = 500
# Height based on the playa height + 20.
HEIGHT = playa.height + 20
# Def is a function. This is a function and appears when you put parentheses.
def draw():
# Put all the screen black.
screen.clear()
# Draw the image in the screen.
playa.draw()
# Def is a function. This is a function and appears when you put parentheses.
def update():
# Number of the velocity of the image.
playa.left += 4
# If the image left is bigger than the width.
if playa.left > WIDTH:
# The puntuation is zero.
playa.right = 0
This is a videotutorial showing how to create a basic game named game1.py:
Game 2
This is a pygame zero game to see how the image pass.
# Verde1 is a sprit. 'verde1' is a name of a image in the folder images.
verde1 = Actor('verde1')
# Topright means to locate the image in the top right corner.
verde1.topright = 0, 10
# Screen width based on the width of the verde1.
WIDTH = 500
# Height based on the verde1 height + 20.
HEIGHT = verde1.height + 20
# Def is a function. This is a function and appears when you put parentheses.
def draw():
# Put all the screen black.
screen.clear()
# Draw the image in the screen.
verde1.draw()
# Def is a function. This is a function and appears when you put parentheses.
def update():
# This is the speed of the passing image.
verde1.left += 8
# If the image left is bigger than the width.
if verde1.left > WIDTH:
# The puntuation of the image is zero.
verde1.right = 0
# The puntuation start with zero points.
score = 0
# Def is a function. This is a function and appears when you put parentheses.
def on_mouse_down(pos):
# The computer start counting your points.
global score
# If you touch the image.
if verde1.collidepoint(pos):
# You win one point.
score += 1
# If you don't touch the image.
else:
# You lose two points.
score -= 2
# The computer write the sentence in quotes.
print("There is no photo here, you lost two points")
# The computer write your puntuation.
print(score)
This is a videotutorial showing how to create a basic game named game2.py:
Game 3
This is a pygame zero game to see how the image pass and if you toch the image, you win a point.
# rosalia is a sprit. 'rosalia' is a name of a image in the folder images.
rosalia = Actor('rosalia')
# Topright means to locate the image in the top right corner.
rosalia.topright = 0, 10
# Screen width based on the width of the rosalia.
WIDTH = 500
# Height based on the rosalia height + 20.
HEIGHT = rosalia.height + 20
# Def is a function. This is a function and appears when you put parentheses.
def draw():
# Put all the screen black.
screen.clear()
# Draw the image in the screen.
rosalia.draw()
# Def is a function. This is a function and appears when you put parentheses.
def update():
# This is the speed of the passing image.
rosalia.left += 6
# If the image left is bigger than the width.
if rosalia.left > WIDTH:
# The puntuation of the image is zero points.
rosalia.right = 0
# The puntuation start with zero points.
score = 0
# Def is a function. This is a function and appears when you put parentheses.
def on_mouse_down(pos):
# The computer start counting your points.
global score
# If you touch the image.
if rosalia.collidepoint(pos):
# Sounds this sound.
sounds.epa.play()
# The image change to the image 'rosalia_hurt'.
rosalia.image = 'rosalia_hurt'
# You win one point.
score += 1
# If you don't touch the image.
else:
# You lose two points.
score -= 2
# The computer write the sentence in quotes.
print ("Try again, there's nothing here!")
# The computer write your puntuation.
print(score)
This is a videotutorial showing how to create a basic game named game3.py:
Game 4
This is a pygame zero game to se the image pass and if you touch the image, this image change and you hear a sound.
# bro is a sprite, that is, a moving image in a game.
# bro is the name of a Class sprite.
# 'bro' is a name of a image in the folder images.
bro = Actor ('bro')
# Topright means to locate the image in the top right corner.
bro.topright = 0,10
# Screen width based on the width of the bro.
WIDTH = 700
# Height based on the playa height + 10.
HEIGHT = bro.height + 10
# Def is a function. This is a function and appears when you put parentheses.
def draw():
# Put all the screen black.
screen.clear()
# Draw the image in the screen.
bro.draw()
# Def is a function. This is a function and appears when you put parentheses.
def update():
# Number of the velocity of the image
# This is the speed of the passing image.
bro.left += 8
# If the image left is bigger than the width.
if bro.left >WIDTH:
# The puntuation of the image is zero points.
bro.right = 0
# The puntuation start with zero points.
score = 0
# Def is a function. This is a function and appears when you put parentheses.
def on_mouse_down(pos):
# The computer start counting your points.
global score
# If you touch the image.
if bro.collidepoint(pos):
# The bro sticks.
set_bro_hit()
# If you touch the image you win a point.
score += 1
# The computer write your puntuation.
print(score)
# If you don't touch the image.
else:
# If you don't touch the image you lose two points.
score -= 2
# The computer start counting your points.
print(score)
# Def is a function. In this case appers when the image change.
def set_bro_hit():
# If you touch the first image, appers the second image named bro_hurt.
bro.image = 'bro_hurt'
# At the same time that the image change, appers a sound.
sounds.gol1.play()
# Time that the second image lasts.
clock.schedule_unique(set_bro_normal, 2.5)
# Def is a function.In this case appers when everything returns to the start.
def set_bro_normal():
bro.image = "bro"
This is a videotutorial showing how to create a basic game named game4.py:
Game 5
This is a pygame zero where the star goes down and you have to get it to reach the tree.
# Prepares the computer to understand and generate random numbers.
import random
# Is the number of width from the screen.
WIDTH = 800
# Is the number of height from the screen.
HEIGHT = 600
# arbol6 is a sprit. 'arbol6' is a name of a image in the folder images. This image appers in thee middle of the width.
arbol6 = Actor('arbol6', midbottom=(WIDTH // 2, HEIGHT))
# estrella3 is a sprit. 'estrella3' is a name of a image in the folder images.
estrella3 = Actor ('estrella3')
# Topright means to locate the image in the top right corner.
estrella3.topright = 100,10
# Def is a function. This is a function and appears when you put parentheses.
def draw():
# In quotes you put the color that you want in the screen.
screen.fill('firebrick')
# Draw the image in the screen.
estrella3.draw()
# Draw the image in the screen.
arbol6.draw()
# Between these two numbers appear random numbers that determine the position of the first image that is "falling".
xpos = random.randint(0, 800)
# Def is a function. This is a function and appears when you put parentheses.
def update():
# Is a random position.
global xpos
# The position of the star3 in y moves 5 pixels down.
estrella3.y += 5
# When you move the estrella3 to the left.
if keyboard.left:
# The position of star3 in x, move 5 pixels to the left.
estrella3.x -= 5
# When you move the estrella3 to the right.
if keyboard.right:
# The position of estrella3 in x, move 5 pixels to the righ.
estrella3.x += 5
# If you join the star to the tree.
if arbol6.colliderect(estrella3):
# The computer write the sentence in quotes.
print('you win, you have joined the star with the tree ')
# y coordinate of the object estrella3 is zero means it is located in the upper part of the screen.
estrella3.y = 0
# Position of a random number between 0 and 800.
xpos = random.randint(0, 800)
# The position of the estrella3 is the random number of the above.
estrella3.x = xpos
# If the estrella3 is bigger than 600.
if estrella3.y > 600:
# The computer write the sentence in quotes.
print("you lose, you have not joined the star with the tree")
# y coordinate of the object estrella3 is zero means it is located in the upper part of the screen.
estrella3. y = 0
# Is a random position between 0 and 800.
xpos = random.randint(0, 800)
# The estrella5 is placed in a random position.
estrella3.x = xpos
# Def is a function. This is a function and appears when you put parentheses.
def on_mouse_down(pos):
# The computer start counting your points.
global score
# If you put the image together.
if estrella3.collidepoint(pos):
# The estrella3 sticks.
set_estrella3_hit()
# You win a point.
score += 1
# The computer write your puntuation.
print(score)
# If the above does not happen.
else:
# You lose a point.
score -= 1
# The computer write your puntuation.
print(score)
# Def is a function. This is a function and appears when you put parentheses.
def set_estrella5_hit():
# la imagen de estrella3 que tengo guardada en el ordenador aparece como la imagen que hay en el documento.
estrella3.image = 'estrella3'
# Sounds this sound.
sounds.eep.play()
# Time that the image lasts.
clock.schedule_unique(set_estrella3_normal, 0.5)
# Def is a function.In this case appers when everything returns to the start.
def set_estrella5_normal():
# This is for when the star meets the tree.
estrella3.image = "arbol6"
This is a videotutorial showing how to create a basic game named game5.py:
Game 6
This is a pygame zero game in which you have to touch the mosquito.
# Prepares the computer to generate random numbers in different positions.
from random import randint
# mosquito1 is a sprit. 'mosquito1' is a name of a image in the folder images.
mosquito1 = Actor ("mosquito1")
# Def is a function. This is a function and appears when you put parentheses.
def draw():
# Put all the screen black.
screen.clear()
# Draw the image in the screen.
mosquito1.draw()
# Def is a function. This is a function and appears when you put parentheses.
def shoot_mosquito1():
# The random numbers indicate the position of the mosquito, in the width form.
mosquito1.x = randint(10, 400)
# The random numbers indicate the position of the mosquito, in the height form.
mosquito1.y = randint(10, 200)
# Def is a function. This is a function and appears when you put parentheses.
def on_mouse_down(pos):
# If (if) the user shoot the mosquito1.
if mosquito1.collidepoint(pos):
# If the above action is fulfilled, the computer tells you the text in parentheses and in quotation marks
print("Good shot, you kill the mosquito!")
# The computer repositiones the mosquito and moves it to a random number
shoot_mosquito1() #indica a la línia 6 que hde moure la posició del mosquito1
# If you don't kill the mosquito1.
else: #si no succeix l'acció de dalt
# The computer write the sentence in quotes.
print("You missed, you don't kill the mosquito. Try again!") #l'ordinador et diu la frase que està dins els parèntesis entre cometes
# The game ends.
quit()
This is a videotutorial showing how to create a basic game named game6.py: