Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Game
- {
- public partial class Form1 : Form
- {
- bool left;
- bool right;
- bool jump;
- bool onObject = true;
- int G = 20;
- int force;
- public Form1()
- {
- InitializeComponent();
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- loadGameControls();
- RegisterBlocks();
- playerSkin();
- }
- private void Form1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Left)
- {
- left = true;
- player.Image = Image.FromFile("walk_l.gif");
- }
- if (e.KeyCode == Keys.Right)
- {
- right = true;
- player.Image = Image.FromFile("walk_r.gif");
- }
- if (!jump)
- {
- if (e.KeyCode == Keys.Up)
- {
- jump = true;
- force = G;
- player.Image = Image.FromFile("jump.png");
- }
- }
- }
- private void Form1_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Left){left = false;}
- if (e.KeyCode == Keys.Right){right = false;}
- }
- void playerSkin()
- {
- if (!jump && !right && !left)
- {
- player.Image = Image.FromFile("stand.png");
- }
- }
- void checkColision(PictureBox block)
- {
- //Colision
- //Right
- if (player.Left + player.Width >= block.Left && block.Left + player.Width > player.Left && player.Top < block.Top && player.Bottom > block.Bottom)
- {
- right = false;
- player.Left = block.Left - player.Width;
- }
- //Left
- if (block.Right + player.Width >= player.Right && block.Right + player.Width <= player.Right + block.Width && block.Bottom + block.Height >= player.Bottom + 5 && block.Top <= player.Top)
- {
- left = false;
- player.Left = block.Right;
- }
- //Top
- if (player.Left + player.Width > block.Left && player.Left < block.Left + block.Width && player.Top + player.Height >= block.Top && player.Top < block.Top)
- {
- player.Top = block.Top - player.Height;
- force = 0;
- onObject = true;
- jump = false;
- }
- else
- {
- onObject = false;
- }
- //Bottom
- if (player.Left + player.Width > block.Left && player.Left < block.Left + block.Width && player.Bottom > block.Bottom && player.Bottom <= block.Bottom + player.Height + 3)
- {
- force = -1;
- }
- }
- void RegisterBlocks()
- {
- checkColision(b3);
- checkColision(b1);
- checkColision(b2);
- checkColision(block);
- }
- void loadGameControls()
- {
- //Movement
- if (left)
- {
- player.Left -= 5;
- }
- if (right)
- {
- player.Left += 5;
- }
- if (jump)
- {
- player.Top -= force;
- force -= 1;
- }
- if ((player.Top + player.Height >= screen.Height))
- {
- player.Top = screen.Height - player.Height;
- jump = false;
- } else if (!onObject)
- {
- player.Top += 5;
- }
- //Bounds
- if (player.Left <= 0 - player.Width)
- {
- player.Left += screen.Width;
- }
- if (player.Left >= screen.Width)
- {
- player.Left = 0 - player.Width;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement