Skip to content

Commit

Permalink
correções no exemplo Catch The Stars
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiagodemas committed Dec 21, 2017
1 parent 7b65fd3 commit e7cf39d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 25 deletions.
Binary file modified .cache-main
Binary file not shown.
35 changes: 18 additions & 17 deletions src/exemplo/CatchTheStars.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import com.sun.org.apache.bcel.internal.generic.Select
object CatchTheStars extends App {

case class Player(var x: Double, var y: Double) {
val image = Image("Nave.png")
val img = Image("Spacecraft.png")
var score = 0
var vel_x, vel_y = 0.0
var angle = 0.0

def draw() = {
image.centralized_draw(x, y, 3, angle)
img.centralized_draw(x, y, 3, angle)
}

def rotate_right() = {
Expand All @@ -42,7 +42,7 @@ object CatchTheStars extends App {
}

def catch_the_stars(stars: List[Star]) = {
val caught = Select.stars {
val caught = stars.filter{
star => Game.distance(x, y, star.x, star.y) >= 35
}
val n = stars.size - caught.size
Expand All @@ -56,14 +56,14 @@ object CatchTheStars extends App {
val x = r.nextInt(game.width)
val y = r.nextInt(game.height)
val color = ColorF(r.nextInt(216) + 40, r.nextInt(216) + 40, r.nextInt(216) + 40)
val images = Image.fatie("Estrela.png", 25, 25)
val images = Image.fatie("Star.png", 25, 25)
val i = r.nextInt(images.size)



def draw() = {
val image = images((Clock.milisegundos / 100 + i) % images.size)
image.centralized_draw(x, y, 1)
image.centralized_draw(x, y, 1) //PROBLEMA N IDENTIFICADO

}
}
Expand Down Expand Up @@ -92,54 +92,55 @@ object CatchTheStars extends App {
}
}

// Estado Inicio
// State start
def update_start() = {
if (Keyboard.KEY_I) state = "PLAYING"
}

def draw_start() = {
val msg = "PRESSIONE [ I ] PARA COMEÇAR"
val msg = "PRESS [I] TO GET STARTED"
font.draw_centered(msg, game.width / 2, game.height / 2, 3, ColorF.YELLOW)
}

// Estado Jogando
// State Player
def update_playing() = {
val r = scala.util.Random
// eventos
if (Keyboard.RIGHT) spacecraft.rotate_right
if (Keyboard.LEFT) spacecraft.rotate_left
if (Keyboard.UP) spacecraft.accelerate
// inserir novas estrelas estrelas se necessario
// insert new stars if necessary
if (r.nextInt(100) < 4 && stars.size < 25) {
stars = Star() :: stars
}

stars = spacecraft.catch_the_stars(stars) // catar estrelas
spacecraft.move // atualizar a posicao do jogador
time = time + 1.0 / 60.0 // incrementar o tempo
stars = spacecraft.catch_the_stars(stars) // Catch the stars //PROBLEMA N IDENTIFICADO
spacecraft.move // update player position
time = time + 1.0 / 60.0 // increase time
if (time.intValue() >= 30) {
state = "END" // terminar o jogo depois de 30 segundos
state = "END" // finish the game after 30 seconds
}
}

def draw_player() = {
spacecraft.draw
for (star <- stars) {
star.draw
star.draw //PROBLEMA N IDENTIFICADO

}
font.draw(s"Placar: ${spacecraft.score}", 10, 20, 3, ColorF.YELLOW)
font.draw(s"Time: ${time.toInt}s", 10, 40, 3, ColorF.YELLOW)
}

// Estado: fim do jogo
// state: Game over
def draw_end() = {
val msg = s"GAME OVER, VOCE FEZ ${spacecraft.score} PONTOS"
val msg = s"GAME OVER, YOU SCORED ${spacecraft.score} POINTS"
font.draw_centered(msg, game.width / 2, game.height / 2, 3, ColorF.YELLOW)
}

def update_end() = {}

game.start("Cata Estrelas", 640, 480, update, draw)
game.start("Catch the Stars", 640, 480, update, draw)


}
6 changes: 3 additions & 3 deletions src/jerimum/Animation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ case class Animation(speed: Int, images: List[Image]) {
private[this] val start = System.currentTimeMillis()
private[this] val size = images.length

def imagem(): Image = {
val indice = ((System.currentTimeMillis() - start) / speed % size).toInt
images(indice)
def image(): Image = {
val index = ((System.currentTimeMillis() - start) / speed % size).toInt
images(index)
}
}
6 changes: 3 additions & 3 deletions src/jerimum/Game.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scala.util.{ Failure, Try }


object Game extends Runnable {
var title: String = "Sem Nome"
var title: String = "Unnamed"
var width: Int = 640
var height: Int = 480
var fps: Int = 60
Expand Down Expand Up @@ -64,7 +64,7 @@ object Game extends Runnable {
time = 0
}
}
parar()
stop()
}

def start(title: String = "Potigol com Jerimum", width: Int = 640,
Expand All @@ -84,7 +84,7 @@ object Game extends Runnable {
}
}

private[this] def parar() = synchronized {
private[this] def stop() = synchronized {
if (running) {
running = false
Try(thread.join()) match {
Expand Down
4 changes: 2 additions & 2 deletions src/jerimum/Image.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Image(val buffer: BufferedImage, val road: String = "") {
List(l)
}

private[this] def girar(g: Graphics2D, angle: Double, x: Double, y: Double, scalaX: Double, scalaY: Double)(draw: => Unit): Unit = {
private[this] def spin(g: Graphics2D, angle: Double, x: Double, y: Double, scalaX: Double, scalaY: Double)(draw: => Unit): Unit = {
val old = g.getTransform()
if (angle != 0.0) g.rotate(Math.toRadians(angle), x + buffer.getWidth / 2, y + buffer.getHeight / 2)
draw
Expand All @@ -74,7 +74,7 @@ class Image(val buffer: BufferedImage, val road: String = "") {

def draw(x: Double, y: Double, z: Int, angle: Double = 0.0, scalaX: Double = 1.0, scalaY: Double = 1.0): Unit = {
Draw.include(z, g => {
girar(g, angle, x, y, scalaX, scalaY) {
spin(g, angle, x, y, scalaX, scalaY) {
val width = (buffer.getWidth * scalaX).toInt
val height = (buffer.getHeight * scalaY).toInt
val deltaX = if (width < 0) -width else 0
Expand Down

0 comments on commit e7cf39d

Please sign in to comment.