Facebook
From archkubi, 1 Year ago, written in C.
This paste is a reply to Untitled from archkubi - view diff
Embed
Download Paste or View Raw
Hits: 354
  1. import nimraylib_now
  2. import std/random
  3.  
  4. ## window and window size
  5. var screenWidth = 1024
  6. var screenHeight = 600
  7. ## SetTraceLogLevel(LOG_ERROR) this is not in
  8. initWindow(screenWidth, screenHeight,
  9.            "raylib [textures] example - texture to image")
  10.  
  11. ## background
  12. var image: Image = loadImage("/home/archkubi/Github/gnuchange/nimRaylib/src/img/bg.png")
  13. var texture: Texture2D = loadTextureFromImage(image)
  14.  
  15. ## player
  16. var playerFront: Image = loadImage("/home/archkubi/Github/gnuchange/nimRaylib/src/img/front.png")
  17. var playerLeft: Image = loadImage("/home/archkubi/Github/gnuchange/nimRaylib/src/img/left.png")
  18. var playerRight: Image = loadImage("/home/archkubi/Github/gnuchange/nimRaylib/src/img/right.png")
  19.  
  20. var player: Texture2D = loadTextureFromImage(playerFront)
  21. var xPos = screenWidth div 2 - 90
  22. var yPos = screenHeight div 2 - 64 + 180
  23.  
  24. ## gravity
  25. var gravity = 5
  26. var jumForce = 120
  27. var isJumping = false
  28. var groundHeight = screenHeight / 2 - 275
  29.  
  30. ## astroid
  31. type
  32.   AstroObject = object
  33.     texture: Image
  34.     x, y: int
  35.     speed: float
  36.     active: bool
  37.  
  38.  
  39. var numAstros = 5
  40. var astros: seq[AstroObject]
  41.  
  42. for i in 0 ..< numAstros:
  43.   var astro_texture = loadImage("/home/archkubi/Github/gnuchange/nimRaylib/src/img/astro.png")
  44.   var astro_x = random.rand(906)
  45.   var astro_y = random.rand(1000)
  46.   var astroSpeed = random.rand(5)
  47.   var astroObject = AstroObject(astro_texture, astro_x, astro_y, astroSpeed)
  48.   astros.add(astroObject)
  49.    
  50.  
  51. setTargetFPS(60)
  52. while not windowShouldClose():
  53.   if KeyboardKey.A.isKeyDown and xPos > 0:
  54.     xPos -= 5 * 1
  55.     player = loadTextureFromImage(playerLeft)
  56.   elif KeyboardKey.D.isKeyDown and xPos < 905:
  57.     xPos += 5 * 1
  58.     player = loadTextureFromImage(playerRight)
  59.   else:
  60.     player = loadTextureFromImage(playerFront)
  61.  
  62.   ## jump
  63.   var characterBaseY = yPos + player.height
  64.   var floor = int(screenHeight) - int(groundHeight)
  65.   if characterBaseY >= floor:
  66.     isJumping = false
  67.     yPos = floor - player.height
  68.   else:
  69.     yPos += gravity
  70.  
  71.   ## gravity
  72.   if KeyboardKey.SPACE.isKeyDown and not isJumping:
  73.     isJumping = true
  74.     yPos -= jumForce
  75.  
  76.   ## astro
  77.   for astro in astros:
  78.     astro.y += astro.speed
  79.     if astro.y > screenHeight:
  80.       astro.y = -random(1..1000)
  81.       astro.x = random(1..906)
  82.       astro.speed = random(1..4)
  83.    
  84.   ## normal
  85.   beginDrawing()
  86.   drawTexture(texture, screenWidth div 2 - texture.width div 2, screenHeight div 2 - texture.height div 2, White)
  87.   drawRectangle(0, 570, screenWidth, 30,  WHITE)
  88.   drawFPS(10, 10)
  89.   drawText("This is Nim", 200, 20, 50, WHITE)
  90.  
  91.   ## player
  92.   drawTexture(player, xPos, yPos, White)
  93.  
  94.   ## draw astros
  95.   for astro in astros:
  96.     if astro.active:
  97.       drawTexture(astro.texture, int(astro.x), int(astro.y), WHITE)
  98.  
  99.  
  100.  
  101.   endDrawing()
  102. closeWindow()