manage actions in game logic
This commit is contained in:
parent
dfbfb093e0
commit
3223521d67
@ -23,8 +23,6 @@ WINDOW_HEIGHT = 600
|
|||||||
WINDOW_TITLE = "TETRARCADE"
|
WINDOW_TITLE = "TETRARCADE"
|
||||||
|
|
||||||
# Delays (seconds)
|
# Delays (seconds)
|
||||||
AUTOREPEAT_DELAY = 0.220 # Official : 0.300
|
|
||||||
AUTOREPEAT_INTERVAL = 0.010 # Official : 0.010
|
|
||||||
HIGHLIGHT_TEXT_DISPLAY_DELAY = 1
|
HIGHLIGHT_TEXT_DISPLAY_DELAY = 1
|
||||||
|
|
||||||
# Text
|
# Text
|
||||||
@ -166,10 +164,8 @@ class TetrArcade(Tetris, arcade.Window):
|
|||||||
arcade.key.ENTER: self.new_game
|
arcade.key.ENTER: self.new_game
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.autorepeatable_actions = (self.move_left, self.move_right, self.soft_drop)
|
|
||||||
|
|
||||||
self.highlight_texts = []
|
self.highlight_texts = []
|
||||||
self.pressed_actions = []
|
|
||||||
|
|
||||||
arcade.Window.__init__(
|
arcade.Window.__init__(
|
||||||
self,
|
self,
|
||||||
@ -199,8 +195,6 @@ class TetrArcade(Tetris, arcade.Window):
|
|||||||
|
|
||||||
def new_game(self):
|
def new_game(self):
|
||||||
self.highlight_texts = []
|
self.highlight_texts = []
|
||||||
self.pressed_actions = []
|
|
||||||
self.auto_repeat = False
|
|
||||||
super().new_game()
|
super().new_game()
|
||||||
|
|
||||||
def new_current_piece(self):
|
def new_current_piece(self):
|
||||||
@ -211,23 +205,14 @@ class TetrArcade(Tetris, arcade.Window):
|
|||||||
|
|
||||||
def lock(self):
|
def lock(self):
|
||||||
super().lock()
|
super().lock()
|
||||||
if self.pressed_actions:
|
|
||||||
self.stop_autorepeat()
|
|
||||||
self.scheduler.start(self.repeat_action, AUTOREPEAT_DELAY)
|
|
||||||
|
|
||||||
def swap(self):
|
def swap(self):
|
||||||
super().swap()
|
super().swap()
|
||||||
self.reload_held_piece()
|
self.reload_held_piece()
|
||||||
self.reload_current_piece()
|
self.reload_current_piece()
|
||||||
|
|
||||||
def pause(self):
|
|
||||||
super().pause()
|
|
||||||
self.pressed_actions = []
|
|
||||||
self.stop_autorepeat()
|
|
||||||
|
|
||||||
def game_over(self):
|
def game_over(self):
|
||||||
super().game_over()
|
super().game_over()
|
||||||
self.scheduler.stop(self.repeat_action)
|
|
||||||
|
|
||||||
def on_key_press(self, key, modifiers):
|
def on_key_press(self, key, modifiers):
|
||||||
for key_or_modifier in (key, modifiers):
|
for key_or_modifier in (key, modifiers):
|
||||||
@ -236,11 +221,7 @@ class TetrArcade(Tetris, arcade.Window):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
action()
|
self.do_action(action)
|
||||||
if action in self.autorepeatable_actions:
|
|
||||||
self.stop_autorepeat()
|
|
||||||
self.pressed_actions.append(action)
|
|
||||||
self.scheduler.start(self.repeat_action, AUTOREPEAT_DELAY)
|
|
||||||
|
|
||||||
def on_key_release(self, key, modifiers):
|
def on_key_release(self, key, modifiers):
|
||||||
try:
|
try:
|
||||||
@ -248,27 +229,10 @@ class TetrArcade(Tetris, arcade.Window):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if action in self.autorepeatable_actions:
|
self.remove_action(action)
|
||||||
try:
|
|
||||||
self.pressed_actions.remove(action)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def repeat_action(self):
|
|
||||||
if self.pressed_actions:
|
|
||||||
self.pressed_actions[-1]()
|
|
||||||
if not self.auto_repeat:
|
|
||||||
self.auto_repeat = True
|
|
||||||
self.scheduler.stop(self.repeat_action)
|
|
||||||
self.scheduler.start(self.repeat_action, AUTOREPEAT_INTERVAL)
|
|
||||||
else:
|
|
||||||
self.stop_autorepeat()
|
|
||||||
|
|
||||||
def stop_autorepeat(self):
|
|
||||||
self.auto_repeat = False
|
|
||||||
self.scheduler.stop(self.repeat_action)
|
|
||||||
|
|
||||||
def show_text(self, text):
|
def show_text(self, text):
|
||||||
|
self.scheduler.stop(self.del_highlight_text)
|
||||||
self.highlight_texts.append(text)
|
self.highlight_texts.append(text)
|
||||||
self.scheduler.start(self.del_highlight_text, HIGHLIGHT_TEXT_DISPLAY_DELAY)
|
self.scheduler.start(self.del_highlight_text, HIGHLIGHT_TEXT_DISPLAY_DELAY)
|
||||||
|
|
||||||
|
50
tetris.py
50
tetris.py
@ -10,6 +10,8 @@ NB_NEXT_PIECES = 5
|
|||||||
# Delays (seconds)
|
# Delays (seconds)
|
||||||
LOCK_DELAY = 0.5
|
LOCK_DELAY = 0.5
|
||||||
FALL_DELAY = 1
|
FALL_DELAY = 1
|
||||||
|
AUTOREPEAT_DELAY = 0.220 # Official : 0.300
|
||||||
|
AUTOREPEAT_PERIOD = 0.010 # Official : 0.010
|
||||||
|
|
||||||
|
|
||||||
class Coord:
|
class Coord:
|
||||||
@ -197,6 +199,8 @@ class Tetris():
|
|||||||
self.current_piece = None
|
self.current_piece = None
|
||||||
self.held_piece = None
|
self.held_piece = None
|
||||||
self.time = 0
|
self.time = 0
|
||||||
|
self.autorepeatable_actions = (self.move_left, self.move_right, self.soft_drop)
|
||||||
|
self.pressed_actions = []
|
||||||
|
|
||||||
def new_game(self):
|
def new_game(self):
|
||||||
self.level = 0
|
self.level = 0
|
||||||
@ -205,6 +209,9 @@ class Tetris():
|
|||||||
self.goal = 0
|
self.goal = 0
|
||||||
self.time = 0
|
self.time = 0
|
||||||
|
|
||||||
|
self.pressed_actions = []
|
||||||
|
self.auto_repeat = False
|
||||||
|
|
||||||
self.lock_delay = LOCK_DELAY
|
self.lock_delay = LOCK_DELAY
|
||||||
self.fall_delay = FALL_DELAY
|
self.fall_delay = FALL_DELAY
|
||||||
|
|
||||||
@ -348,6 +355,9 @@ class Tetris():
|
|||||||
|
|
||||||
self.current_piece.prelocked = False
|
self.current_piece.prelocked = False
|
||||||
self.scheduler.stop(self.lock)
|
self.scheduler.stop(self.lock)
|
||||||
|
if self.pressed_actions:
|
||||||
|
self.stop_autorepeat()
|
||||||
|
self.scheduler.start(self.repeat_action, AUTOREPEAT_DELAY)
|
||||||
|
|
||||||
if all(
|
if all(
|
||||||
(mino_position + self.current_piece.position).y >= NB_LINES
|
(mino_position + self.current_piece.position).y >= NB_LINES
|
||||||
@ -459,14 +469,46 @@ class Tetris():
|
|||||||
self.scheduler.start(self.lock, self.lock_delay)
|
self.scheduler.start(self.lock, self.lock_delay)
|
||||||
self.scheduler.start(self.clock, 1)
|
self.scheduler.start(self.clock, 1)
|
||||||
|
|
||||||
def clock(self, delta_time=1):
|
|
||||||
self.time += delta_time
|
|
||||||
|
|
||||||
def game_over(self):
|
def game_over(self):
|
||||||
self.status = Status.OVER
|
self.status = Status.OVER
|
||||||
self.scheduler.stop(self.drop)
|
self.scheduler.stop(self.drop)
|
||||||
self.scheduler.stop(self.clock)
|
self.scheduler.stop(self.clock)
|
||||||
|
self.scheduler.stop(self.repeat_action)
|
||||||
|
|
||||||
|
def clock(self, delta_time=1):
|
||||||
|
self.time += delta_time
|
||||||
|
|
||||||
|
def do_action(self, action):
|
||||||
|
action()
|
||||||
|
if action in self.autorepeatable_actions:
|
||||||
|
self.stop_autorepeat()
|
||||||
|
self.pressed_actions.append(action)
|
||||||
|
if action == self.drop:
|
||||||
|
self.scheduler.start(self.repeat_action, AUTOREPEAT_PERIOD)
|
||||||
|
else:
|
||||||
|
self.scheduler.start(self.repeat_action, AUTOREPEAT_DELAY)
|
||||||
|
|
||||||
|
def repeat_action(self):
|
||||||
|
if self.pressed_actions:
|
||||||
|
self.pressed_actions[-1]()
|
||||||
|
if not self.auto_repeat:
|
||||||
|
self.auto_repeat = True
|
||||||
|
self.scheduler.stop(self.repeat_action)
|
||||||
|
self.scheduler.start(self.repeat_action, AUTOREPEAT_PERIOD)
|
||||||
|
else:
|
||||||
|
self.stop_autorepeat()
|
||||||
|
|
||||||
|
def remove_action(self, action):
|
||||||
|
if action in self.autorepeatable_actions:
|
||||||
|
try:
|
||||||
|
self.pressed_actions.remove(action)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def stop_autorepeat(self):
|
||||||
|
self.auto_repeat = False
|
||||||
|
self.scheduler.stop(self.repeat_action)
|
||||||
|
|
||||||
def show_text(self, text):
|
def show_text(self, text):
|
||||||
print(text)
|
raise NotImplementedError
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user