Skip to content
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
127 lines (108 sloc) 4.57 KB
#! /usr/local/bin/python2
# -*- encoding: utf-8 -*-
# Authors: Asger Anders Lund Hansen, Mads Ynddal and Troels Ynddal
# License: See LICENSE file
# GitHub:
import traceback
import time
import os.path
import os
import sys
import platform
from PyBoy.Logger import logger
from PyBoy.WindowEvent import WindowEvent
if platform.system() != "Windows":
from Debug import Debug
from PyBoy import PyBoy
from PyBoy.GameWindow import SdlGameWindow as Window
def getROM(ROMdir):
# Give a list of ROMs to start
found_files = filter(lambda f: f.lower().endswith(
".gb") or f.lower().endswith(".gbc"), os.listdir(ROMdir))
for i, f in enumerate(found_files):
print ("%s\t%s" % (i + 1, f))
filename = raw_input("Write the name or number of the tetris ROM file:\n")
filename = ROMdir + found_files[int(filename) - 1]
filename = ROMdir + filename
return filename
if __name__ == "__main__":
bootROM = None
ROMdir = "ROMs/"
scale = 1
# Verify directories
if not bootROM is None and not os.path.exists(bootROM):
print ("Boot-ROM not found. Please copy the Boot-ROM to '%s'. Using replacement in the meanwhile..." % bootROM)
bootROM = None
if not os.path.exists(ROMdir) and len(sys.argv) < 2:
print ("ROM folder not found. Please copy the Game-ROM to '%s'" % ROMdir)
# Check if the ROM is given through argv
if len(sys.argv) > 1: # First arg is SDL2/PyGame
filename = sys.argv[1]
filename = getROM(ROMdir)
# Start PyBoy and run loop
pyboy = PyBoy(Window(scale=scale), filename, bootROM)
frame = 0
first_brick = False
view = pyboy.getTileView(False)
while not pyboy.tick():
print "frame:", frame
# Start game. Just press Start and A when the game allows us.
# The frames are not 100% accurate.
if frame == 144:
elif frame == 145:
elif frame == 152:
elif frame == 153:
elif frame == 156:
elif frame == 157:
elif frame == 162:
elif frame == 163:
# Play game. When we are passed the 168th frame, the game has begone.
# The "technique" is just to move the Tetromino to the right.
elif frame >168:
if frame % 2 == 0:
elif frame % 2 == 1:
print "Screen pos:", pyboy.getScreenPosition()
# As an example, it could be useful to know the coordinates
# of the sprites on the screen and which they look like.
for n in range(40):
sprite = pyboy.getSprite(n)
if sprite.is_on_screen():
print "Sprite:", sprite.get_x(), sprite.get_y(), sprite.get_tile()
# Show how we can read the tile data for the screen. We can use
# this to see when one of the Tetrominos touch the bottom. This
# could be used to extract a matrix of the occupied squares by
# iterating from the top to the bottom of the screen.
# Sidenote: The currently moving Tetromino is a sprite, so it
# won't show up in the tile data. The tile data shows only the
# placed Tetrominos.
# We could also read out the score from the screen instead of
# finding the corresponding value in RAM.
if not first_brick:
for n in range(10):
if view.get_tile(n+2,17) != 47:
first_brick = True
print "First brick touched the bottom!"
frame += 1
except KeyboardInterrupt:
print ("Interrupted by keyboard")
except Exception as ex:
You can’t perform that action at this time.