PYTHON
Welcome to basic games!
WE HAVE LEARNT TO CODE WITH PYTHON
Enter gmail
This is a basic python code to input a gmail and simulate that it is incorrect
print("Enter your gmail:")
# Print is evalued before print on the screen and it put automatically what is in parentheses.
x = input()
# Input is the standard input for the user to enter a answer to "print".
print ("are you sure that your gmail is",x,"?")
# Print is what it put automatically when the question is answered ("are you sure that ...")
if x == "igarcialoba@gmail.com":
# The answer to put in x= input()
print ("your gmail is incorrect")
# If the user enter the x, the cumputer print what is in parenthesis.
This is a videotutorial showing how to create the code:
Enter films
This is a basic python code to input a film you want to watch
print("Enter the film that you want to watch:")
# Print is evalued before print on the screen and it put automatically what is in parentheses.
x = input()
# Input is the standard input for the user to enter a answer to "print".
print("The film", x , "is a great option you have selected")
# If the user answer the x=="Finding Nemo or x=="The lion king", it print:
#"the film", x (finding nemo or the lion king), "...")
if x == "Finding Nemo":
# One of the two options for answering the print("Enter the film...)
print("But it isn't free, if you want to whatch it, enter your bank account")
# The second sentence when the user answer Finding Nemo.
elif x == "The lion king":
# Elif is for enter another option in print("...") in this case is :"The lion King".
print("This is a free option film")
# When the user enter the film "The lion king", it print ("This is a ...")
else:
# If the user prinnt is not "Finding Nemo" or "The lion king":
print( "But we don't have the film", x)
# The seond sentence it print in addition to "The film", x, ...". This sentence is only print in "else")
This is a video showing the operation of the code:
Mathematical operations
This is a basic python code to do mathemaical operations:
import random
# Prepare the computer to understand and generate "random numbers"
o = random.randint(800, 900)
# 0 is the minimum random number and 90 the maximum.
p = random.randint(200, 300)
# 200 is the minimum random number and 300 the maximum.
l = random.randint(300, 480)
# 300 is the minimum random number and 480 the maximum.
f = random.randint(480, 520)
# 480 is the minimum random number and 5200 the maximum.
print("What is", o, "/", p, "+", l, "-", f, "?")
# What it print for the user
j = float(input())
# Float allows the answer with decimals and recognize it. It is the space for the user answers
if j == o / p + l - f:
# If the answer of the user (variable j) is the same of the operation it prints:
print("Great")
# At the screen ir prints ("Great")
else:
# If the user doesn't answer as the operation it prints:
print("Try again")
# It prints at the scren ("Try again")
This is a video showing the operation of the code
Because there was a problem with the code execution, the operation will not be visible at this time.
The problem will be solved soon.
Sorry for the inconvenience.
Math questions
This is a basic python code to play guessing different math questions:
score = 0
# The puntuation at the beginin
print("What is 54 - 10 + 7 ?")
# The first question that it prints at the screen.
y = int(input())
# Input is the standard input for the user to enter a answer to "print".
if y == 51:
# The answer to the first question acquires the variable "y".
print("Very well!! You are a great mathematical!")
# If the answer of the user is same at 51 print:("Very well!!!...")
score = score + 1
# If the user get it right, 1 is added to the initial score
print("Can you do: 65/5 ?")
# The second question that it prints at the screen.
y = int(input())
# Input is the standard input for the user to enter a answer to "print".
if y == 13:
# The answer to the second question acquires the variable "y".
print("Fantastic!!, Great job!")
# If the answer of the user is same at 13 print:("Fantastic!!, Great job!")
score = score + 1
# If the user get it right, 1 is added to the previous score
print(" 45 - 8 - 6")
# The third question that it prints at the screen.
y = int(input())
# Input is the standard input for the user to enter a answer to "print".
if y == 31:
# The answer to the third question acquires the variable "y".
print("Incredible! I didn't less from you")
# If the answer of the user is same at 31 print: ("Incredible!...")
score = score + 1
# If the user get it right, 1 is added to the previous score
print("Your score:" , score)
# When the game is over, it count the final score
This is a video showing the operation of the code:
Guess the year of born
This is a basic python code to play guessing the year of the born with some tracks
import random
ñ = random.randint(1936, 1947)
# The variable ñ is a random number between 1936 and 1947 (the answer of the question)
while True:
# If the user write an answer...
print("Can you guess what year I was born?")
# The question in parenthesis is evalued before print in the screen.
k = int(input())
# The k is the variable corresponded of a random number and and int(input)) is the space for the user to respond.
if k == ñ:
# If the variable of the year is the same as the one that the user has entered...
break
# Another block of code
elif k > ñ:
# If the correct option is highest than the answer print:
print("Wow, nono i'm more old!")
# If the answer is higher than the answer of the user, print:("Wow, nono..")
elif k < ñ:
# If the correct option is less high than the answer of the user, print: ("I'm old but ...!!")
print("I'm old but not that much!!")
print("Yess, that is the answer, I'm so old...")
# If the variable of the year is the same as the one that the user has entered... ("Yess, that ...")
This is a video showing the operation of the code
How many candies I have?
This is a basic python code to guess how many candies there are
import random # random is a python library to create random numbers
d = random.randint(10, 20)
# d is a integer number from ten to twenty.
guesses = 0
# Guesses is a variable with initial value 0.
while True:
# Do the following code all the time counting up guesses until break condition.
guesses = guesses + 1
# Add a guess every time I try to answer.
print("Can you guess how candies I have?")
# Show in the screen this question.
k = int(input())
# k is a number entered by the user (input().
if k == d:
# If the number entered by the user is equal to the random number stop the code.
break
# Break means stoop the code when the previois condition is met
elif k > d:
# If the number entered by user is more than the random number tell the user "a little lower"
print("A little lower ")
# Elif means else if menas if also hppens this.
elif k < d:
# If the number entered by user is menor than the random number tell user " Some more".
print("Some more")
print("Exactly! Do you want some of them? You have tried and find in only ", guesses, "attempts.")
# If the number entered by the user is correct tell the user "Exactly..."
# Tell the user also the number of guesses and the word
This is a video showing the operation of the code
The bat and the ball
This is a Pong game to hold a ball that jumps with the bat
import pgzrun , pygame
import pgzrun
import pygame
WIDTH = 560
# Width from the bakground
HEIGHT = 560
# Heifgt from the bakground
ball = Rect((100, 300), (30, 30))
# Size with the straight sides of the game elements
bat = Rect((150, 500), (180, 20))
# Size with the straight sides of the game elements
vx = 4.5
# The velocity of the variable x is 3.5 pixels
vy = 4.5
# The velocity of the variable x is 3.5 pixels
bg = pygame.image.load("./images/play.jpg").convert()
# The bakground og the game is the image that is in the folder "images" with the name play.jpg
def draw():
# The physical characteristics that the game must have are listed below.
screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
# The way the game will run will be in full screen
screen.blit(bg, [0,0])
# When we put blit, we are combinating the screen sice and the sice of the background image
screen.draw.filled_rect(ball, "orange")
# The screen drawing (ball) has straight sides and is also filled with color (orange)
screen.draw.filled_rect(bat, "white")
# The screen drawing (bat) has straight sides and is also filled with color (white)
def update():
# The movements that the elements will make in different situations are defined
global vx, vy
# Global variables
ball.x += vx
# To move the position of the ball "x" a velocity corresponding to the variable x is added
ball.y += vy
# To move the position of the ball "Y" a velocity corresponding to the variable "Y" is added
if ball.right > WIDTH or ball.left < 0:
# If the ball is placed towards the right in a position greater than the width of the screen or comes out from a position on the left side:
vx = -vy
# Change ball direction and speed
if ball.colliderect(bat) or ball.top < 0:
# If the ball bounces off the bat and reaches the top less than 0:
vy = -vy
# Change ball direction and speed
if ball.bottom > HEIGHT:
# If the ball doesn't bounce off the bat because is more high than the height:
exit()
# The game is over
if (keyboard.right):
# If the user hits the right arrow on the keyboard :
bat.x += 4
# The bat will move 4 pixels to the rigth.
elif (keyboard.left):
# If the user hits the left arrow on the keyboard :
bat.x -= 4
#T he bat will move 4 pixels to the rigth.
This is a video showing the operation of the code
Lift
This is a Alien Fall game to observe the operation of an image that loops up and down
lift = Actor ('lift') # actor is the name of a class sprite (Ivana is an example of the class Homo Sapiens)
# Find an image in images folder named: lift.jpg or lift. png.
lift.topcentre = 3, 20
# Coordinates of lift position.
WIDTH = lift.width
# Screen width based exactly on the width of the lift # screen sice in pixels.
HEIGHT = 400
# Screen height is 400 pixels, bigger than lift height.
def draw():
# Fuction of the draw.
Screen.fill((247, 110, 110))
# rgb (red, green and blue) colour of the screen.
lift.draw()
# Draw the image in the screen.
def update():
# Fuction
lift.bottom +=3
# Move three pixels to the right continuosly.
if lift.bottom > HEIGHT + 400:
# If the image goes up more than the screen size, start again from the bottom.
lift.top = -400
# The lift position at the top is -400 pixels.
This is a video showing the operation of the code
Car at the road
This is a Alien Fall game to observe how how an element moves on an image and gaining if it is touched on this
import pgzrun , pygame
import pgzrun
caar = Actor('caar')
#The actor of the game is an image named "carr"
caar.topright = 0, 15
#The position in coordinates of the car
WIDTH = 500
# The width of the screen is 500 pixels.
HEIGHT = caar.height - 20
# The height of the screen is the caar height minus 20 pixels
bg = pygame.image.load("./images/rooad.jpg").convert()
# The bakground og the game is the image that is in the folder "images" with the name rooad.jpg
def draw():
screen.blit(bg, [0,0])
caar.draw()
def update():
caar.left += 3
if caar.left > WIDTH:
caar.right = 0
# If the image is at the left and moves more pixels than the width of the screen
#it turnes at the right
score = 0
def on_mouse_down(pos):
# What will happen if you touch the image with the mouse
global score
if caar.collidepoint(pos):
score += 6
# If the user touch the image, added 10
else:
score -= 6
#If the user doesn't touch the image, subtract 10
print("This is the road!")
print(score)
pgzrun.go()
This is a video showing the operation of the code
Notre Dame burning
This is a Alien Fall game to observe how how an element moves on an image and and if you touch the image, it will change and a noise will be heard to get points
import pgzrun , pygame
notredamee = Actor('notredamee')
notredamee.topright = 0, 40
WIDTH = 500
# The width of the screen game is 500 pixels
HEIGTH = notredamee.height + 1000
# The heigth of the screen is the height of the actor plus 1000 pixels
bg = pygame.image.load("./images/skyorange.jpg").convert()
# The bakground og the game is the image that is in the folder "images" named rooad.jpg
def draw():
# Fuction
screen.blit(bg, [0,0])
notredamee.draw()
def update():
notredamee.left += 2
if notredamee.left > WIDTH:
notredamee.right = 0
# If notredame goes 2 pixels left, notredame is bigger than the width, notredame returns at the position 0 right
score = 0
# The puntuation at the beginning
def on_mouse_down(pos):
# If the user touches above the image:
global score
# Is the moment in which the points are counted.
if notredamee.collidepoint(pos):
# If the user touches notredame:
sounds.water.play()
# A sound named water.play is heard.
notredamee.image = 'notredame_fire'
# The image is changed for another named 'notredame_fire'.
score += 1
# 1 is added to initial puntuation.
else:
# If the user doesn't touch the image:
score -= 1
# A point is subtracted from the score.
print("put out the fire!")
# It prints at the screen: "put out the fire!").
print(score)
# Finally it prints at the screen the final score.
pgzrun.go()
This is a video showing the operation of the code
Sound of a bell
This is a Alien Fall game to observe how how an element moves on an image and play for a noise to be heard and get points
import pgzrun , pygame
bell = Actor('bell')
# The actor of the game is an image named "bell"
bell.topright = 0, 5
WIDTH = 700
# The width of the screen game is 700 pixels
HEIGHT = bell.height + 50
#The heigth of the screen is the height of the actor plus 50 pixels
bg = pygame.image.load("./images/blue sky.png").convert()
# The bakground og the game is the image that is in the folder "images" "named blue sky.png"
def draw ():
# Fuction of the draw
screen.blit(bg, [0,0])
bell.draw()
# The draw of the image is at the screen
def update():
# Fuction if the user plays
bell.left += 2
# If the bell goes 2 pixels to the left:
if bell.left > WIDTH:
# The bell at the left is more bigger than the width:
bell.right = 9
# The bell rurns at the right 9 pixels
score = 0
# The initial score is 0
def on_mouse_down(pos):
# Fuction if the user hovers mouse the image
global score
# Is the moment in which the points are counted.
if bell.collidepoint(pos):
# If the user touches the bell:
set_bell_hit()
# Is true that the bell is touched by the user
score+= 1
# The score of the user is the initial score plus 1
print(score)
else:
# If the user does't touch the bell
score -= 9
print(score)
def set_bell_hit():
# Fuction if the user touches the bell:
bell.image = 'bell_ramrod.png'
# If the user touches the image, it changes to another image
sounds.dingg.play()
# A noise named dingg.play sounds
clock.schedule_unique(set_bell_normal, 0.9)
# Seconds the second image lasts
def set_bell_normal():
bell.image = "bell"
# The initial image returns
pgzrun.go()
This is a video showing the operation of the code
Touch the dartboard
This is a Alien Fall game to touch the element when it is anywhere on the screen and get a good rating
from random import randint
WIDTH = 730
# The width of the screen game
HEIGHT = 430
# The height of the screen game
dartboard = Actor("dartboard")
# The actor of the game is an image named dartboard.
import pgzrun , pygame
bg = pygame.image.load("./images/forest.jpg").convert()
# The bakground of the game is the image that is in the folder "images" "named forest.jpg"
def draw():
# Fuction of the draw
screen.blit(bg, [0,0])
alien.draw()
def shoot_dartboard():
# Fuction if the user touches the image
dartboard.x = randint(0, 500)
# The dartboard x is at the coordinates randint 0 and 500 (between these).
dartboard.y = randint(0, 500)
# The dartboard y is at the coordinates randint 0 and 500 (between these).
def on_mouse_down(pos):
# Fuction
if dartboard.collidepoint(pos):
# If the user touches the dartboardn:
print("Good shot!")
# It prints at the screen :("Good shot!")
shoot_dartboard()
# The computer places the alien to another randint coordinate
else:
# If the user doesn't touches the image:
print("You missed!")
# It prints ate the screen: "You missed!")
quit()
shoot_dartboard()
This is a video showing the operation of the code
Union Puzzle
This is a Alien Fall game to join a mobile element to another imobil with the arrows of the keyboard and hear a noise
import pgzrun
import random
WIDTH = 600
# The width of the screen
HEIGHT = 430
# The height of the screen
puzzle1 = Actor('puzzle 1', midbottom=(WIDTH // 2, HEIGHT))
# The actor corresponds to an image" named puzzle 1"
puzzle1 = Actor('puzzle 1')
# The actor of the game is named "puzzle1".
puzzle1.topright = 100, 10
# The position of the actor.
bg = pygame.image.load("./images/play class.jpg").convert()
# The bakground of the game is the image that is in the folder "images" "named class.jpg"
def draw():
# Fuction of the draw
screen.blit(bg, [0,0])
puzzle1.draw()
puzzle1.draw()
# The puzzle 1 is draw at the screen (puzzle 1)
xpos = random.randint(0, 600)
# The posotion x of the actor is a random numner randint 0 and 800
def update():
# Fuction
global xpos
# The x position of the actor if...
puzzle1.y += 5
# When the alien is at the position "y", move 5 pixels down
if keyboard.left:
# If the user touches the arrow left of the keyboard:
puzzle1.x -= 5
# The puzzle1 at the position "x" moves 5 pixels left
if keyboard.right:
# If the user touches the arrow right of the keyboard:
puzzle1.x += 5
# The puzzle1 at the position "x" moves 5 pixels right.
if puzzle1.colliderect(puzzle1):
# If the puzzle and puzzle1 join:
print('Has fitted it')
# Print at the screen:('Has fitted it')
puzzle1.y = 0
xpos = random.randint(0, 600)
# Position x
puzzle1.x = xpos
# The puzzle1 is in a "x" positiom
if puzzle1.y > 600:
# If the position "y" of the puzzle1 is bigger than 600:
print("Fit it better and you will win")
# Print at the screen:("Fit it better and you...")
puzzle1. y = 0
# The number of coordinate is located at the top of the screen
xpos = random.randint(0, 600)
# The x position of the puzzle1 is in a number between 0 and 600
puzzle1.x = xpos
# The puzzle1 is in a "x" position of the screen
def on_mouse_down(pos):
# Fuction
global score
# The moment to count points
if puzzle1.collidepoint(pos):
set_puzzle1_hit()
# The puzzle1 joins
score += 1
# 1 point is added
print(score)
# It prints the puntuation
else:
# If the user doesn't join the puzzle:
score -= 1
# It removes a point
print(score)
# It prints the points
def set_puzzle1_hit():
# Fuction
puzzle1.image = 'puzzle1'
# Appear the image "puzzle1"
sounds.eep.play()
# It plays this sound
clock.schedule_unique(set_alien_normal, 0.5)
# Time the image remains on the screen
def set_puzzle1_normal():
# Fuction, if the image crash with the another image, the firt return to start
puzzle1.image = "puzzle1"
# The image with the two images (puzzle1 and puzzle1)
pgzrun.go()
This is a video showing the operation of the code