吃鱼小游戏,可以让小朋友练习键盘方向键和鼠标,采用javafx框架tornadofx实现...


import javafx.application.Application
import javafx.stage.Stage
import javafx.scene.paint.Color
import javafx.scene.text.Font
import javafx.scene.text.FontWeight
import javafx.animation.AnimationTimer
import javafx.beans.Observable
import javafx.collections.ObservableList
import javafx.geometry.Pos
import javafx.scene.image.Image
import javafx.scene.input.KeyEvent
import javafx.scene.input.MouseEvent
import javafx.util.Duration
import tornadofx.*
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.sin
import kotlin.random.Random

fun main(args: Array<String>) = Application.launch(eatFishApp::class.java, *args)

class eatFishApp : App(eatFishView::class,Styles::class) {
    val v: eatFishView by inject()
    override fun start(stage: Stage) {
        super.start(stage)
        stage.scene.setOnKeyPressed(keyPressedHandler)
        stage.scene.setOnKeyReleased(keyReleasedHandler)
    }

    private val keyPressedHandler: (KeyEvent) -> Unit = { e ->
        val code = e.code.toString()
        if (!v.input.contains(code))
            v.input.add(code)
    }
    private val keyReleasedHandler: (KeyEvent) -> Unit = { e ->
        v.input.remove(e.code.toString())
    }
}

class eatFishView : View("我要吃了你!") {
    val lastNanoTime = longProperty(System.nanoTime())
    val elapsedTime = doubleProperty()
    val canvasSize = 800.0
    val score = intProperty(0)
    val input = mutableListOf<String>()
    val briefcase = Sprite("collectMoney/shayu.gif", 200.0, 0.0,80.0,80.0)
    var moneybagList = initFish()
    val canReset = booleanProperty(moneybagList.isEmpty())

    val theFont = Font.font("Helvetica", FontWeight.BOLD, 24.0)
    var gc = canvas(canvasSize, canvasSize).graphicsContext2D

    override val root = borderpane {
        primaryStage.isResizable = false
        top = hbox(10) {
            alignment=Pos.CENTER
            paddingAll=10
            imageview(Image("collectMoney/tiger.gif",50.0,50.0,false,true))
            button("Reset") {
                prefHeight=50.0
                prefWidth=200.0

                enableWhen(canReset)
                action {
                    moneybagList=initFish()
                    run{
                        moneybagList.map {
                            it.render(gc)
                        }
                    }
                    score.value=0
                }
            }
            label(score.stringBinding{"已经吃了: $it 条鱼了"})
        }
        val mouseMoveListener: (MouseEvent) -> Unit = { e ->
            briefcase.setPosition(e.x - 30, e.y - 60)
        }
        center = canvas(canvasSize+200, canvasSize) {
            gc = this.graphicsContext2D
            gc.font = theFont
            gc.fill = Color.GREEN
            gc.stroke = Color.BLACK
            gc.lineWidth = 1.0

            setOnMouseMoved(mouseMoveListener)
        }
    }

    init {
        AnimT().start()
    }

    inner class AnimT : AnimationTimer() {
        override fun handle(currentNanoTime: Long) {
            // calculate time since last update.
            elapsedTime.value = (currentNanoTime - lastNanoTime.value) / 1000000000.0
            lastNanoTime.value = currentNanoTime

            // game logic
            briefcase.setVelocity(0.0, 0.0)
            if (input.contains("LEFT"))
                briefcase.addVelocity(-100.0, 0.0)
            if (input.contains("RIGHT"))
                briefcase.addVelocity(100.0, 0.0)
            if (input.contains("UP"))
                briefcase.addVelocity(0.0, -100.0)
            if (input.contains("DOWN"))
                briefcase.addVelocity(0.0, 100.0)

            briefcase.update(elapsedTime.value)

            // collision detection
            val moneybagIter = moneybagList.iterator()
            while (moneybagIter.hasNext()) {
                val moneybag = moneybagIter.next()
                if (briefcase.intersects(moneybag)) {
                    moneybagIter.remove()
                    score.value++
                }
            }

            canReset.value = moneybagList.isEmpty()
            // render
            gc.clearRect(0.0, 0.0, canvasSize+200, canvasSize)
            briefcase.render(gc)
            moneybagList.map { it.render(gc) }
        }
    }

    fun initFish():ObservableList<Sprite>{
       return (0..14).map {
            Sprite("collectMoney/$it.gif", 700 * Math.random() + 40, 700 * Math.random() + 40,80.0,80.0)
        }.asObservable()
    }
}
class Styles : Stylesheet() {
    init {
        button {
            padding = box(10.px)
            alignment = Pos.CENTER
            backgroundColor += c("#DD7549")
            fontWeight = FontWeight.EXTRA_BOLD
            fontSize=28.px

            and (hover) {
                backgroundColor += c("#A05434")
                textFill = Color.WHITE
            }
        }
        label{
            fontSize=28.px
            textFill = Color.RED
        }
    }
}

 

转载于:https://my.oschina.net/u/3820046/blog/3101908

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在JavaFX中设置打印纸张大小,可以使用`PageLayout`类和`PrinterJob`类。下面是一些基本的代码示例: ```java PrinterJob job = PrinterJob.createPrinterJob(); if (job != null) { boolean success = job.showPrintDialog(stage); if (success) { // 获取默认打印机 Printer printer = Printer.getDefaultPrinter(); // 创建一个页面布局,指定纸张大小、页面方向和边距 Paper paper = new Paper(); double width = ...; // 设置纸张宽度 double height = ...; // 设置纸张高度 double leftMargin = ...; // 设置左边距 double rightMargin = ...; // 设置右边距 double topMargin = ...; // 设置上边距 double bottomMargin = ...; // 设置下边距 paper.setSize(width, height); paper.setImageableArea(leftMargin, topMargin, width - leftMargin - rightMargin, height - topMargin - bottomMargin); PageLayout pageLayout = printer.createPageLayout(paper, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT); // 获取要打印的节点 Node node = ... // 设置打印作业属性 job.setPrinter(printer); job.setJobName("My Document"); job.setPageLayout(pageLayout); // 执行打印操作 boolean printed = job.printPage(node); if (printed) { job.endJob(); } } } ``` 其中,`createPrinterJob()`方法用于创建一个打印作业。`showPrintDialog()`方法用于显示打印对话框,用户可以在该对话框中选择打印机和打印设置。`getDefaultPrinter()`方法用于获取默认打印机。`createPageLayout()`方法用于创建一个页面布局,可以指定纸张大小、页面方向和边距。`setSize()`方法用于设置纸张大小。`setImageableArea()`方法用于设置可打印区域的边距。`setPrinter()`方法用于设置打印机。`setJobName()`方法用于设置打印作业名称。`setPageLayout()`方法用于设置打印页面的布局。`printPage()`方法用于将要打印的节点添加到打印作业中,并执行打印操作。`endJob()`方法用于结束打印作业。 需要注意的是,要设置打印纸张大小,必须先创建一个页面布局,然后将页面布局传递给`setPageLayout()`方法。创建页面布局时,需要指定纸张大小、页面方向和边距。纸张大小应该与打印机支持的纸张大小相匹配,否则可能会导致打印失败。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值