Last active
April 26, 2022 17:29
-
-
Save Pablito2020/720694dabc1f1528842b9162ea18c674 to your computer and use it in GitHub Desktop.
Silly example for working with my tetris library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import block_factory.RandomBlockCreator | |
import board.Cell | |
import game.GameCell | |
import game.ghost.GhostGame | |
import score.SimpleScoreCalculator | |
import java.awt.FlowLayout | |
import java.awt.Frame | |
import java.awt.Label | |
import java.awt.event.KeyEvent | |
import java.awt.event.KeyListener | |
class Main(private val gameFacade: GameFacade): KeyListener{ | |
override fun keyTyped(e: KeyEvent?) {} | |
override fun keyPressed(e: KeyEvent?) { | |
when(e?.keyCode) { | |
KeyEvent.VK_DOWN -> gameFacade.down() | |
KeyEvent.VK_LEFT -> gameFacade.left() | |
KeyEvent.VK_RIGHT -> gameFacade.right() | |
KeyEvent.VK_A -> gameFacade.rotateLeft() | |
KeyEvent.VK_S -> gameFacade.rotateRight() | |
} | |
paint(gameFacade.getGrid()) | |
} | |
override fun keyReleased(e: KeyEvent?) {} | |
fun paint(grid: List<List<GameCell>>) { | |
print("|----------|\n") | |
for (row in grid) { | |
print("|") | |
for (cell in row) | |
print(cell) | |
print("|\n") | |
} | |
print("|----------|\n\n\n\n\n") | |
} | |
private fun print(cell: GameCell) { | |
when(cell.cell) { | |
Cell.EMPTY -> print(" ") | |
else -> { | |
if (cell.isCurrentBlockCell) | |
print("I") | |
else if (cell.isGhostBlockCell) | |
print("*") | |
else | |
print("X") | |
} | |
} | |
} | |
} | |
fun main() { | |
val frame = Frame() | |
frame.layout = FlowLayout() | |
frame.setSize(500, 500) | |
val l = Label() | |
l.setText("This is a demonstration") | |
frame.add(l) | |
frame.setVisible(true) | |
val game = GameFacade(ghost = true) // Initialize with ghost block enabled. | |
game.start() | |
frame.addKeyListener(Main(game)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment