Advertisement
Guest User

Untitled

a guest
Oct 25th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.10 KB | None | 0 0
  1. //main.cs
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using SFML.Audio;
  6. using SFML.Graphics;
  7. using SFML.Window;
  8. using SFML;
  9. using System.Collections.Generic;
  10.  
  11. namespace ContraSharp
  12. {
  13.     public class Program
  14.     {
  15.  
  16.         static Player player;
  17.         static Menu menu;
  18.  
  19.         static void Main()
  20.         {
  21.             RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML.Net Game", Styles.Default);
  22.  
  23.             Map map = new Map(new Texture("images/tileset.png"));
  24.             player = new Player(new Texture("images/sprites.png"), map, window);
  25.  
  26.             map.LoadTileMap(File.ReadAllLines("maps/map.dat"));
  27.  
  28.             Menu menu = new Menu(new Texture("images/sprites.png"), window);
  29.             menu.onMenu = false;
  30.  
  31.             window.Closed += new EventHandler(OnClosed);
  32.             window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);
  33.  
  34.             Stopwatch sw = new Stopwatch();
  35.             while (window.IsOpen())
  36.             {
  37.                 window.DispatchEvents();
  38.                 float time = sw.ElapsedMilliseconds * 1;//game speed
  39.                 sw.Restart();
  40.                 window.Clear(Color.Black);
  41.                 if (menu.onMenu == true)
  42.                 {
  43.                     menu.Update(time);
  44.                     window.Display();
  45.                     System.Threading.Thread.Sleep(1);
  46.                     continue;
  47.                 }
  48.                
  49.                 player.Update(time);
  50.  
  51.                 if (player.rect.Left > window.Size.X / 2)
  52.                     map.offsetX = (int)(player.rect.Left - window.Size.X / 2);
  53.                 map.offsetY = (int)(player.rect.Top - window.Size.Y / 2);
  54.  
  55.                
  56.  
  57.                 map.objects.Update(time);
  58.  
  59.                 for (int i = 0; i < map.height; i++)
  60.                     for (int j = 0; j < map.width; j++)
  61.                     {
  62.                         if (map.tileMap[i][j] == ' ')
  63.                             continue;
  64.  
  65.                         GameObject o = map.objects[map.tileMap[i][j]];
  66.                         o.sprite.Position = new Vector2f(j * 16 - map.offsetX, i * 16 - map.offsetY);
  67.                         window.Draw(o.sprite);
  68.                     }
  69.                 window.Clear(Color.Black);
  70.                 window.Display();
  71.                 window.DispatchEvents();
  72.  
  73.                 System.Threading.Thread.Sleep(1);
  74.  
  75.             }
  76.  
  77.         }
  78. //..//
  79.     }
  80. }
  81. //player.cs
  82. using System;
  83. using SFML.Audio;
  84. using SFML.Graphics;
  85. using SFML.Window;
  86. using System.Collections.Generic;
  87.  
  88. namespace ContraSharp
  89. {
  90.  
  91.     public class Player
  92.     {
  93.  
  94.  
  95.         public bool isLife = true, onGround;
  96.         public Sprite sprite;
  97.         public FloatRect rect;
  98.         public float dx, dy, currentFrame, frameCount;
  99.         public Direction direction;
  100.         public Map map;
  101.         public int Bullet_Number = 0;
  102.         public RenderWindow window;
  103.  
  104.         public Player(Texture texture, Map map, RenderWindow win)
  105.         {
  106.             window = win;
  107.             sprite = new Sprite(texture);
  108.             rect = new FloatRect(100, 100, 20, 40);
  109.             direction = Direction.Right;
  110.             this.map = map;
  111.         }
  112.  
  113.         public void Update(float time)
  114.         {
  115.             rect.Left += dx * time;
  116.  
  117.             Collision(Direction.Horizontal);
  118.  
  119.             if(!onGround)
  120.                 dy = dy + 0.0006f * time;
  121.  
  122.             rect.Top += dy * time;
  123.             onGround = false;
  124.  
  125.             Collision(Direction.Vertical);
  126.  
  127.             frameCount = dx != 0 & dy == 0 ? 6 : 6;
  128.             currentFrame += 0.006f * time;
  129.             if(currentFrame > frameCount)
  130.                 currentFrame -= frameCount;
  131.             if (dx > 0)
  132.             {
  133.                 if (currentFrame > 0 && currentFrame < 1) sprite.TextureRect = new IntRect(130, 127, 25, 35);
  134.                 else if (currentFrame > 1 && currentFrame < 2) sprite.TextureRect = new IntRect(100, 127, 25, 35);
  135.                 else if (currentFrame > 2 && currentFrame < 3) sprite.TextureRect = new IntRect(70, 127, 25, 35);
  136.                 else if (currentFrame > 3 && currentFrame < 4) sprite.TextureRect = new IntRect(40, 127, 25, 35);
  137.                 else if (currentFrame > 4 && currentFrame < 5) sprite.TextureRect = new IntRect(100, 127, 25, 35);
  138.                 else if (currentFrame > 5 && currentFrame < 6) sprite.TextureRect = new IntRect(160, 127, 25, 35);
  139.             }
  140.             else if (dx < 0)
  141.             {
  142.                 if (currentFrame > 0 && currentFrame < 1) sprite.TextureRect = new IntRect(130+25, 127, -25, 35);
  143.                 else if (currentFrame > 1 && currentFrame < 2) sprite.TextureRect = new IntRect(100+25, 127, -25, 35);
  144.                 else if (currentFrame > 2 && currentFrame < 3) sprite.TextureRect = new IntRect(70+25, 127, -25, 35);
  145.                 else if (currentFrame > 3 && currentFrame < 4) sprite.TextureRect = new IntRect(40+25, 127, -25, 35);
  146.                 else if (currentFrame > 4 && currentFrame < 5) sprite.TextureRect = new IntRect(100+25, 127, -25, 35);
  147.                 else if (currentFrame > 5 && currentFrame < 6) sprite.TextureRect = new IntRect(160+25, 127, -25, 35);
  148.             }
  149.             if (dx == 0)
  150.             {
  151.                 sprite.TextureRect = direction == Direction.Right ? new IntRect(43, 14, 23, 35) : new IntRect(43+23, 14, -23, 35);
  152.             }
  153.             if (dy != 0)
  154.             {
  155.                 if (currentFrame > 0 && currentFrame < 1) sprite.TextureRect = direction == Direction.Right ? new IntRect(87, 167, 20, 20) : new IntRect(87+20, 167, -20, 20);
  156.                 else if (currentFrame > 1 && currentFrame < 2) sprite.TextureRect = direction == Direction.Right ? new IntRect(116, 167, 20, 20) : new IntRect(116+20, 167, -20, 20);
  157.                 else if (currentFrame > 2 && currentFrame < 3) sprite.TextureRect = direction == Direction.Right ?  new IntRect(142, 167, 20, 20) : new IntRect(142+20, 167, -20, 20);
  158.                 else if (currentFrame > 3 && currentFrame < 4) sprite.TextureRect = direction == Direction.Right ? new IntRect(170, 167, 20, 20) : new IntRect(170+20, 167, -20, 20);
  159.                 else currentFrame-=4;
  160.             }
  161.             sprite.Position = new Vector2f(rect.Left - 10 - map.offsetX, rect.Top - 10 - map.offsetY);
  162.             window.Draw(sprite);
  163.         }
  164.  
  165.         /*...*/
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement