Little game for practise, need a list of enemies

So I've done a couple weeks of CS50 and I'm trying to make a small game in c#. The game is just to show what I can do. However I'm trying to make several enemies instead of just one and I'm not finding the solution that I need. Hopefully some of you can point me in the right direction.

I know I have to make a list and then use a for loop every time I want to access that enemy, but I am not sure where everything goes and if I should make an enemy class. I will put in bold what I think I should add.

I know it looks like a mess right now, but I hope someone can guide me in the right direction.

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 FinalGame { public partial class FormView : Form { // Initialize player location variables private int _x; private int _y; // Initialize enemy location variables private int eX; private int eY; private int e2X; private int e2Y; private int e3X; private int e3Y; // Initialize powerUp location variables private int pX; private int pY; // Initialize player movement variables private int xSpeed; private int ySpeed; Point velocity; // Initialize enemy movement variables private int enemyxSpeed; private int enemyySpeed; private int enemy2xSpeed; private int enemy2ySpeed; private int enemy3xSpeed; private int enemy3ySpeed; Random random = new Random(); // Initialize all actors Rectangle player; Rectangle enemy; Rectangle enemy2; Rectangle enemy3; Rectangle powerUp; **List<Rectangle> enemyList= new List<enemy>** public FormView() { InitializeComponent(); // Start the player in the middle of the screen _x = 1280/2; _y = 720/2; // Start the enemy up top eX = 50; eY = 50; e2X = 150; e2Y = 150; e3X = 250; e3Y = 250; // Location of the Powerup pX = 500; pY = 500; // Set player speed to 0 xSpeed = 0; ySpeed = 0; velocity.X = 0; velocity.Y = 0; // Choose random enemy x and y speed enemyxSpeed = random.Next(-2, 2); enemyySpeed = random.Next(-2, 2); enemy2xSpeed = random.Next(-2, 2); enemy2ySpeed = random.Next(-2, 2); enemy3xSpeed = random.Next(-2, 2); enemy3ySpeed = random.Next(-2, 2); **foreach (enemy in enemyList) { //generate random x and y speed }** } private void FormView_Paint(object sender, PaintEventArgs e) { // Draw the player as a blue square e.Graphics.FillRectangle(Brushes.Blue, player); // Draw enemies as a red square e.Graphics.FillRectangle(Brushes.Red, enemy); e.Graphics.FillRectangle(Brushes.Red, enemy2); e.Graphics.FillRectangle(Brushes.Red, enemy3); **foreach (enemy in enemyList) { //draw the graphics }** // Draw powerups as a green circle e.Graphics.FillEllipse(Brushes.Green, powerUp); } private void tmrMoving_Tick(object sender, EventArgs e) { // Redraw the actors every tick enemy = new Rectangle(eX, eY, 25, 25); enemy2 = new Rectangle(e2X, e2Y, 25, 25); enemy3 = new Rectangle(e3X, e3Y, 25, 25); **foreach (enemy in enemyList) { //make the enemy }** powerUp = new Rectangle(pX, pY, 25, 25); player = new Rectangle(_x, _y, 25, 25); // Move the player _x += velocity.X; _y += velocity.Y; Invalidate(); // Keep the player inside the bounds if (_x <= 0) { _x = 0; } if (_x >= 1280 - 41) { _x = 1280 - 41; } if (_y <= 0) { _y = 0; } if (_y >= 720 - 64) { _y = 720 - 64; } // Move the enemies eX += enemyxSpeed; eY += enemyySpeed; e2X += enemy2xSpeed; e2Y += enemy2ySpeed; e3X += enemy3xSpeed; e3Y += enemy3ySpeed; // Keep the enemies inside the bounds and bounce him off walls if (eX <= 0 || eX >= 1280 - 41) { enemyxSpeed = -enemyxSpeed; } if (eY <= 0 || eY >= 720 - 64) { enemyySpeed = -enemyySpeed; } if (e2X <= 0 || e2X >= 1280 - 41) { enemy2xSpeed = -enemy2xSpeed; } if (e2Y <= 0 || e2Y >= 720 - 64) { enemy2ySpeed = -enemy2ySpeed; } if (e3X <= 0 || e3X >= 1280 - 41) { enemy3xSpeed = -enemy3xSpeed; } if (e3Y <= 0 || e3Y >= 720 - 64) { enemy3ySpeed = -enemy3ySpeed; } **foreach (enemy in enemyList) { //generate random x and y speed }** // Player collision with enemy if (player.IntersectsWith(enemy) || player.IntersectsWith(enemy2) || player.IntersectsWith(enemy3)) { Application.Exit(); } } // Let user move player with WASD private void FormView_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.A) { xSpeed = 3; velocity.X = -xSpeed; } if (e.KeyCode == Keys.D) { xSpeed = 3; velocity.X = xSpeed; } if (e.KeyCode == Keys.W) { ySpeed = 3; velocity.Y = -ySpeed; } if (e.KeyCode == Keys.S) { ySpeed = 3; velocity.Y = ySpeed; } } // Stop the movement once key is not pressed private void FormView_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.A) { velocity.X = 0; } if (e.KeyCode == Keys.D) { velocity.X = 0; } if (e.KeyCode == Keys.W) { velocity.Y = 0; } if (e.KeyCode == Keys.S) { velocity.Y = 0; } } } } 

by feedabeast via /r/csharp

Leave a Reply