PYTHON
Welcome to my basic games!
we have learnt to code with python
Alien 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 (Carla is an example of the class homosapiense), this means create an object from a class
# blancaymario is a name of a image in the folder images
alien = Actor ('blancaymario')
# topright means to locate the image in the top right corner
alien.topright = 0, 10
# the pixel number of the screen
WIDTH = 600
HEIGHT = alien.height + 30
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
How old is Blanca Suarez?
This is the code to guess how many candies I have using python
import random # random is a python library to create random numbers
n = random.randint(0,100)
guesses = 0
while True:
guesses = guesses + 1
print("How old is Blanca Suarez?")
g = int(input())
if g == 33:
break
elif g < 37:
print("Very low, goes up")
elif g > 32:
print("Very high,low")
print("Correct! You took", guesses, "guesses.")
PONG
Is a game that has a circle that bounces around a box and you have to prevent the circle from touching the ground with the help of a rectangle that you have to move from left to right and so on.
WIDTH = 500
HEIGHT = 500
ball = Rect((150, 400), (20, 20))
bat = Rect((200, 480), (60, 20))
vx = 5
vy = 5
def draw():
screen.clear()
screen.draw.filled_rect(ball, "pink")
screen.draw.filled_rect(bat, "purple")
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()
if(keyboard.right):
bat.x += 2
elif(keyboard.left):
bat.x -= 2
input01
consists of having a conversation with the computer
# Write your code here :-)
print("Enter your name:")
x = input()
print("Hi", x)
if x == "Carla":
print("Oh! nice name")
input02
This game is for having a conversation about sports
print("Enter your favorite sport:")
x = input()
print()
if x == "Dance":
print("Cool! What sport do you practice?")
elif x == "Football":
print("How cool!")
else:
print("Thanks",x)
guess01
This game is used to talk about things
import random
n = random.randint(0,10)
while True:
print("How many inhabitants in USA?")
g = int(input())
if g == 331421000:
break
else:
print("False")
print("GOOD!")
math01
This game used to make and check math
import random
n = random.randint(0,10)
print("What is", n, "+7?")
g = int(input())
if g == n + 7:
print("Is correct")
else:
print("Wrong!!!")# Write your code here :-)
math02
Some kind of mathematical calculation solves you
score = 0
print("What is 1+1 ?")
a = int(input())
if a == 2:
print("Correct")
score = score + 1
print("What is 5x5?")
b = int(input())
if b == 25:
print("Correct")
score = score + 1
print("Your score:", score)
print("How many people visit the eiffel tower every year?")
c = int(input())
if c == 7000000:
print("Yes of course")
score = score + 1
print("How many in habitants does France have in 2021 ?")
d = int(input())
if d == 68011000:
print("Perfect")
score = score + 1
print("Your score:", score)
flappy birds
This game consists in that there is a character in this case a bird which you have to
import pgzrun
import random
TITLE = 'Flappy Bird'
WIDTH = 400
#the width of the game screen
HEIGHT= 400
#the height of the game screen
# These constants control the difficulty of the game
GAP = 130
#the gap is 130
GRAVITY = 0.3
#the gravity the game goes to is 0.3
FLAP_STRENGTH = 6.5
#is the resistance of the game
SPEED = 3
#how fast the game is going
bird = Actor('bird1', (75, 200))
#the height and width of the object called a bird
bird.dead = False
#when the bird is dead
bird.score = 0
#when the bird is dead they don't give us points
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))
#the width and start of the bottom of the game
pipe_top.draw()
#the top tube drawing
pipe_bottom.draw()
#the down tube drawing
bird.draw()
#the image of the bird
pgzrun.go()
Balloon Flight
With this type of game, what you have to try to achieve is that the balloon must be guided without it sticking against the bird that is flying.
from random import randint
WIDTH = 700
HEIGHT = 700
balloon = Actor("balloon")
balloon.pos = 400, 200
bird = Actor("bird-up")
bird.pos = randint(400, 600), randint(5, 100)
house = Actor("house")
house.pos = randint(600, 1000), 400
tree = Actor("tree")
tree.pos = randint(800, 1200), 900
bird_up = True
up = False
game_over = False
score = 0
number_of_updates = 0
scores = []
def update_high_scores():
global score, scores
filename = r"/Users/bharti/Desktop/python-games/balloon-flight/high-scores.txt"
scores = []
with open(filename, "r") as file:
line = file.readline()
high_scores = line.split()
for high_score in high_scores:
if(score > int(high_score)):
scores.append(str(score) + " ")
score = int(high_score)
else:
scores.append(str(high_score) + " ")
with open(filename, "w") as file:
for high_score in scores:
file.write(high_score)
def display_high_scores ():
screen.draw.text("HIGH SCORES", (350,150), color="blue")
y = 175
position = 1
for high_score in scores:
screen.draw.text(str(position) + ", " + high_score, (350, y), color="blue")
y += 25
position += 1
def draw():
screen.blit("background", (0, 0))
if not game_over:
balloon.draw()
bird.draw()
house.draw()
tree.draw()
screen.draw.text("Score: " + str(score), (700, 5), color="blue")
else:
display_high_scores()
def on_mouse_down():
global up
up = True
balloon.y -= 50
def on_mouse_up():
global up
up = False
def flap():
global bird_up
if bird_up:
bird.image = "bird-down"
bird_up = True
def update():
global game_over, score, number_of_updates
if not game_over:
if not up:
balloon.y += 1
if bird.x > 0:
bird.x -= 4
if number_of_updates == 9:
flap()
number_of_updates = 0
else:
number_of_updates += 1
else:
bird.x = randint(800, 1600)
bird.y = randint(10, 200)
score += 1
number_of_updates = 0
if house.right > 0:
house.x -= 2
else:
house.x = randint(800, 1600)
score += 1
if tree.right > 0:
tree.x -= 2
else:
tree.x = randint(800, 1600)
score += 1
if balloon.top < 0 or balloon.bottom > 560:
game_over = True
update_high_scores ()
if balloon.collidepoint(bird.x, bird.y) or \
balloon.collidepoint(house.x, house.y) or \
balloon.collidepoint(tree.x, tree.y):
game_over = True
update_high_scores()