Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

kotlin - How do i create a function that adds already drawn blocks to a list?

currently i have an uni project which involves on building a simpler version of the game arkanoid (ball that reflects on racket destroying blocks) on canvas using kotlin. We use intelij. Im trying to create a function that based on a mutable list of bricks, builds a brick on canvas and adds it to the list, and when the ball hits it , it disapears and gets removed from that list.

So far i have this

enum class TypeOfBrick(val color: Int, val points: Int){
    Sb(SILVER,0),
    Wb(WHITE,1),
    Ob(ORANGE,2),
    Cb(CYAN,3),
    Gb(GREEN,4),
    Rb(RED,6),
    Bb(BLUE,7),
    Mb(MAGENTA,8),
    Yb(YELLOW,9),
}

data class Brick(val x: Int, val y: Int, val height: Int = Brick_Height, val width: Int = Brick_Width, val typeOfBrick: TypeOfBrick)

val brickList = mutableListOf<TypeOfBrick>()

fun createBrick(x: Int = Random.nextInt(32, Width-Brick_Width), y: Int = Random.nextInt(3*Brick_Height, 9*Brick_Height), typeOfBrick: TypeOfBrick): Brick{
    return Brick(x, y, Brick_Height, Brick_Width, typeOfBrick)
}

fun Canvas.drawBrick(brick: Brick){
    this.drawRect(brick.x, brick.y, Brick_Width, Brick_Height, brick.typeOfBrick.color)
}

fun addBricksToList(canvas: Canvas, brick: Brick) {
    /*
     * HERE: I know inside here i can use the for loop,
     * but i just cant seem to do it.
     */
}
question from:https://stackoverflow.com/questions/65859322/how-do-i-create-a-function-that-adds-already-drawn-blocks-to-a-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...