Browse Source

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

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

100
ttrs.py

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
#!/usr/bin/env python3
import socket
import time
import enum
import math
@ -174,14 +175,72 @@ class Playground(): @@ -174,14 +175,72 @@ class Playground():
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)
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():
def __init__(self, playground):
def __init__(self, playground, player):
self.playground = playground
self.player = player
self.running = False
self.next_move = 0
self.rotate = False
self.drop = False
self.tick_callbacks = []
def add_tick_callback(self, cb):
@ -200,23 +259,12 @@ class TtrsGame(): @@ -200,23 +259,12 @@ class TtrsGame():
while self.running:
for cb in self.tick_callbacks:
cb()
#time.sleep(TICK_TIME)
time.sleep(TICK_TIME)
ticks += 1
x = input()
if x == 'a':
self.next_move = -1
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 self.player.quit:
self.running = False
break
if mino is None:
newminocls = random.choice(Tetrimino.__subclasses__())
@ -236,7 +284,7 @@ class TtrsGame(): @@ -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
try:
mino.move(Point(0, 1))
@ -246,22 +294,19 @@ class TtrsGame(): @@ -246,22 +294,19 @@ class TtrsGame():
self.playground.minos = set()
continue
if ticks - lastmove >= MOVE_INTERVAL and self.next_move != 0:
if ticks - lastmove >= MOVE_INTERVAL and self.player.move != 0:
lastmove = ticks
try:
mino.move(Point(self.next_move, 0))
mino.move(Point(self.player.move, 0))
except InvalidMoveError:
pass
self.next_move = 0
if self.rotate:
self.rotate = False
if self.player.rotate:
try:
mino.rotate()
except InvalidMoveError:
pass
@ -287,7 +332,8 @@ if __name__ == '__main__': @@ -287,7 +332,8 @@ if __name__ == '__main__':
pg.paint(frame, 0, 0)
out.sendFrame(frame)
game = TtrsGame(pg)
player = TestTtrsPlayer()
game = TtrsGame(pg, player)
game.add_tick_callback(repaint)
game.run()

Loading…
Cancel
Save