Welcome to my basic games!

we have learnt to code with python

Input 1

What's your name?

This is the python code using python 3:


 print("Enter your name:")                      # This instructs the computer to write the words that are in quotes.
x = input()                                     # This allows you to type or answer a question.
print("Hello", x)                               # The name you have written will appear in the place of the x.
if x == "Enric":                                # This says if what you write is the word that is in quotes.
    print("Oh! That's a beautiful name! ")      # If the name previously written is "Enric" he answers what is in quotes.
else:                                           # This line include any answer other than "Enric".
    print("Meh, Enric is better.")              # If the name previously written isn't "Enric" he answers what is in quotes.

Input 2

What's your continent?

This is the python code using python 3:


 print("Enter your continent")                  # This instructs the computer to write the words that are in quotes.
x = input()                                     # This allows you to type or answer a question.
print("Wow I've never been to", x)              # This line answers what is written in quotes and the x is the answer that is added according to the continent.
if x == "Europe":                               # If the "x" is Europe, one answer or another will come out.
    print("Europe, good continent")             # The computer answers what is in quotes.
elif x == "America":                            # If the "x" is America, one answer or another will come out.
    print("America, beautiful continent")       # The computer answers what is in quotes.
else:                                           # This line include any answer other than "Europe", or "America".
    print("Wow this continent is incredible")   # If the name previously written isn't "Europe"or "America" he answers what is in quotes.
 

Math 1

How much is "x" plus 11?

This is the python code using python 3:


 import random                                  # Prepares the computer to understand and generate "random numbers" n random
                                                # Create 2 integer random variables (int is integer)
n = random.randint(0,50)                        # 0 is the random min, 50 is the maximum

print("What is", n, "plus 11?")                 # What is in quotes appears in the answer and the n belongs to the random number generated previously
x = int(input())                                # Forces to write a whole number
if x == n + 11:                                 # if x is equal to the previously generated number plus 11...
    print("Correct")                            # ... type the word "Correct" on the screen
else:                                           # This line include any answer other than "n + 11"
    print("Wrong")                              # Type the word "Wrong" on the screen
 

Math 2

Math exam

This is the python code using python 3:


 score = 0                                      # The puntuation with which it starts is equal to 0

print("What is 64/8 ?")                         # Type the question "What is 64/8?" on the screen
g = int(input())                                # Forces to write the answer with a whole number
if g == 8:                                      # If the answer is equal to 8...
    print("Correct")                            # ... type the word "Correct" on the screen
    score = score + 1                           # ... the puntuation add 1

print("What is 7·9 ?")                          # Type the question "What is 7·9?" on the screen
g = int(input())                                # Forces to write the answer with a whole number
if g == 63:                                     # If the answer is equal to 63...
    print("Correct")                            # ... type the word "Correct" on the screen
    score = score + 1                           # ... the puntuation add 1

print("What is 54-32 ?")                        # Type the question "What is 54-32?" on the screen
g = int(input())                                # Forces to write the answer with a whole number
if g == 22:                                     # If the answer is equal to 22...
    print("Correct")                            # ... type the word "Correct" on the screen
    score = score + 1                           # ... the puntuation add 1
print("Your score:", score)                     # Show the final puntuation
 

Guess 1

What number am i thinking of 1-15?

This is the python code using python 3:


n = random.randint(0,15)                        # 0 is the random min, 15 is the maximum

while True:                                     
    print("I am thinking of a number between 0 and 15, can you guess what it is?")    # Type the phrases in quotes
    g = int(input())                            # Forces to write the answer with a whole number
    if g == n:                                  # If the answer is equal to the random number generated
        break                                   # If the previous line is fulfilled this makes the code terminate
    else:                                       # This line include any answer other than "n"
        print("Wrong, try again")               # if is other number than "n" type "Wrong, try again"
print("Very good!")                             # if the number is correct type "Very good!"
 

Guess 2

How many dollars i have?

This is the python code using python 3:


import random                                   #  random is a python library to create random numbers

