Drop a piece

This commit is contained in:
Hajime Hoshi 2013-12-20 01:23:00 +09:00
parent 631b226b5b
commit 2ac027c452
3 changed files with 22 additions and 10 deletions

View File

@ -20,7 +20,7 @@ func (f *Field) IsBlocked(x, y int) bool {
if y < 0 {
return false
}
if fieldBlockNumX <= y {
if fieldBlockNumY <= y {
return true
}
return f.blocks[x][y] != BlockTypeNone
@ -44,6 +44,13 @@ func (f *Field) MovePieceToRight(piece *Piece, x, y int, angle Angle) int {
return x + 1
}
func (f *Field) DropPiece(piece *Piece, x, y int, angle Angle) int {
if f.collides(piece, x, y+1, angle) {
return y
}
return y + 1
}
func (f *Field) RotatePieceRight(piece *Piece, x, y int, angle Angle) Angle {
if f.collides(piece, x, y, angle.RotateRight()) {
return angle

View File

@ -36,9 +36,8 @@ const fieldWidth = blockWidth * fieldBlockNumX
const fieldHeight = blockHeight * fieldBlockNumY
func (s *GameScene) choosePiece() *Piece {
//num := NormalBlockTypeNum
//blockType := BlockType(s.rand.Intn(num) + 1)
blockType := BlockType1
num := NormalBlockTypeNum
blockType := BlockType(s.rand.Intn(num) + 1)
return Pieces[blockType]
}
@ -55,18 +54,24 @@ func (s *GameScene) Update(state *GameState) {
s.currentPiece, s.currentPieceX, s.currentPieceY,
s.currentPieceAngle)
}
l := state.Input.StateForKey(ui.KeyLeft)
if l == 1 || (10 <= l && l % 2 == 0) {
if l := state.Input.StateForKey(ui.KeyLeft);
l == 1 || (10 <= l && l % 2 == 0) {
s.currentPieceX = s.field.MovePieceToLeft(
s.currentPiece, s.currentPieceX, s.currentPieceY,
s.currentPieceAngle)
}
r := state.Input.StateForKey(ui.KeyRight)
if r == 1 || (10 <= r && r % 2 == 0) {
if r := state.Input.StateForKey(ui.KeyRight);
r == 1 || (10 <= r && r % 2 == 0) {
s.currentPieceX = s.field.MovePieceToRight(
s.currentPiece, s.currentPieceX, s.currentPieceY,
s.currentPieceAngle)
}
if d := state.Input.StateForKey(ui.KeyDown);
(d - 1) % 2 == 0 {
s.currentPieceY = s.field.DropPiece(
s.currentPiece, s.currentPieceX, s.currentPieceY,
s.currentPieceAngle)
}
}
func (s *GameScene) Draw(context graphics.Context) {

View File

@ -136,8 +136,8 @@ var cocoaKeyCodeToKey = map[int]ui.Key{
49: ui.KeySpace,
123: ui.KeyLeft,
124: ui.KeyRight,
125: ui.KeyUp,
126: ui.KeyDown,
125: ui.KeyDown,
126: ui.KeyUp,
}
//export ebiten_KeyDown