Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Cowboy_Zombaliens
- {
- public partial class Form4 : Form
- {
- //set up movement booleans. if the boolean is true, the player moves, if the boolean is false it will not.
- bool goLeft;
- bool goRight;
- bool goUp;
- bool goDown;
- // set up a vlaue for the scoring
- int score = 0;
- private Point pt;
- public Form4()
- {
- InitializeComponent();
- }
- private void keyisdown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.A)
- {
- goLeft = true;
- }
- if (e.KeyCode == Keys.D)
- {
- goRight = true;
- }
- if (e.KeyCode == Keys.W)
- {
- goUp = true;
- }
- if (e.KeyCode == Keys.S)
- {
- goDown = true;
- }
- if (e.KeyCode == Keys.R)
- {
- new Form4().Show();
- this.Hide();
- }
- //Returns to the main menu when pressing escape
- if (e.KeyCode == Keys.Escape)
- {
- new Form2().Show();
- this.Hide();
- }
- }
- public void AIMove(int x, int y, float speed)
- {
- float tx = x - pt.X;
- float ty = y - pt.Y;
- float length = (float)Math.Sqrt(tx * tx + ty * ty);
- if (length > speed)
- {
- // move towards the goal
- pt.X = (int)(pt.X + speed * tx / length);
- pt.Y = (int)(pt.Y + speed * ty / length);
- }
- else
- {
- // already there
- pt.X = x;
- pt.Y = y;
- }
- }
- private void keyisup(object sender, KeyEventArgs e)
- {
- //Get users input
- if (e.KeyCode == Keys.A)
- {
- goLeft = false;
- }
- if (e.KeyCode == Keys.D)
- {
- goRight = false;
- }
- if (e.KeyCode == Keys.W)
- {
- goUp = false;
- }
- if (e.KeyCode == Keys.S)
- {
- goDown = false;
- }
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (Player.Bounds.IntersectsWith(pbxEnemy.Bounds))
- {
- timer1.Stop(); //stops the timer to prevent game from running
- MessageBox.Show("A Cowboy Zombalien has eaten your brain.\nYour final score is " + score, "GAME OVER!"); //displays game over menu
- new Form2().Show();
- this.Hide();
- }
- //get the current player location before movement occurs
- int CurrentPlayerLocationX = Player.Top;
- int CurrentPlayerLocationY = Player.Left;
- //player moves based on which key is pressed
- if (goLeft)
- {
- Player.Left -= 5;
- }
- if (goRight)
- {
- Player.Left += 5;
- }
- if (goUp)
- {
- Player.Top -= 5;
- }
- if (goDown)
- {
- Player.Top += 5;
- }
- //Get the players new position
- int NewPlayerLocationX = Player.Location.X;
- int NewPlayerLocationY = Player.Location.Y;
- foreach (Control x in this.Controls)
- {
- //tests if the player is touching a wall
- if (x is PictureBox && x.Tag == "wall")
- {
- if (Player.Bounds.IntersectsWith(x.Bounds))
- {
- //moves the player out of the wall
- Player.Top = CurrentPlayerLocationX;
- Player.Left = CurrentPlayerLocationY;
- }
- }
- //Checks if the player has picked up a coin
- }
- foreach (Control x in this.Controls)
- {
- if (x is PictureBox && x.Tag == "coin")
- {
- if (Player.Bounds.IntersectsWith(x.Bounds))
- {
- Random random = new System.Random();
- int RandomXLocation = random.Next(10, 1550); //returns a random X co-ord
- int RandomYLocation = random.Next(10, 850); //returns a random Y co-ord
- x.Top = RandomYLocation; //moves coin to the random co-ords above
- x.Left = RandomXLocation; //moves coin to the random co-ords
- score = score + 10;
- }
- }
- //Checks if the player has picked up a diamond
- if (x is PictureBox && x.Tag == "diamond")
- {
- if (Player.Bounds.IntersectsWith(x.Bounds))
- {
- Random random = new System.Random();
- int RandomXLocation = random.Next(10, 1550); //returns a random X co-ord
- int RandomYLocation = random.Next(10, 850); //returns a random Y co-ord
- x.Top = RandomYLocation; //moves the diamond to the random co-ords above
- x.Left = RandomXLocation; //moves the diamond to the random co-ords above
- score = score + 125; //increases score
- }
- }
- lblScore.Text = "Score: " + score;
- }
- pbxEnemy.AIMove(100, 100, 0.5f);
- }
- }
- }
- //private void Form4_Load(object sender, EventArgs e)
- //{
- //}
- //private void lblScore_Click(object sender, EventArgs e)
- //{
- //}
- //private void Player_Click(object sender, EventArgs e)
- //{
- //}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement