-
Notifications
You must be signed in to change notification settings - Fork 0
/
emulator.py
executable file
·171 lines (149 loc) · 5.86 KB
/
emulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
import sys
sys.path.append('PyBoy/Source')
import os
import traceback
import glob
from PyBoy.WindowEvent import WindowEvent
from PyBoy import PyBoy
from PIL import Image
from PyBoy.GameWindow import SdlGameWindow as Window
class Emulator:
def __init__(self):
bootROM = None
ROMdir = "ROM/"
scale = 2
try:
# Check if the ROM is given through argv
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
filename = self.getROM(ROMdir)
# Start PyBoy and run loop
self.pyboy = PyBoy(Window(scale=scale), filename, bootROM)
except Exception:
traceback.print_exc()
def getROM(self, 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 Pokemon Blue ROM file:\n")
try:
filename = ROMdir + found_files[int(filename) - 1]
except:
filename = ROMdir + filename
return filename
def ticks(self, n):
for i in range(0, n):
self.pyboy.tick()
def gamestart(self):
print("> Game starting...")
cartridge = self.pyboy.mb.cartridge.filename.split('/')[-1:][0]
print("> Loading {0}".format(cartridge))
# ticks(pyboy, 100)
# pyboy.mb.buttonEvent([WindowEvent.LoadState])
self.pyboy.mb.loadState("STATES/{0}.state".format(cartridge))
self.pyboy.tick()
self.pyboy.sendInput([WindowEvent.PressButtonA])
self.pyboy.tick()
self.pyboy.sendInput([WindowEvent.ReleaseButtonA])
self.ticks(250)
def savegame(self):
list_of_files = glob.glob("STATES/*.state")
list_of_files.sort()
cartridge = self.pyboy.mb.cartridge.filename.split('/')[-1:][0]
counter = "00000"
savefile = cartridge + counter + ".state"
for file in list_of_files:
actualname = file.split('/')[-1:][0]
if savefile == actualname:
intcounter = int(counter)
intcounter += 1
counter = str(intcounter)
if len(counter) < 5:
counter = "0"*(5-len(counter)) + counter
savefile = cartridge + counter + ".state"
self.pyboy.mb.saveState("STATES/"+savefile)
def resumegame(self):
list_of_files = glob.glob("STATES/*.state")
list_of_filenames = map(lambda fullpath: fullpath.split('/')[-1:][0],
list_of_files)
list_of_filenames.sort()
cartridge = self.pyboy.mb.cartridge.filename.split('/')[-1:][0]
counter = "00000"
savefile = cartridge + counter + ".state"
if savefile not in list_of_filenames:
self.gamestart()
return
for file in list_of_filenames:
if savefile == file:
intcounter = int(counter)
intcounter += 1
counter = str(intcounter)
if len(counter) < 5:
counter = "0"*(5-len(counter)) + counter
savefile = cartridge + counter + ".state"
counter = str(int(counter)-1)
counter = "0"*(5-len(counter)) + counter
savefile = cartridge + counter + ".state"
print(savefile)
self.pyboy.mb.loadState("STATES/"+savefile)
def presskey(self, key):
keys = {"up":[[WindowEvent.PressArrowUp],[WindowEvent.ReleaseArrowUp]],
"down":[[WindowEvent.PressArrowDown],[WindowEvent.ReleaseArrowDown]],
"left":[[WindowEvent.PressArrowLeft],[WindowEvent.ReleaseArrowLeft]],
"right":[[WindowEvent.PressArrowRight],[WindowEvent.ReleaseArrowRight]],
"a":[[WindowEvent.PressButtonA],[WindowEvent.ReleaseButtonA]],
"b":[[WindowEvent.PressButtonB],[WindowEvent.ReleaseButtonB]],
"start":[[WindowEvent.PressButtonStart],[WindowEvent.ReleaseButtonStart]],
"select":[[WindowEvent.PressButtonSelect],[WindowEvent.ReleaseButtonSelect]]}
self.pyboy.sendInput(keys[key][0])
self.ticks(10)
self.pyboy.sendInput(keys[key][1])
self.ticks(200)
self.savegame()
def getScreenshot(self):
screenshot = self.pyboy.window.getScreenBuffer()
im = Image.new('RGBA', (288, 320))
pixellist = []
for line in screenshot:
for pixel in line:
pixelstring = str(hex(pixel))
if len(pixelstring) < 8:
pixelstring = "0x" + ('0'*(8-len(pixelstring))) + pixelstring[2:]
r = int(pixelstring[2:4], 16)
g = int(pixelstring[4:6], 16)
b = int(pixelstring[6:8], 16)
pixellist.append((r, g, b))
im.putdata(pixellist)
transposed = im.transpose(Image.TRANSPOSE)
return transposed
def read(self, address):
return self.pyboy.getMemoryValue(address)
# def main():
# bootROM = None
# ROMdir = "ROM/"
# scale = 2
# try:
# # Check if the ROM is given through argv
# if len(sys.argv) > 1:
# filename = sys.argv[1]
# else:
# filename = getROM(ROMdir)
#
# # Start PyBoy and run loop
# pyboy = PyBoy(Window(scale=scale), filename, bootROM)
# frame = 0
# view = pyboy.getTileView(False)
# while not pyboy.tick():
# print("frame: " + str(frame))
#
# frame += 1
# pyboy.stop()
#
# except KeyboardInterrupt:
# print ("Interrupted by keyboard")
# except Exception as ex:
# traceback.print_exc()