Using the pygame package, I was able to create game, or at least a basic level of it,  which was quite fun and challenging to build. Of course it was an introductory experiment to all the wonderful things that someone can achieve with Python and this is why I will keep interacting with the language, for its versatility and flexibility. The code is embedded below and you can use it for recreating the game-you just need to make sure that you have all the pictures needed to the same folder, in which you script is saved and run. You can use whatever you want for background, player and enemy picture, I improvised as well. The goal is to reach the treasure, without hitting, or colliding with the enemy.

By pressing the space keyboard, you are able to move your player, Ironman in this case, up and down to the map. Ultron, the enemy, is moving horizontally in order to prevent Ironman reaching the treasure. The issue of why a billionaire like Tony Stark would even bother for a chest of treasure in the first place is out of the discussion right now! The only thing that matters is that once Tony makes it to the treasure, we change level and Ultron’s speed is increased. This keeps happening, unless Tony hits Ultron, in which case,  the position of the player is restarted. Here is a snapshot of actually playing the game:

Here is the code behind it.

import pygame
pygame.init()

def checkCollision(x, y, treasureX, treasureY):
# Checking for collisions betweens the objects created

global screen, textWin
collisionState = False

if y >= treasureY and y <= treasureY + 70:
if x >= treasureX and x <= treasureX + 80:
y = 130
collisionState = True
elif x + 100 >= treasureX and x + 100 <= treasureX + 80:
y = 130
collisionState = True
elif y + 170 >= treasureY and y + 170 <= treasureY + 70:
if x >= treasureX and x <= treasureX + 80:
y = 130
collisionState = True
elif x + 100 >= treasureX and x + 100 <= treasureX + 80:
y = 130
collisionState = True
return collisionState, y

screen = pygame.display.set_mode((1000, 800))

finished = False
x = 450 – (80/2)
y = 130 # the coordinates as initial values

# index = [0, 1, 2, 3, …, n]
# array = [0, “Hello”, 2.5]
# print(array[2])
# print(pygame.K_SPACE)

playerImage = pygame.image.load(“Player.png”)
# the location of the picture should be on the local folder
playerImage = pygame.transform.scale(playerImage, (100, 170))
playerImage = playerImage.convert_alpha()

# Setting the background Image
backgroundImage = pygame.image.load(“Ground.png”)
backgroundImage = pygame.transform.scale(backgroundImage, (1000, 800))
screen.blit(backgroundImage, (0, 0))

treasureImage = pygame.image.load(“Treasure.jpg”)
treasureImage = pygame.transform.scale(treasureImage, (80, 70))
treasureImage = treasureImage.convert_alpha()
treasureX = 450-(80/2)
treasureY = 490

enemyImage = pygame.image.load(“Enemy.png”)
enemyImage = pygame.transform.scale(enemyImage, (50, 50))
enemyImage = enemyImage.convert_alpha()
enemyX = 0
enemyY = 380

font = pygame.font.SysFont(“comicsans”, 70)
level = 1

frame = pygame.time.Clock()
collisionTreasure = False
collisionEnemy = False
movingRight = True

while finished == False: # while our game is not finished
for event in pygame.event.get():
if event.type == pygame.QUIT:
finished = True

pressedKeys = pygame.key.get_pressed()
# print(pressedKeys[pygame.K_SPACE])
if enemyX >= (970-50):
movingRight = False
elif enemyX <= 30:
movingRight = True

if movingRight == True:
enemyX += 4 * level
else:
enemyX -= 4 * level

if pressedKeys[pygame.K_SPACE] == 1:
y += 5

# rect1 = pygame.Rect(x, y, 40, 40)
# x, y (from top left corner), width and height (in pixels)
color = (0, 0, 255) #R, G, B
white = (255, 255, 255)

screen.blit(backgroundImage, (0, 0))
screen.blit(treasureImage, (treasureX, treasureY))
screen.blit(playerImage, (x, y))
screen.blit(enemyImage, (enemyX, enemyY))
collisionTreasure, y = checkCollision(x, y, treasureX, treasureY)
collisionEnemy, y = checkCollision(x, y, enemyX, enemyY)

if collisionTreasure == True:
level += 1
textWin = font.render(“Congrats! You’ve reached level ” + str(level) + “!”, True, (0, 0, 0))
screen.blit(textWin, (20, 20))
pygame.display.flip()
frame.tick(1)

#pygame.draw.rect(screen, color, rect1) – the picture is overwritting this drawing

pygame.display.flip()
# it allows us to update the screen, based on the draw from above

frame.tick(40) # frames per second-30 different frames every second

Posted by Dimitris

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s