Browse Source

added Player class for better abstraction of user input, implemented TestTtrsPlayer using pygame

feature/balanceutils
Fr3deric 8 years ago committed by Frederic
parent
commit
48fa0cb91e
  1. 100
      ttrs.py

100
ttrs.py

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import socket
import time import time
import enum import enum
import math import math
@ -174,14 +175,72 @@ class Playground():
for b in set.union(self.blocks, *[ m.blocks for m in self.minos ]): for b in set.union(self.blocks, *[ m.blocks for m in self.minos ]):
frame.setPixel(xpos + int(b.pos.x), ypos + int(b.pos.y), b.color) frame.setPixel(xpos + int(b.pos.x), ypos + int(b.pos.y), b.color)
class TtrsPlayer():
def __init__(self):
self.move = 0
self.drop = False
self.rotate = False
self.quit = False
class TestTtrsPlayer(TtrsPlayer):
def __init__(self):
self.__move = 0
self.__drop = False
self.__rotate = False
self.__quit = False
import pygame
self.__pygame = pygame
self.screen = pygame.display.set_mode((100, 100))
pygame.display.update()
def __process_events(self):
pygame = self.__pygame
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
self.__move = -1
elif event.key == pygame.K_d:
self.__move = 1
elif event.key == pygame.K_w:
self.__rotate = True
elif event.key == pygame.K_s:
self.__drop = True
elif event.key == pygame.K_ESCAPE:
self.__quit = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
self.__move = 0
elif event.key == pygame.K_w:
self.__rotate = False
elif event.key == pygame.K_s:
self.__drop = False
@property
def move(self):
self.__process_events()
return self.__move
@property
def drop(self):
self.__process_events()
return self.__drop
@property
def rotate(self):
self.__process_events()
return self.__rotate
@property
def quit(self):
self.__process_events()
return self.__quit
class TtrsGame(): class TtrsGame():
def __init__(self, playground): def __init__(self, playground, player):
self.playground = playground self.playground = playground
self.player = player
self.running = False self.running = False
self.next_move = 0
self.rotate = False
self.drop = False
self.tick_callbacks = [] self.tick_callbacks = []
def add_tick_callback(self, cb): def add_tick_callback(self, cb):
@ -200,23 +259,12 @@ class TtrsGame():
while self.running: while self.running:
for cb in self.tick_callbacks: for cb in self.tick_callbacks:
cb() cb()
#time.sleep(TICK_TIME) time.sleep(TICK_TIME)
ticks += 1 ticks += 1
x = input() if self.player.quit:
if x == 'a': self.running = False
self.next_move = -1 break
elif x == 'd':
self.next_move = 1
elif x == 'w':
self.rotate = True
elif x == 's':
self.drop = True
else:
self.drop = False
self.rotate = False
self.next_move = 0
if mino is None: if mino is None:
newminocls = random.choice(Tetrimino.__subclasses__()) newminocls = random.choice(Tetrimino.__subclasses__())
@ -236,7 +284,7 @@ class TtrsGame():
if ticks - lastfall >= FALL_INTERVAL or self.drop: if ticks - lastfall >= FALL_INTERVAL or self.player.drop:
lastfall = ticks lastfall = ticks
try: try:
mino.move(Point(0, 1)) mino.move(Point(0, 1))
@ -246,16 +294,14 @@ class TtrsGame():
self.playground.minos = set() self.playground.minos = set()
continue continue
if ticks - lastmove >= MOVE_INTERVAL and self.next_move != 0: if ticks - lastmove >= MOVE_INTERVAL and self.player.move != 0:
lastmove = ticks lastmove = ticks
try: try:
mino.move(Point(self.next_move, 0)) mino.move(Point(self.player.move, 0))
except InvalidMoveError: except InvalidMoveError:
pass pass
self.next_move = 0
if self.rotate: if self.player.rotate:
self.rotate = False
try: try:
mino.rotate() mino.rotate()
except InvalidMoveError: except InvalidMoveError:
@ -269,7 +315,6 @@ class TtrsGame():
if __name__ == '__main__': if __name__ == '__main__':
w = 22 w = 22
h = 16 h = 16
@ -287,7 +332,8 @@ if __name__ == '__main__':
pg.paint(frame, 0, 0) pg.paint(frame, 0, 0)
out.sendFrame(frame) out.sendFrame(frame)
game = TtrsGame(pg) player = TestTtrsPlayer()
game = TtrsGame(pg, player)
game.add_tick_callback(repaint) game.add_tick_callback(repaint)
game.run() game.run()

Loading…
Cancel
Save