Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //main.cs
- using System;
- using System.Diagnostics;
- using System.IO;
- using SFML.Audio;
- using SFML.Graphics;
- using SFML.Window;
- using SFML;
- using System.Collections.Generic;
- namespace ContraSharp
- {
- public class Program
- {
- static Player player;
- static Menu menu;
- static void Main()
- {
- RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML.Net Game", Styles.Default);
- Map map = new Map(new Texture("images/tileset.png"));
- player = new Player(new Texture("images/sprites.png"), map, window);
- map.LoadTileMap(File.ReadAllLines("maps/map.dat"));
- Menu menu = new Menu(new Texture("images/sprites.png"), window);
- menu.onMenu = false;
- window.Closed += new EventHandler(OnClosed);
- window.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);
- Stopwatch sw = new Stopwatch();
- while (window.IsOpen())
- {
- window.DispatchEvents();
- float time = sw.ElapsedMilliseconds * 1;//game speed
- sw.Restart();
- window.Clear(Color.Black);
- if (menu.onMenu == true)
- {
- menu.Update(time);
- window.Display();
- System.Threading.Thread.Sleep(1);
- continue;
- }
- player.Update(time);
- if (player.rect.Left > window.Size.X / 2)
- map.offsetX = (int)(player.rect.Left - window.Size.X / 2);
- map.offsetY = (int)(player.rect.Top - window.Size.Y / 2);
- map.objects.Update(time);
- for (int i = 0; i < map.height; i++)
- for (int j = 0; j < map.width; j++)
- {
- if (map.tileMap[i][j] == ' ')
- continue;
- GameObject o = map.objects[map.tileMap[i][j]];
- o.sprite.Position = new Vector2f(j * 16 - map.offsetX, i * 16 - map.offsetY);
- window.Draw(o.sprite);
- }
- window.Clear(Color.Black);
- window.Display();
- window.DispatchEvents();
- System.Threading.Thread.Sleep(1);
- }
- }
- //..//
- }
- }
- //player.cs
- using System;
- using SFML.Audio;
- using SFML.Graphics;
- using SFML.Window;
- using System.Collections.Generic;
- namespace ContraSharp
- {
- public class Player
- {
- public bool isLife = true, onGround;
- public Sprite sprite;
- public FloatRect rect;
- public float dx, dy, currentFrame, frameCount;
- public Direction direction;
- public Map map;
- public int Bullet_Number = 0;
- public RenderWindow window;
- public Player(Texture texture, Map map, RenderWindow win)
- {
- window = win;
- sprite = new Sprite(texture);
- rect = new FloatRect(100, 100, 20, 40);
- direction = Direction.Right;
- this.map = map;
- }
- public void Update(float time)
- {
- rect.Left += dx * time;
- Collision(Direction.Horizontal);
- if(!onGround)
- dy = dy + 0.0006f * time;
- rect.Top += dy * time;
- onGround = false;
- Collision(Direction.Vertical);
- frameCount = dx != 0 & dy == 0 ? 6 : 6;
- currentFrame += 0.006f * time;
- if(currentFrame > frameCount)
- currentFrame -= frameCount;
- if (dx > 0)
- {
- if (currentFrame > 0 && currentFrame < 1) sprite.TextureRect = new IntRect(130, 127, 25, 35);
- else if (currentFrame > 1 && currentFrame < 2) sprite.TextureRect = new IntRect(100, 127, 25, 35);
- else if (currentFrame > 2 && currentFrame < 3) sprite.TextureRect = new IntRect(70, 127, 25, 35);
- else if (currentFrame > 3 && currentFrame < 4) sprite.TextureRect = new IntRect(40, 127, 25, 35);
- else if (currentFrame > 4 && currentFrame < 5) sprite.TextureRect = new IntRect(100, 127, 25, 35);
- else if (currentFrame > 5 && currentFrame < 6) sprite.TextureRect = new IntRect(160, 127, 25, 35);
- }
- else if (dx < 0)
- {
- if (currentFrame > 0 && currentFrame < 1) sprite.TextureRect = new IntRect(130+25, 127, -25, 35);
- else if (currentFrame > 1 && currentFrame < 2) sprite.TextureRect = new IntRect(100+25, 127, -25, 35);
- else if (currentFrame > 2 && currentFrame < 3) sprite.TextureRect = new IntRect(70+25, 127, -25, 35);
- else if (currentFrame > 3 && currentFrame < 4) sprite.TextureRect = new IntRect(40+25, 127, -25, 35);
- else if (currentFrame > 4 && currentFrame < 5) sprite.TextureRect = new IntRect(100+25, 127, -25, 35);
- else if (currentFrame > 5 && currentFrame < 6) sprite.TextureRect = new IntRect(160+25, 127, -25, 35);
- }
- if (dx == 0)
- {
- sprite.TextureRect = direction == Direction.Right ? new IntRect(43, 14, 23, 35) : new IntRect(43+23, 14, -23, 35);
- }
- if (dy != 0)
- {
- if (currentFrame > 0 && currentFrame < 1) sprite.TextureRect = direction == Direction.Right ? new IntRect(87, 167, 20, 20) : new IntRect(87+20, 167, -20, 20);
- else if (currentFrame > 1 && currentFrame < 2) sprite.TextureRect = direction == Direction.Right ? new IntRect(116, 167, 20, 20) : new IntRect(116+20, 167, -20, 20);
- else if (currentFrame > 2 && currentFrame < 3) sprite.TextureRect = direction == Direction.Right ? new IntRect(142, 167, 20, 20) : new IntRect(142+20, 167, -20, 20);
- else if (currentFrame > 3 && currentFrame < 4) sprite.TextureRect = direction == Direction.Right ? new IntRect(170, 167, 20, 20) : new IntRect(170+20, 167, -20, 20);
- else currentFrame-=4;
- }
- sprite.Position = new Vector2f(rect.Left - 10 - map.offsetX, rect.Top - 10 - map.offsetY);
- window.Draw(sprite);
- }
- /*...*/
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement