Welcome to my basic games!

we have learnt to code with python

Allien fall

This is the python code using pygame zero:

   
  # alien is a sprite, that is, a moving image in a game
  # Actor is the name of a Class sprite (Pau is an example of the class homosapiense), this means create an object from a class
  # ansuf is a name of a image in the folder images
  alien = Actor ('ansuf')
  # topright means to locate the image in the top right corner
alien.topright = 0, 10
  # the pixel number of the screen
WIDTH = 500
HEIGHT = alien.height + 40

def draw():
    screen.clear()
    alien.draw()


def update():
    alien.left += 2
    if alien.left > WIDTH:
        alien.right = 0

    

This is a videotutorial showing how to create a basic game named Alien Fall

Guess 2

This is the code to guess how old is Leo Messi I have using python


    import random # random is a python library to create random numbers
    
    
  guesses = guesses + 1              # guesses is a variable with initial value 1
  print("How old is Leo Messi?")     
  g = int(input())                   # g is a number entered by the user (imput())
  if g == 34:                        # 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 < 45:                       # elifmeans else if means if also happens this
      print("A little more!")        #  if the number enttered by user is more than the random number tell the user "A little more!"
  elif g > 30:           
      print("A little less!")        # elifmeans else if means if also happens this
print("Correct! You took", guesses, "guesses.")   # if the number entered by the user is correct tell the user"Exactly..."

Game of pong

This is the code to game pong


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

ball = Rect((150, 400), (20, 20))           # This establish te measures of the ball
bat = Rect((200, 480), (60, 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, "blue")   # The color of the ball
    screen.draw.filled_rect(bat, "green")   # 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 += 2                          # move 20 pixels to right the bat          
    elif(keyboard.left):
        bat.x -= 2                          # move 20 pixels to left the bat


How many cities does Germany have?

This is the code to guess the answer


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

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

while True:                                       # do the following code all the time 
    print("How many cities does Germany have?")
    g = int(input())                              # g is a number entered by the user (imput())
    if g == 2075:                                 # 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
    else:
        print("No, I give you another chance")
print("Good")

Math01

This code is used to solve operations


    
    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, 20)        # 0 is the random min, 20 is the maximum

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

Math02

This code is the same as the previous one but more difficult


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

print("What is 4x2+8:2 ?")        # Type the question "What is 4x2+8:2 ?" on the screen
g = int(input())                  # Forces to write the answer with a whole number
if g == 12:                       # If the answer is equal to 12...
  print("Correct")                # ... type the word "Correct" on the screen
  score = score + 2               # ... the puntuation add 2
print("What is -48+87:3?")        # Type the question "What is -48+87:3?" on the screen
f = int(input())                  # Forces to write the answer with a whole number
if f == -19:                      # If the answer is equal to 19...
  print("Correct")                # ... type the word "Correct" on the screen
  score = score + 4               # ... the puntuation add 4
 

print("How many countries are in the world ?")  # Type the question "How many countries are in the world ?" on the screen
e = int(input())                                # Forces to write the answer with a whole answer
if e == 195:                                    # If the answer is 195...
  print("Correct")                              # ... type the word "Correct" on the screen
  score = score + 6                             # ... the puntuation add 6

print("How many cities does Spain have?")       # Type the question "How many cities does Spain have?" on the screen
j = int(input())                                # Forces to write the answer with a whole answer
if j == 8131:                                   # If the answer is 8131...
 print("Correct")                               # ... type the word "Correct" on the screen
 score = score + 8                              # ... the puntuation add 8

 print("Congratulations, you have made the highest number of points:", score)  # Show the final puntuation

Input01

This code is to put your age and have the machine answer anything about your age.


    
    
print("Enter your age:")              # 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, 14 years!!", x)           # The age you have written will appear in the place of the x.
if x == "14":                         # This says if what you write is the word that is in quotes.
    print("it's a good age")          # If the age previously written is "14" he answers what is in quotes.
else:                                 # This line include any answer other than "14".
    print("Well 14 years is better")  # If the age previously written isn't "14" he answers what is in quotes.
 
 

Input02

This code is to put your name and the machine responds with something nice about your name.


     
     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)                      # This line answers what is written in quotes and the x is the answer that is added according to the name.
if x == "Pau":                         # If the "x" is Pau, one answer or another will come out.
  print("")                            # The computer answers what is in quotes.
elif x == "":                          # If the "x" is nothing, will come out.
  print("It's a fascinating name!")    # The computer answers what is in quotes.
else:                                  # This line is for something other than "Pau" to answer you.
  print("I do not know your name", x)  # If the answer is not "Pau", he responds with a phrase
 
 

FlappyBird

This code is a game in which a bird comes out that has to go through the middle of the pipes to survive.


     
     import pgzrun
import random
TITLE = 'Flappy Bird'
WIDTH = 400
HEIGHT = 708
# These constants control the difficulty of the game
GAP = 130
GRAVITY = 0.3
FLAP_STRENGTH = 6.5
SPEED = 3
bird = Actor('bird1', (75, 200))
bird.dead = False
bird.score = 0
bird.vy = 0
#storage.setdefault('highscore' 0)
def reset_pipes():
    pipe_gap_y = random.randint(200, HEIGHT - 200)
    pipe_top.pos = (WIDTH, pipe_gap_y - GAP // 2)
    pipe_bottom.pos = (WIDTH, pipe_gap_y + GAP // 2)
pipe_top = Actor('top', anchor=('left', 'bottom'))
pipe_bottom = Actor('bottom', anchor=('left', 'top'))
reset_pipes()  #Set initial pipe positions.
def update_pipes():
    pipe_top.left -= SPEED
    pipe_bottom.left -= SPEED
    if pipe_top.right < 0:
        reset_pipes()
        if not bird.dead:
            bird.score += 1
def update_bird():
    uy = bird.vy
    bird.vy += GRAVITY
    bird.y += (uy + bird.vy) / 2
    bird.x = 75
    if not bird.dead:
        if bird.vy < -3:
            bird.image = 'bird2'
        else:
            bird.image = 'bird1'
    if bird.colliderect(pipe_top) or bird.colliderect(pipe_bottom):
        bird.dead = True
        bird.image= 'birddead'
    if not 0 < bird.y < 720:
        bird.y = 200
        bird.dead = False
        bird.score = 0
        bird.vy = 0
        reset_pipes()
def update():
    update_pipes()
    update_bird()
def on_key_down():
    if not bird.dead:
        bird.vy = -FLAP_STRENGTH
def draw():
    screen.blit('background', (0, 0))
    pipe_top.draw()
    pipe_bottom.draw()
    bird.draw()
pgzrun.go()


Follow the numbers

This code is a game that consists in that you get different points with numbers that what you have to do with those points is to join them and form a figure.

   
    from random import randint

WIDTH = 600
HEIGHT = 600
dots = []
lines = []

next_dot = 0

for dot in range(0, 20):
    actor = Actor("dot")
    actor.pos = randint(40, WIDTH - 40), \
    randint(40, HEIGHT - 40)
    dots.append(actor)

def draw():
    screen.fill("purple")
    number = 1
    for dot in dots:
        screen.draw.text(str(number), \
                        (dot.pos[0], dot.pos[1] + 12))
        dot.draw()
        number = number + 1
    for line in lines:
        screen.draw.line(line[0], line[1], (100, 0, 0))

def on_mouse_down(pos):
    global next_dot
    global lines
    if dots[next_dot].collidepoint(pos):
        if next_dot:
            lines.append((dots[next_dot - 1].pos, dots[next_dot].pos))
        next_dot = next_dot + 1
    else:
        lines = []
        next_dot = 0