[그루비와 프로세싱] 퐁게임(미완성)

19096 단어 processingGroovy
공을 화면 가장자리 막대로 되돌리는 게임.
동영상 내에서 도중까지 밖에 만들지 않기 때문에 같은 곳까지 밖에 만들지 않았다.

키보드 입력 부분이 동영상과 달리 키를 누르고 있는 사이와 키를 말했을 때의 2개로 나누어 실장했다(할 수밖에 없었다)

동영상 목록 : LWJGL 튜토리얼
동영상은 코코

DisplayTest.groovy
package episode009

import groovy.swing.SwingBuilder
import processing.core.PApplet

import javax.swing.*

class DisplayTest extends PApplet {

    def lastFrame

    def bat
    def ball

    def void setup() {
        setUpDisplay()
        setUpEntities()
        setUpTimer()
    }

    def void draw() {
        renderEntities()
        logic(delta)
    }

    def setUpDisplay() {
        frameRate(60)
    }

    def setUpEntities() {
        bat = new Bat(this, 10, height / 2 - 80 / 2, 10, 80)
        ball = new Ball(this, width / 2 - 10 / 2, height / 2 - 10 / 2, 10)
        ball.dx = -0.1
    }

    def setUpTimer() {
        lastFrame = System.currentTimeMillis()
    }

    def renderEntities() {
        background(0, 0, 0)
        ball.draw()
        bat.draw()
    }

    def logic(delta) {
        ball.update(delta)
        bat.update(delta)
        if (ball.x <= bat.x + bat.width
                && ball.x >= bat.x
                && ball.y >= bat.y
                && ball.y <= bat.y + bat.height) {
            ball.dx = 0.3
        }
    }

    def void keyPressed() {
        switch (keyCode) {
            case UP:
                bat.dy = -0.2
                break
            case DOWN:
                bat.dy = 0.2
                break
        }
    }

    def void keyReleased() {
        bat.dy = 0
    }

    def getDelta() {
        def currentTime = System.currentTimeMillis()
        def delta = currentTime - lastFrame
        lastFrame = currentTime
        delta
    }

    def static void main(args) {
        def display = new DisplayTest()
        new SwingBuilder().frame(
                title: 'Episode 9',
                defaultCloseOperation: JFrame.EXIT_ON_CLOSE,
                size: [640, 480], show: true) {
            widget(display)
        }
        display.init()
    }
}

class Entity {
    def x, y, width, height
    def display

    def Entity(display, x, y, width, height) {
        this.display = display
        this.x = x
        this.y = y
        this.width = width
        this.height = height
    }

    def setLocation(x, y) {
        this.x = x
        this.y = y
    }

    def intersects(other) {
        x < other.x && y < other.y && x + width > other.x && y + height > other.y
    }
}

class MovableEntity extends Entity {
    def dx, dy

    def MovableEntity(display, x, y, width, height) {
        super(display, x, y, width, height)
        this.dx = 0
        this.dy = 0
    }

    def update(delta) {
        x += delta * dx
        y += delta * dy
    }
}

class Bat {
    @Delegate MovableEntity entity

    def Bat(display, x, y, width, height) {
        this.entity = new MovableEntity(display, x, y, width, height)
    }

    def draw() {
        def posX = x
        def posY = y
        def boxWidth = width
        def boxHeight = height
        display.with {
            noStroke()
            fill(255, 255, 255)
            rect(posX, posY, boxWidth, boxHeight)
        }
    }
}

class Ball {
    def weight
    @Delegate MovableEntity entity

    def Ball(display, x, y, weight) {
        this.entity = new MovableEntity(display, x, y, weight, weight)
        this.weight = weight
    }

    def draw() {
        def posX = x
        def posY = y
        display.with {
            noSmooth()
            stroke(255, 255, 255)
            strokeWeight(weight)
            point(posX, posY)
        }
    }
}

좋은 웹페이지 즐겨찾기