class TitleScreenTests(TestCase): def setUp(self): self.events = EventGenerator() self.result = '' def one(self): self.result += '1' def two(self): self.result += '2' def three(self): self.result += '3' def test_show_title(self, screen): """Displays a main menu.""" menu = [ ('One', self.one), ('Two', self.two), ('Three', self.three), ] title = TitleScreen(screen, self.events, TITLE_RECT, TITLE_IMAGE, menu, MENU_RECT, VERTICAL_MOVES) self.events.add_scripted_keys([K_UP, K_RETURN, K_DOWN, K_DOWN, K_SPACE, K_RETURN, K_UP, K_RETURN, K_UP], converter=int) self.events.add_scripted_event(QUIT_EVENT) title.run() self.assertEqual(self.result, '3')
class TitleScreenTests(TestCase): def setUp(self): self.events = EventGenerator() self.result = '' def one(self): self.result += '1' def two(self): self.result += '2' def three(self): self.result += '3' def test_show_title(self, screen): """Displays a main menu.""" menu = [ ('One', self.one), ('Two', self.two), ('Three', self.three), ] title = TitleScreen(screen, self.events, TITLE_RECT, TITLE_IMAGE, menu, MENU_RECT, VERTICAL_MOVES) self.events.add_scripted_keys([ K_UP, K_RETURN, K_DOWN, K_DOWN, K_SPACE, K_RETURN, K_UP, K_RETURN, K_UP ], converter=int) self.events.add_scripted_event(QUIT_EVENT) title.run() self.assertEqual(self.result, '3')
def test_anykey_negative(self): self.keypressed = False self.egen = EventGenerator() self.elis = AnyKeyListener(self.callback) self.egen.add_listener(self.elis) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert not self.keypressed
def test_game_paused(self, screen): """Display pause box.""" frame = Frame(screen, Rect(100, 100, 260, 160)) egen = EventGenerator() egen.add_scripted_keys(['a']) gp = GamePausedBox(frame, config.PAUSE_IMAGE, text='test game paused signal', egen=egen) gp.draw() pygame.display.update() gp.activate()
def test_highscores(self): """Enters something into the highscore list.""" hl = HighscoreList(HIGHSCORE_FILE) events = EventGenerator() events.add_scripted_keys(["B", "C", chr(K_RETURN), chr(K_RETURN)]) events.add_scripted_event(QUIT_EVENT) TEST_GAME_CONTEXT.screen.clear() frame = Frame(TEST_GAME_CONTEXT.screen, HIGHSCORE_RECT) hs = HighscoreBox(frame, events, hl, HIGHSCORE_IMAGE, HIGHSCORE_TEXT_POS) hs.enter_score(333) self.assertEqual(hl.scores[0], (333, "BC")) hs.activate()
def setup(self): self.egen = EventGenerator() keymap = { pygame.K_a: self.hel, pygame.K_b: self.lo, pygame.K_c: self.world, pygame.K_d: self.space } self.elis = EventListener(keymap) self.egen.add_listener(self.elis) self.updated = 0 self.result = ""
def test_highscores(self, screen): """Enters something into the highscore list.""" os.system('cp %s %s' % (HIGHSCORE_BACKUP, config.HIGHSCORE_FILE)) hl = HighscoreList(config.HIGHSCORE_FILE) events = EventGenerator() events.add_scripted_keys(['B', 'C', chr(K_RETURN), chr(K_RETURN)]) events.add_scripted_event(QUIT_EVENT) screen.clear() frame = Frame(screen, config.HIGHSCORE_RECT) hs = HighscoreBox(frame, events, hl, config.HIGHSCORE_IMAGE, config.HIGHSCORE_TEXT_POS) hs.enter_score(33333) hs.activate() assert hl.scores[0] == (33333, 'BC')
def show_title_screen(screen, rect, image, menu, menu_rect, moves): # self.settings.KEY_REPEAT = self.settings.MENU_KEY_REPEAT events = EventGenerator() title = TitleScreen(screen, events, rect, image, menu, menu_rect, moves) title.run() screen.clear() pygame.display.update()
def test_anykey_negative(self): self.keypressed = False self.egen = EventGenerator() self.elis = AnyKeyListener(self.callback) self.egen.add_listener(self.elis) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertFalse(self.keypressed)
def setUp(self): self.egen = EventGenerator() keymap = { 'a': self.hel, 'b': self.lo, 'c': self.world, 'd': self.space } self.elis = EventListener(keymap) self.egen.add_listener(self.elis) self.updated = 0 self.result = ""
def __init__(self, frame, image=None, text="Game Paused - press any key to continue", egen=None): """Initializes the Pause Box.""" self.image = ImageBox(frame, image) self.text = TextBox(frame, text) if egen == None: egen = EventGenerator() self.egen = egen self.elis = AnyKeyListener(self.pause_ended) self.egen.add_listener(self.elis)
def show_highscores(new_score, screen, rect, filename, image, textpos): """ Display high score list and gets a name if the score is high enough. """ screen.clear() frame = Frame(screen, rect) events = EventGenerator() hs = HighscoreList(filename) hs = HighscoreBox(frame, events, hs, image, textpos) hs.enter_score(new_score) hs.activate() screen.clear() pygame.display.update()
class SlidingPuzzle: def __init__(self): self.screen = Screen(Vector(350, 350), 'data/background.png') frame = Frame(self.screen, Rect(64, 64, 320, 320)) tile_factory = TileFactory('data/tiles.conf') self.tm = TiledMap(frame, tile_factory) self.gap = Vector(4, 4) self.tm.set_map(PUZZLEMAP) self.tm.draw() self.events = None pygame.display.update() def move(self, direction): start = self.gap - direction if self.tm.at(start) == '#': return move = MapMove(self.tm, start, direction, 2) wait_for_move(move, self.screen, self.tm.draw, 0.01) self.gap = start self.check_complete() def get_same(self, row): counter = Counter(row) return counter.most_common(1)[0][1] def check_complete(self): s = self.tm.get_map() rows = s.split('\n') same = [self.get_same(row) for row in rows[1:5]] if sum(same) == 15: print("\nCongratulations!\n") self.events.exit_signalled() def run(self): self.events = EventGenerator() self.events.add_listener(FigureMoveListener(self.move)) self.events.event_loop()
def event_gen(): return EventGenerator()
class AnyKeyListenerTests(TestCase): def callback(self): self.keypressed = True def test_anykey_negative(self): self.keypressed = False self.egen = EventGenerator() self.elis = AnyKeyListener(self.callback) self.egen.add_listener(self.elis) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertFalse(self.keypressed) def test_anykey_positive(self): self.keypressed = False self.egen = EventGenerator() self.elis = AnyKeyListener(self.callback) self.egen.add_listener(self.elis) self.egen.add_scripted_keys('a') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertTrue(self.keypressed)
class TestEvent: def hel(self): self.result += "Hel" def lo(self): self.result += "lo" def world(self): self.result += "World" def space(self): self.result += " " def setup(self): self.egen = EventGenerator() keymap = { pygame.K_a: self.hel, pygame.K_b: self.lo, pygame.K_c: self.world, pygame.K_d: self.space } self.elis = EventListener(keymap) self.egen.add_listener(self.elis) self.updated = 0 self.result = "" def update(self): """Counts how many times the method was called""" # callback function called from EventGenerator() self.updated += 1 def test_event_generator(self): """Generate some key events and check whether they arrive.""" self.egen.add_scripted_keys('acb') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "HelWorldlo" def test_callback(self): """Pass scripted keys as a string.""" self.egen.add_callback(self) self.egen.add_scripted_keys('abdc') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "Hello World" assert self.updated == 5 def test_scripted_repeat(self): """Pass scripted keys as a string.""" self.egen.add_callback(self) self.egen.add_scripted_keys('aaaa') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.updated == 5
class EventTests(TestCase): def hel(self): self.result += "Hel" def lo(self): self.result += "lo" def world(self): self.result += "World" def space(self): self.result += " " def setUp(self): self.egen = EventGenerator() keymap = { 'a': self.hel, 'b': self.lo, 'c': self.world, 'd': self.space } self.elis = EventListener(keymap) self.egen.add_listener(self.elis) self.updated = 0 self.result = "" def update(self): """Counts how many times the method was called""" # callback function called from EventGenerator() self.updated += 1 def test_event_generator(self): """Generate some key events and check whether they arrive.""" self.egen.add_scripted_keys('acb') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "HelWorldlo") def test_callback(self): """Pass scripted keys as a string.""" self.egen.add_callback(self) self.egen.add_scripted_keys('abdc') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "Hello World") self.assertEqual(self.updated, 5) def test_scripted_repeat(self): """Pass scripted keys as a string.""" self.egen.add_callback(self) self.egen.add_scripted_keys('aaaa') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.updated, 5)
def setUp(self): self.egen = EventGenerator() self.updated = 0 self.result = ""
class TextEnteringListenerTests(TestCase): def setUp(self): self.egen = EventGenerator() self.updated = 0 self.result = "" def text_finished(self, text): """Callback for listener.""" self.result = text def key_pressed(self, text): """Counts how many times the method was called""" self.updated += 1 def test_text_entering(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "ACBD") self.assertEqual(self.updated, 4) def test_return_missing(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "") def test_delete(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_DELETE)) self.egen.add_scripted_keys('efg') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "ACBEFG") self.assertEqual(self.updated, 8) def test_no_upper(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished, False) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() self.assertEqual(self.result, "acbd") self.assertEqual(self.updated, 4)
class TestTextEnteringListener: def setup(self): self.egen = EventGenerator() self.updated = 0 self.result = "" def text_finished(self, text): """Callback for listener.""" self.result = text def key_pressed(self, text): """Counts how many times the method was called""" self.updated += 1 def test_text_entering(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "ACBD" assert self.updated == 4 def test_return_missing(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "" def test_delete(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_DELETE)) self.egen.add_scripted_keys('efg') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "ACBEFG" assert self.updated == 8 def test_no_upper(self): """Generate some key events and check whether they arrive.""" elis = TextEnteringListener(self.key_pressed, self.text_finished, False) self.egen.add_listener(elis) self.egen.add_scripted_keys('acbd') self.egen.add_scripted_keys(chr(K_RETURN)) self.egen.add_scripted_event(QUIT_EVENT) self.egen.event_loop() assert self.result == "acbd" assert self.updated == 4
def setup(self): self.egen = EventGenerator() self.updated = 0 self.result = ""
def setUp(self): self.events = EventGenerator() self.result = ''
def run(self): self.events = EventGenerator() self.events.add_listener(FigureMoveListener(self.move)) self.events.event_loop()