SHARE
TWEET

abc

a guest Jul 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package wld.objs
  2.  
  3. import java.awt.Color
  4. import java.awt.Graphics
  5. import java.awt.Rectangle
  6. import java.util.*
  7. import javax.swing.JFrame
  8. import javax.swing.JPanel
  9. import kotlin.collections.ArrayList
  10.  
  11. const val STEP = 2
  12. const val BOT_SIZE = 2
  13. const val WIDTH = 800
  14. const val HEIGHT = 600
  15. const val SLEEP_TIME:Long = 15
  16. const val BOT_POPULATION = 50000
  17. const val DNA_LEN = 1500
  18.  
  19. fun main() {
  20.     val thread = Thread(Runnable {
  21.         println("start new world session")
  22.         val world = World()
  23.         while (!world.isWorldNotTerminated){
  24.             world.update()
  25.             world.render()
  26.             Thread.sleep(SLEEP_TIME)
  27.         }
  28.     })
  29.     thread.start()
  30. }
  31.  
  32. class View(private val objects:ArrayList<Bot>):JPanel(){
  33.  
  34.     override fun paintComponent(g: Graphics?) {
  35.         for (obj in objects){
  36.             obj.draw(g)
  37.         }
  38.     }
  39. }
  40.  
  41. class Bot {
  42.  
  43.     private val selfColor = Color.getHSBColor(Random().nextFloat(),Random().nextFloat(),Random().nextFloat())
  44.     private var xPos:Int = 0
  45.     private var yPos:Int = 0
  46.     private var dna = mutableListOf<Int>()
  47.     private var curDnaIndex = 0
  48.  
  49.  
  50.     init {
  51.         for (i in 0 until DNA_LEN){
  52.             dna.add((Math.random()*4).toInt())
  53.         }
  54.  
  55.         //println("dna chain: $dna")
  56.  
  57.         xPos = WIDTH/2
  58.         yPos = HEIGHT/2
  59.         //println("new bot coors are {x:$xPos y:$yPos}")
  60.     }
  61.  
  62.     fun draw(g: Graphics?) {
  63.         g!!.color = selfColor
  64.         g.fillRect(xPos,yPos, BOT_SIZE, BOT_SIZE)
  65.     }
  66.  
  67.     fun update() {
  68.  
  69.         if (curDnaIndex >= DNA_LEN){
  70.             curDnaIndex = 0
  71.             //println("dna chain finished and reloaded")
  72.         }
  73.  
  74.         when(dna[curDnaIndex++]){
  75.             0 -> xPos += STEP
  76.             1 -> xPos -= STEP
  77.             2 -> yPos += STEP
  78.             3 -> yPos -= STEP
  79.         }
  80.  
  81.         when(xPos){
  82.             WIDTH -> xPos = 0
  83.             -BOT_SIZE -> xPos = WIDTH - BOT_SIZE
  84.         }
  85.  
  86.         when(yPos){
  87.             HEIGHT -> yPos = 0
  88.             -BOT_SIZE -> yPos = HEIGHT - BOT_SIZE
  89.         }
  90.     }
  91. }
  92.  
  93. class World {
  94.  
  95.     var isWorldNotTerminated: Boolean = false
  96.     private val frame = JFrame("title")
  97.     private val objects = ArrayList<Bot>()
  98.     private val view = View(objects)
  99.  
  100.     init {
  101.  
  102.         for (i in 0 until BOT_POPULATION){
  103.             objects.add(Bot())
  104.         }
  105.  
  106.         frame.bounds = Rectangle(0,0, WIDTH, HEIGHT)
  107.         frame.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
  108.         frame.isResizable = false
  109.         frame.contentPane.add(view)
  110.         frame.background = Color.BLACK
  111.         frame.isVisible = true
  112.     }
  113.  
  114.     fun update() {
  115.         for (obj in objects){
  116.             obj.update()
  117.         }
  118.     }
  119.  
  120.     fun render() {
  121.         frame.repaint()
  122.         view.repaint()
  123.     }
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top