Skip to content

Commit

Permalink
Updated to use v2 methods, simplified inputs to use .button
Browse files Browse the repository at this point in the history
  • Loading branch information
TomCasavant committed May 16, 2024
1 parent 45f30df commit 0c2489a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 43 deletions.
53 changes: 11 additions & 42 deletions gb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import numpy as np
from moviepy.editor import ImageSequenceClip
from PIL import Image
from pyboy import PyBoy, WindowEvent
from pyboy import PyBoy


class Gameboy:
Expand Down Expand Up @@ -144,72 +144,41 @@ def load_rom(self, rom):
"""Loads the rom into a pyboy object"""
return PyBoy(
rom,
window_type="SDL2" if self.debug else "headless",
window_scale=3,
window="SDL2" if self.debug else "null",
debug=False,
game_wrapper=True,
)

def dpad_up(self) -> None:
"""Presses up on the gameboy"""
self.pyboy.send_input(WindowEvent.PRESS_ARROW_UP)
self.tick(4)
self.pyboy.send_input(WindowEvent.RELEASE_ARROW_UP)
self.pyboy.button('up', 3)

def dpad_down(self) -> None:
"""Presses down on the gameboy"""
self.pyboy.send_input(WindowEvent.PRESS_ARROW_DOWN)
print("down")
self.tick(3)
self.pyboy.send_input(WindowEvent.RELEASE_ARROW_DOWN)
# self.tick()
self.pyboy.button('down', 3)

def dpad_right(self) -> None:
"""Presses right on the gameboy"""
self.pyboy.send_input(WindowEvent.PRESS_ARROW_RIGHT)
print("right")
self.tick(3)
self.pyboy.send_input(WindowEvent.RELEASE_ARROW_RIGHT)
# self.tick()
self.pyboy.button('right', 3)

def dpad_left(self) -> None:
"""Presses left on the gameboy"""
self.pyboy.send_input(WindowEvent.PRESS_ARROW_LEFT)
print("left")
self.tick(3)
self.pyboy.send_input(WindowEvent.RELEASE_ARROW_LEFT)
# self.tick()
self.pyboy.button('left', 3)

def a(self) -> None:
"""Presses a on the gameboy"""
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_A)
print("a")
self.tick(3)
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_A)
# self.tick()
self.pyboy.button('a', 3)

def b(self) -> None:
"""Presses b on the gameboy"""
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_B)
print("b")
self.tick(3)
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_B)
# self.tick()
self.pyboy.button('b', 3)

def start(self) -> None:
"""Presses start on the gameboy"""
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_START)
print("start")
self.tick(3)
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_START)
# self.tick()
self.pyboy.button('start', 3)

def select(self) -> None:
"""Presses select on the gameboy"""
self.pyboy.send_input(WindowEvent.PRESS_BUTTON_SELECT)
print("select")
self.tick(3)
self.pyboy.send_input(WindowEvent.RELEASE_BUTTON_SELECT)
self.pyboy.button('select', 3)

def screenshot(self, path="screenshots"):
"""Takes a screenshot of gameboy screen and saves it to the path"""
Expand All @@ -230,7 +199,7 @@ def screenshot(self, path="screenshots"):
# Save the screenshot with the next available number
screenshot_path = os.path.join(screenshot_dir, f"screenshot_{next_number}.png")
screenshot_path_full = os.path.join(script_dir, "screenshot.png")
self.pyboy.screen_image().save(screenshot_path_full)
self.pyboy.screen.image.save(screenshot_path_full)
# Copy the screenshot to the screenshots directory
shutil.copyfile(screenshot_path_full, screenshot_path)
return screenshot_path_full
Expand Down
9 changes: 8 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
astroid==3.1.0
blurhash==1.1.4
certifi==2024.2.2
charset-normalizer==3.3.2
decorator==4.4.2
dill==0.3.8
idna==3.6
imageio==2.34.0
imageio-ffmpeg==0.4.9
isort==5.13.2
Mastodon.py==1.8.1
mccabe==0.7.0
moviepy==1.0.3
numpy==1.26.4
pillow==10.2.0
platformdirs==4.2.1
proglog==0.1.10
pyboy==1.6.14
pyboy==2.0.3
pylint==3.1.0
PySDL2==0.9.16
pysdl2-dll==2.30.0
python-dateutil==2.8.2
Expand All @@ -19,5 +25,6 @@ requests==2.31.0
setuptools==69.1.0
six==1.16.0
toml==0.10.2
tomlkit==0.12.4
tqdm==4.66.2
urllib3==2.2.0

0 comments on commit 0c2489a

Please sign in to comment.