n = random.randint(0, 1000)                     # 0 is the random min, 1000 is the maximum
guesses = 0                                     # guesses is a variable with initial value 0

while True:                                     # do the following code all the time 
    guesses = guesses + 1                       # add guess every time i try to answer
    print("I am thinking of a number, can you guess what it is") 
    g = int(input())                            # g is a number entered by the user (imput())
    if g == n:                                  # if the number entered by the user  is equal to the random number stop the code
        break                                   # break means stop the code when the previous condition is met
    elif g < n:                                 #  if the number enttered by user is more than the random number tell the user "Very low!"
        print("Very low!")                      # elifmeans else if means if also happens this
    elif g > n:                                 # if the number entered by user is menor than the random number tell user" Very high!"
        print("Very high!")
print("Very well! You took", guesses, "guesses.")  # if the number entered by the user is correct tell the user"Exactly..."
# tell the user also the number of guesses      

Game 1

Shopping cart crash

This is the python code using pygame zero:


# carro is a sprite, that is, a moving image in a game
# Actor is the name of a Class sprite ( Enric is an example of the class Homo sapiens), this means create and object from a class
# carro is a name of image in folder images
carro = Actor('carro')                          
carro.topright = 0, 10                          # Topright means to locate the image in the top right corner.
# screen sice in pixels                         
WIDTH = 600                                     # This determines the Screen width
HEIGHT = carro.height + 10                      # This determines Height of carro 

def draw():                                     
    screen.clear()                              # This makes the screen background black
    carro.draw()                                # This put the image in the screen


def update():                                   
    carro.left += 10                            # The velocity of the image carro to the left
    if carro.left > WIDTH:                      # If the image on the left is larger than the width.
        carro.right = 0                         # The velocity of the image carro to the right

Game 2

Hit the alien!

This is the python code using pygame zero:



alien = Actor('alien')                          # Alien is a sprite, that is, a moving image in a game
alien.topright = 0, 10                          # Topright means to locate the image in the top right corner.

WIDTH = 600                                     # This determines the Screen width
HEIGHT = alien.height + 10                      # This determines Height of alien 

def draw():                                     
    screen.clear()                              # This makes the screen background black
    alien.draw()                                # This put the image in the screen

def update():                                   
    alien.left += 10                            # The velocity of the image alien to the left
    if alien.left > WIDTH:                      # If the image on the left is larger than the width.
        alien.right = 0                         # The velocity of the image alien to the right

score = 0                                       # The puntuation with which it starts is equal to 0

def on_mouse_down(pos):                         
    global score                                # The computer starts to count score
    if alien.collidepoint(pos):                 # If touch the alien
        score += 1                              # add 1 to the score
    else:                                       # This line include any answer other than toch the alien
        score -= 1                              # minus 1 point of score
        print("Nothing here")                   # 
    print(score)                                #

pgzrun.go()

PONG

Pong

This is the python code using pygame zero:


 WIDTH = 700                                    # This establish the Width of screen
HEIGHT = 500                                    # This determines the height of screen

ball = Rect ((350, 1), (20, 20))                # This establish te measures of the ball
bat = Rect ((560, 400), (110, 20))              # This delimit the measures of bat
vx = 8                                          # This is the velecity of the bat
vy = 8                                          # This is the velecity of the bat

def draw():                                     
    screen.clear()                              # This makes the screen background black
    screen.draw.filled_rect(ball, "purple")     # The color of the ball
    screen.draw.filled_rect(bat, "yellow")      # The color of the bat

def update():                                   
    global vx, vy                               
    ball.x += vx                                
    ball.y += vy                                
    if ball.right > WIDTH or ball.left < 0:     
        vx = -vx                                
    if ball.colliderect(bat) or ball.top < 0:   
        vy = -vy                                
    if ball.bottom > HEIGHT:                    
        exit()                                  # end the game
    if(keyboard.right):                         
        bat.x +=20                              # move 20 pixels to right the bat
    elif(keyboard.left):                        
        bat.x -= 20                             # move 20 pixels to left the bat