Войти
BASIC[ProjectYaga]Форум

Реализация игры Mario Demo

#0
(Правка: 13:10) 13:00, 11 янв. 2021
test game mario | Реализация игры Mario Demo
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
'#include <SFML/Network.hpp>


DIM offsetX AS FLOAT = 0
DIM offsetY AS FLOAT = 0
CONST H AS INTEGER = 17
CONST W AS INTEGER = 150


'ARRAY_STATIC_2D TileMap2(H, W) AS STRING

DIM TileMap[H] AS STRING = { _
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", _
"0                                                                                                                                                    0", _
"0                                                                                    w                                                               0", _
"0                   w                                  w                   w                                  w                                      0", _
"0                                      w                                                        k                                 w                  0", _
"0                                                                             kkkk   kk   kkkk      kk                   w                           0", _
"0                      c                                                      k  k  k  k  k     k  k  k                                              0", _
"0                                                                       r     kkk   k  k  kkkk  k  k                                    w            0", _
"0                                                                      rr     k  k  kkkk     k  k  k  k                                     Z        0", _
"0                                                                     rrr     kkkk  k  k  kkkk  k   kk                                               0", _
"0               c    kckck                                           rrrr                                                         rrrrrr             0", _
"0                                      t0                           rrrrr                                                  rrrrrr                    0", _
"0G                                     00              t0          rrrrrr            G                               rrrrr                           0", _
"0           d    g       d             00              00         rrrrrrr                                                                            0", _
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", _
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", _
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP", _
}


#CODE C++ <CODE>
/*
std::string TileMap[H] = {
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"0                                                                                                                                                    0",
"0                                                                                    w                                                               0",
"0                   w                                  w                   w                                  w                                      0",
"0                                      w                                                        k                                 w                  0",
"0                                                                             kkkk   kk   kkkk      kk                   w                           0",
"0                      c                                                      k  k  k  k  k     k  k  k                                              0",
"0                                                                       r     kkk   k  k  kkkk  k  k                                    w            0",
"0                                                                      rr     k  k  kkkk     k  k  k  k                                     Z        0",
"0                                                                     rrr     kkkk  k  k  kkkk  k   kk                                               0",
"0               c    kckck                                           rrrr                                                         rrrrrr             0",
"0                                      t0                           rrrrr                                                  rrrrrr                    0",
"0G                                     00              t0          rrrrrr            G                               rrrrr                           0",
"0           d    g       d             00              00         rrrrrrr                                                                            0",
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
};
*/
</CODE> #END CODE C++

CLASS PLAYER

'PRIVATE:
PUBLIC:
  DIM dx AS FLOAT
  DIM dy AS FLOAT

  DIM rect AS sf.FloatRect
  DIM onGround AS BOOLEAN
  DIM sprite AS sf.Sprite
  DIM currentFrame AS FLOAT
  DIM life AS BOOLEAN
  
  'DISPOSE - диструктор
  'NEW - конструктор
  NEW PLAYER(BYREF image AS sf.Texture)
    sprite.setTexture(image)
    rect = sf.FloatRect(100, 180, 16, 16)
    dx = 0.1
    dy = 0.1
    currentFrame = 0
  END NEW

  SUB update(BYVAL time AS FLOAT)
    rect.left = rect.left + (dx * time)
    Collision(0)
    IF NOT(onGround) THEN dy = dy + (0.0005 * time)
    rect.top = rect.top + (dy * time)
    onGround = FALSE
    Collision(1)
    currentFrame = currentFrame + (time * 0.005)
    IF (currentFrame > 3) THEN currentFrame = currentFrame - 3
    IF (dx > 0) THEN sprite.setTextureRect(sf.IntRect(112 + 31 * INT(currentFrame), 144, 16, 16))
    IF (dx < 0) THEN sprite.setTextureRect(sf.IntRect(112 + 31 * INT(currentFrame) + 16, 144, -16, 16))
    sprite.setPosition(rect.left - offsetX, rect.top - offsetY)
    dx = 0
  END SUB

  SUB Collision(BYVAL num AS INTEGER)
    DIM i AS INTEGER
    DIM j AS INTEGER
    i = rect.top / 16 
    DO WHILE (i < (rect.top + rect.height) / 16)
      j = rect.left / 16
      DO WHILE (j < (rect.left + rect.width) / 16)

        IF ((TileMap[i][j] = ASC("P")) OR (TileMap[i][j] = ASC("k")) OR (TileMap[i][j] = ASC("0")) _
                        OR (TileMap[i][j] = ASC("r")) OR (TileMap[i][j] = ASC("t"))) THEN
          IF (dy > 0 AND num = 1) THEN 
            rect.top = i * 16 - rect.height
            dy = 0
            onGround = TRUE
          END IF
          IF (dy < 0 AND num = 1) THEN
            rect.top = i * 16 + 16
            dy = 0
          END IF
          IF (dx > 0 AND num = 0) THEN
            rect.left = j * 16 - rect.width
          END IF 
          IF (dx < 0 AND num = 0) THEN
            rect.left = j * 16 + 16
          END IF
        END IF

        IF (TileMap[i][j] = ASC("c")) THEN
          'TileMap[i][j] = ASC(" ")
        END IF
        j = j + 1
      LOOP
      i = i + 1
    LOOP
  END SUB

