Video Game Emulator
The video game emulator is a retro gaming machine that emulates a video game console’s hardware and allows you to play a variety of games using the Raspberry Pi and RetroPie. It uses the Raspberry Pi 3 Model B+, a computer monitor, an SD card, game controllers, and a software called RetroPie. In addition, I also created my own game using Pygame, as well as a custom controller.
Engineer
Matthew L
Area of Interest
Computer Programming
School
Lynbrook High School
Grade
Incoming Junior
Reflection
Final Milestone
import pygame
import math
import random
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption(‘Space Invader’’)
background = pygame.image.load(‘background.png’)
playerImg = pygame.image.load(‘player.png’)
playerX = 370
playerY = 480
playerX_change = 0
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change= []
num_of_enemies = 6
for i in range(num_of_enemies):
enemyImg.append(pygame.image.load(‘enemy.png’))
enemyX.append(random.randint(0,735))
enemyY.append(random.randint(50,150))
enemyX_change.append(15)
enemyY_change.append(50)
bulletImg = pygame.image.load(‘bullet.png’)
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 25
bullet_state = “ready”
score = 0
font = pygame.font.Font(‘freesansbold.ttf’, 32)
textX = 10
textY= 10
over_font = pygame.font.Font(‘freesansbold.ttf’, 64)
def game_over_text():
over_text = over_font.render(“GAME OVER”, True, (255,255,255))
screen.blit(over_text,(200, 250))
def show_score(x,y):
text = font.render(“Score: ” + str(score), True, (255,255,255))
screen.blit(text,(x, y))
def player(x,y):
screen.blit(playerImg,(x, y))
def enemy(x,y, i):
screen.blit(enemyImg[i],(x, y))
def fire(x,y):
global bullet_state
bullet_state = “fire”
screen.blit(bulletImg, (x + 16,y + 10))
def isCollision(enemyX, enemyY, bulletX, bulletY):
distance = math.sqrt((math.pow(enemyX – bulletX, 2)) + (math.pow(enemyY – bulletY, 2)))
if distance < 27:
return True
else:
return False
done = True
while done:
screen.fill((0,0,0))
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -13
if event.key == pygame.K_RIGHT:
playerX_change = 13
if event.key == pygame.K_SPACE:
if bullet_state is “ready”:
bulletX = playerX
fire(playerX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <= 0: playerX = 0 elif playerX >= 736:
playerX = 736
for i in range(num_of_enemies):
if enemyY[i] > 440:
for x in range(num_of_enemies):
enemyY[x] = 2000
game_over_text()
break
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0: enemyX_change[i] = 15 enemyY[i] += enemyY_change[i] elif enemyX[i] >= 736:
enemyX_change[i] = -15
enemyY[i] += enemyY_change[i]
collision = isCollision(enemyX[i],enemyY[i],bulletX,bulletY)
if collision:
bulletY = 480
bullet_state = “ready”
score += 1
enemyX[i] = random.randint(0,735)
enemyY[i] = random.randint(50,150)
enemy(enemyX[i], enemyY[i], i)
if bulletY <= 0:
bulletY = 480
bullet_state = “ready”
if bullet_state is “fire”:
fire(bulletX,bulletY)
bulletY -= bulletY_change
player(playerX,playerY)
show_score(textX, textY)
pygame.display.update()
pygame.quit()