END CLASS


CLASS ENEMY

PUBLIC:
  DIM dx AS FLOAT
  DIM dy AS FLOAT
  DIM rect AS sf.FloatRect
  DIM sprite AS sf.Sprite
  DIM currentFrame AS FLOAT
  DIM life AS BOOLEAN
  DIM onGround AS BOOLEAN

  SUB set(BYREF image AS sf.Texture, BYVAL x AS INTEGER, BYVAL y AS INTEGER)
    sprite.setTexture(image)
    rect = sf.FloatRect(x, y, 16, 16)
    dx = 0.05
    currentFrame = 0
    life = TRUE
  END SUB


  SUB update(BYVAL time AS FLOAT)
    rect.left = rect.left + (dx * time)
    Collision()
    currentFrame = currentFrame + (time * 0.005)
    IF (currentFrame > 2) THEN currentFrame = currentFrame - 2
    sprite.setTextureRect(sf.IntRect(18 * INT(currentFrame), 0, 16, 16))
    IF NOT(life) THEN sprite.setTextureRect(sf.IntRect(58, 0, 16, 16))
    sprite.setPosition(rect.left - offsetX, rect.top - offsetY)
  END SUB

  SUB Collision()
    DIM i AS INTEGER
    DIM j AS INTEGER
    i = rect.top / 16 
    DO WHILE (i < (rect.top + rect.height) / 16)
      j = rect.left / 16
      DO WHILE (j < (rect.left + rect.width) / 16)

        IF ((TileMap[i][j] = ASC("P")) OR (TileMap[i][j] = ASC("0"))) THEN
          IF (dx > 0) THEN
            rect.left = j * 16 - rect.width
            dx = dx * (-1)
          ELSE
            IF (dx < 0) THEN
              rect.left = j * 16 + 16
              dx = dx * (-1)
            END IF
          END IF
        END IF
        j = j + 1
      LOOP
      i = i + 1
    LOOP
  END SUB

END CLASS


FUNCTION main(BYVAL COMMANDCOUNT AS INTEGER, BYPTR COMMAND[] AS CHAR) AS INTEGER

  setlocale(LC_ALL, "RUS")

  SET window(sf.VideoMode(400, 250), "MARIO") AS sf.RenderWindow
  DIM tileSet AS sf.Texture
  tileSet.loadFromFile("Mario_Tileset.png")

  SET Mario(tileSet) AS PLAYER
  SET enemy AS ENEMY

  enemy.set(tileSet, 48 * 16, 13 * 16)
  DIM tile(tileSet) AS sf.Sprite

  DIM event AS sf.Event

  'меню
  DIM menu_texture1 AS sf.Texture
  DIM menu_texture2 AS sf.Texture
  DIM menu_texture3 AS sf.Texture
  DIM about_texture AS sf.Texture
  menu_texture1.loadFromFile("m1.png")
  menu_texture2.loadFromFile("m2.png")
  menu_texture3.loadFromFile("m3.png")
  about_texture.loadFromFile("about.png")
  DIM menu1(menu_texture1) AS sf.Sprite
  DIM menu2(menu_texture2) AS sf.Sprite
  DIM menu3(menu_texture3) AS sf.Sprite
  DIM about(about_texture) AS sf.Sprite
  DIM Menu AS BOOLEAN = TRUE
  DIM MenuNum AS INTEGER = 0
  menu1.setPosition(100, 30)
  menu2.setPosition(100, 90)
  menu3.setPosition(100, 150)

  DO WHILE (Menu)
    DO WHILE (window.pollEvent(event))
      IF (event.type = sf.Event.Closed) THEN
        window.close()
        RETURN 0
      END IF
    LOOP
    
    menu1.setColor(sf.Color.White)
    menu2.setColor(sf.Color.White)
    menu3.setColor(sf.Color.White)
    MenuNum = 0
    window.clear(sf.Color(0, 0, 0))

    IF (sf.IntRect(100, 30, 300, 50).contains(sf.Mouse.getPosition(window))) THEN
      menu1.setColor(sf.Color.Yellow)
      MenuNum = 1
    END IF
    IF (sf.IntRect(100, 90, 300, 50).contains(sf.Mouse.getPosition(window))) THEN
      menu2.setColor(sf.Color.Yellow)
      MenuNum = 2
    END IF
    IF (sf.IntRect(100, 150, 300, 50).contains(sf.Mouse.getPosition(window))) THEN
      menu3.setColor(sf.Color.Yellow)
      MenuNum = 3
    END IF

    ' IF (sf.Keyboard.isKeyPressed(sf.Keyboard.Escape)) AND MenuNum <> 2 THEN 
      ' window.close()
      ' Menu = FALSE
      ' RETURN 0
    ' END IF
    
    IF (sf.Mouse.isButtonPressed(sf.Mouse.Left)) THEN
      IF (MenuNum = 1) THEN Menu = FALSE
      IF (MenuNum = 2) THEN
        window.draw(about)
        window.display()
        DO WHILE (NOT(sf.Keyboard.isKeyPressed(sf.Keyboard.Escape)))
          DO WHILE (window.pollEvent(event))
            IF (event.type = sf.Event.Closed) THEN
              window.close()
              RETURN 0
            END IF
          LOOP
        LOOP
      END IF
      IF (MenuNum = 3) THEN
        window.close()
        Menu = FALSE
      END IF
    END IF
    window.draw(menu1)
    window.draw(menu2)
    window.draw(menu3)
    window.display()
  LOOP


  DIM buffer AS sf.SoundBuffer 
  buffer.loadFromFile("Jump.ogg")
  DIM sound AS sf.Sound   
  sound.setBuffer(buffer)
  DIM music AS sf.Music
  music.openFromFile("Mario_Theme.ogg")
  music.play()

  SET clock AS sf.Clock

  DO WHILE (window.isOpen()) 'DO WHILE (window.isOpen() OR (NOT(sf.Keyboard.isKeyPressed(sf.Keyboard.Escape))))
  
    DIM time AS FLOAT = clock.getElapsedTime().asMicroseconds()
    clock.restart()
    time = time / 500 'здесь регулируем скорость игры
    IF (time > 20) THEN time = 20 
    
    'DIM event AS sf.Event
    DO WHILE (window.pollEvent(event))
      IF (event.type = sf.Event.Closed) THEN window.close()
    LOOP
    
    IF (sf.Keyboard.isKeyPressed(sf.Keyboard.Escape)) THEN window.close()
    IF (sf.Keyboard.isKeyPressed(sf.Keyboard.Left)) THEN Mario.dx = -0.1
    IF (sf.Keyboard.isKeyPressed(sf.Keyboard.Right)) THEN Mario.dx = 0.1
    IF (sf.Keyboard.isKeyPressed(sf.Keyboard.Up)) THEN
      IF (Mario.onGround) THEN 
        Mario.dy = -0.27
        Mario.onGround = FALSE
        sound.play()
      END IF
    END IF
    
    Mario.update(time)
    enemy.update(time)
    
    IF (Mario.rect.intersects(enemy.rect)) THEN
      IF (enemy.life) THEN
        IF (Mario.dy > 0) THEN
          enemy.dx = 0
          Mario.dy = -0.2
          enemy.life = FALSE
        ELSE 
          Mario.sprite.setColor(sf.Color.Red)
        END IF
      END IF
    END IF
    IF (Mario.rect.left > 200) THEN offsetX = Mario.rect.left - 200 'смещение

    window.clear(sf.Color(107, 140, 255))

    DIM i AS INTEGER
    DIM j AS INTEGER
    FOR i = 0 TO H - 1
      FOR j = 0 TO W - 1
        IF (TileMap[i][j] = ASC("P")) THEN tile.setTextureRect(sf.IntRect(143 - 16 * 3, 112, 16, 16))
        IF (TileMap[i][j] = ASC("k")) THEN tile.setTextureRect(sf.IntRect(143, 112, 16, 16))
        IF (TileMap[i][j] = ASC("c")) THEN tile.setTextureRect(sf.IntRect(143 - 16, 112, 16, 16))
        IF (TileMap[i][j] = ASC("t")) THEN tile.setTextureRect(sf.IntRect(0, 47, 32, 95 - 47))
        IF (TileMap[i][j] = ASC("g")) THEN tile.setTextureRect(sf.IntRect(0, 16 * 9 - 5, 3 * 16, 16 * 2 + 5))
        IF (TileMap[i][j] = ASC("G")) THEN tile.setTextureRect(sf.IntRect(145, 222, 222 - 145, 255 - 222))
        IF (TileMap[i][j] = ASC("d")) THEN tile.setTextureRect(sf.IntRect(0, 106, 74, 127 - 106))
        IF (TileMap[i][j] = ASC("w")) THEN tile.setTextureRect(sf.IntRect(99, 224, 140 - 99, 255 - 224))
        IF (TileMap[i][j] = ASC("r")) THEN tile.setTextureRect(sf.IntRect(143 - 32, 112, 16, 16))
        IF (TileMap[i][j] = ASC("Z")) THEN tile.setTextureRect(sf.IntRect(95, 0, 107, 111))
        IF ((TileMap[i][j] = ASC(" ")) OR (TileMap[i][j] = ASC("0"))) THEN CONTINUE FOR
        tile.setPosition(j * 16 - offsetX, i * 16 - offsetY) 
        window.draw(tile)
      NEXT
    NEXT

    window.draw(Mario.sprite)
    window.draw(enemy.sprite)
    window.display()
  LOOP

  RETURN 0

END FUNCTION

BASIC[ProjectYaga]Форум