HI everyone,
I have 2 classes:
-invader
-Game
for class: invader
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
namespace StarInvader2
{
[Serializable]
public class Invader
{
//InvaderType invaderType;
Form1 form1;
public Boolean hit = false;
Game game;
public Bitmap image;
public Point Location;
//public Point Location { get; private set; }
public Type InvaderType { get; private set; }
public Bitmap[][] InvaderImageBlock;
Bug bug = new Bug();
Saucer saucer = new Saucer();
Satalite satalite = new Satalite();
SpaceShip spaceShip = new SpaceShip();
Starss starss = new Starss();
//int x = GetScore(bug);
//int score = GetScore(bug);
/*
public int GetScore(Enemy enemy)
{
//int score = bug;
//int score1 = GetScore(saucer);
//int score2 = GetScore(satalite);
// int score3 = GetScore(spaceShip);
// int score4 = GetScore(starss);
//Console.WriteLine("{0} {1}", enemy.GetType(), enemy.KillerScore);
//return enemy.KillerScore;
}
*/
public Rectangle Area
{
get { return new Rectangle(Location, image.Size); }
}
public int Score { get; private set; }
public Invader(Type invaderType, Point location, int score)
{
//this.image = StarInvader2.Properties.Resources.bug1;
this.InvaderType = invaderType;
this.Location = location;
//this.Score = score;
this.image = InvaderImage(0);
//ScoreInvader(Score);
//gameScore(g);
}//End constructor.
public void Draw(Graphics g, int animationCell)
{
g.DrawImage(this.image, this.Location);
// Font drawfont = new Font("Arial", 25);
// g.DrawString(this.Score.ToString(), drawfont, Brushes.White, 1f, 1f);
}//End method
public static Bitmap ResizeImage(Image ImageToResize, int Width, int Height)
{
Bitmap bitmap = new Bitmap(ImageToResize);
using (Graphics graphics = Graphics.FromImage(bitmap))//Draw FromImage
{
graphics.DrawImage(ImageToResize, 0, 0, Width, Height);
}
return bitmap;
}//
private Bitmap InvaderImage(int animationCell)
// public void DisplayImage()
{
switch(InvaderType)
{
case Type.Bug:
switch(animationCell)
{
case 0:
return Properties.Resources.bug1;
case 1:
return Properties.Resources.bug2;
case 2:
return Properties.Resources.bug3;
case 3:
return Properties.Resources.bug4;
default:
break;
}//End swith(Bug)
break;
case Type.Saucer:
switch (animationCell)
{
case 0:
return Properties.Resources.flyingsaucer1;
case 1:
return Properties.Resources.flyingsaucer2;
case 2:
return Properties.Resources.flyingsaucer3;
case 3:
return Properties.Resources.flyingsaucer4;
default:
break;
}//End swith(Satelite)
break;
case Type.satelite:
switch (animationCell)
{
case 0:
return Properties.Resources.satellite1;
case 1:
return Properties.Resources.satellite2;
case 2:
return Properties.Resources.satellite3;
case 3:
return Properties.Resources.satellite4;
default:
break;
}//End swicth
break;
case Type.Spaceship:
switch (animationCell)
{
case 0:
return Properties.Resources.spaceship1;
case 1:
return Properties.Resources.spaceship2;
case 2:
return Properties.Resources.spaceship3;
case 3:
return Properties.Resources.spaceship4;
default:
break;
}//End switch
break;
case Type.star:
switch (animationCell)
{
case 0:
return Properties.Resources.star1;
case 1:
return Properties.Resources.star2;
case 2:
return Properties.Resources.star3;
case 3:
return Properties.Resources.star4;
default:
break;
}//End switch
break;
}//End swith(InvaderType)
return image;
}//End method
public void Move(Direction direction)
{
switch (direction)
{
case Direction.Down:
Location.Y += GameConstants.VerticalInterval;
break;
case Direction.Left:
Location.X -= GameConstants.HorizontalInterval;
break;
case Direction.Right:
Location.X += GameConstants.HorizontalInterval;
break;
case Direction.DownLeft:
Location.Y = GameConstants.VerticalInterval;
//Location.X -= GameConstants.HorizontalInterval;
break;
default: break;
}
} //End method
//}
//return image;
//}//End method
public int ScoreInvader(int Score)
{
Score = 0;
if (InvaderType == Type.Bug)
Score = GameConstants.BugScore;
else if (InvaderType == Type.satelite)
Score = GameConstants.SatelliteScore;
else if (InvaderType == Type.Saucer)
Score = GameConstants.SaucerScore;
else if (InvaderType == Type.Spaceship)
Score = GameConstants.SpaceshipScore;
else if (InvaderType == Type.star)
Score = GameConstants.StarScore;
/*
switch(InvaderType)
{
case Type.Bug:
//case Type.Bug:
Score += GameConstants.BugScore;
break;
case Type.satelite:
Score += GameConstants.SatelliteScore;
break;
case Type.Saucer:
Score += GameConstants.SaucerScore;
break;
case Type.Spaceship:
Score += GameConstants.SpaceshipScore;
break;
case Type.star:
Score += GameConstants.StarScore;
break;
default:
break;
}
*/
this.Score = Score;
return Score;
}//End method
public void UpdateInvaderImage(int animationCell)
{
image = InvaderImageBlock[(int)this.InvaderType][animationCell];
//imgae = InvaderImage[animationCell];
}
/*
public void Bitmap(int animationCell)
{
image = InvaderImage[animationCell];
}
*/
}
}
[/code]
and for class: game:
[code]
public void Draw(Graphics g, bool gameOver /*int animationCell*/ )
{
stars.Draw(g,boundaries);//Draws the stars on the form
if (!gameOver)
{
playerShip.Draw(g);//Draws the player on the form
foreach (Invader invader in invaders)
{
invader.Draw(g, animationCell);
gameScore(g, invader);
invader.ScoreInvader(score);
//invader.ScoreInvader(GameConstants.StarScore);
//MessageBox.Show("The score is:" + invader.ScoreInvader(GameConstants.StarScore));
}
foreach (Shot shot in playerShots)
shot.Draw(g);
foreach (Shot shot in invaderShots)
shot.Draw(g);
//foreach (Invader invader in invaders)
// {
// gameScore(g, invader);
// }
//invader.gameScore(g);
}
else
SetupStartScreen(g);
// form1.Invalidate();//To redraw part of the form that is "dirty".The Refresh() method is: Invalidate() + Update().
}//End method
public void gameScore(Graphics g, Invader invader)
{
Font drawfont = new Font("Arial", 25);
g.DrawString(invader.Score.ToString(), drawfont, Brushes.White, 60f, 35f);
invader.ScoreInvader(score);
//g.DrawString(graphicsBuffer, "SCORE: " + Convert.ToString(score), 10, 10, 16, FontStyle.Bold, Brushes.White);
// g.DrawString(invader.Score)
}
[code]
and Type:
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace StarInvader2
{
public enum Type
{
Bug, //= 0,
Saucer, //= 1,
satelite, //= 2,
Spaceship,// = 3,
star //= 4
}
}
[/code]
But the score doesnt change.
Loading
FroglegPosted Apr 4, 2011, 6:37 PM
Score working
FroglegPosted Apr 3, 2011, 6:33 PM
FroglegPosted Mar 31, 2011, 6:33 PM
FroglegPosted Mar 31, 2011, 6:28 PM
In the checkinvader collision i had the scores set IE
if (invaderType == Type.Bug)
{
score = 30;
}
so you can change it to
if (invaderType == Type.Bug)
{
//score = 30;
score = hitInvader.ScoreInvader(GameConstants.BugScore);
}
and do the same for the others
We have to find what is drawing the zero in top left corner and also what is incrementing it- and remove it
albert albertPosted Mar 31, 2011, 6:13 PM
Suthish NairPosted Mar 31, 2011, 1:05 PM
albert albertPosted Mar 31, 2011, 8:18 AM
THx!!
but is the score changing by u?
I mean for example if u change the score in star for example to 100.
And what means this:
if (hitCount == 10)
{
hitCount = 10;
}
FroglegPosted Mar 31, 2011, 7:41 AM
I put a label2 to on the form for the score
There is still a zero which changes to a 30 but I haven't found the code behind it to remove it
Have a look
albert albertPosted Mar 31, 2011, 6:12 AM
case Type.Bug:
//case Type.Bug:
score += GameConstants.BugScore;
break;
in the class Invader.
I just still dont understand why the second score: satelite is not accessible
albert albertPosted Mar 31, 2011, 6:04 AM
this is I have it now:
But still no call on the second invader score.
only on the firest: bugScore.
I alos tried:
score = score + hitInvader.ScoreInvader(GameConstants.SatelliteScore);
current score + the new score(GameConstants.SatelliteScore).
FroglegPosted Mar 31, 2011, 5:36 AM
I modified this in invader collision
score = hitInvader.ScoreInvader(GameConstants.BugScore);
Change the Space key in form1 gametimer to ShiftKey just for debugging purposes to stop editing while debugging
albert albertPosted Mar 31, 2011, 4:39 AM
this is how I have it now:
FroglegPosted Mar 30, 2011, 7:49 PM
The next one down is satellite - so we concentrate on that - from creation, its hit amount and then adding that to the main score
albert albertPosted Mar 30, 2011, 5:06 PM
Thx
albert albertPosted Mar 30, 2011, 4:20 PM
like this:
[code]
if (collided.Count() > 0)
{
Invader hitInvader = collided.ToList()[0];
//Score scr = new Score(score);
//this.score += hitInvader.Score;
//score = 0;
if (invaderType == Type.Bug)
{
//hitInvader.ScoreInvader(GameConstants.BugScore);
score = score + GameConstants.BugScore;
//invader.ScoreInvader(GameConstants.BugScore);
}
if (invaderType == Type.satelite)
{
//hitInvader.ScoreInvader(GameConstants.SatelliteScore);
score = score + GameConstants.SatelliteScore;
}
if (invaderType == Type.Saucer)
{
//hitInvader.ScoreInvader(GameConstants.SaucerScore);
score = score + GameConstants.SaucerScore;
}
if (invaderType == Type.Spaceship)
{
//hitInvader.ScoreInvader(GameConstants.SpaceshipScore);
score = score + GameConstants.SpaceshipScore;
}
if (invaderType == Type.star)
{
//hitInvader.ScoreInvader(GameConstants.StarScore);
score = score + GameConstants.StarScore;
}
//invader.ScoreInvader(score);
Score scr = new Score(score);
invaders.Remove(hitInvader);
}
}//End foreach
return GameOver;
}//End method
[/code]
But then still there is no difference to see in the invader score.
for example I put a breakpoint on this line:
score = score + GameConstants.StarScore;
It doesnt reach that line.
FroglegPosted Mar 30, 2011, 3:59 PM
Score scr = new Score(score);
down to
Score scr = new Score(score);
invaders.Remove(hitInvader);
You are sending null to the score class because you set the values after it
albert albertPosted Mar 30, 2011, 2:26 PM
here is the project I have it now.
FroglegPosted Mar 30, 2011, 1:34 PM
Upload your latest project so I can be inline with you
albert albertPosted Mar 30, 2011, 10:47 AM
this also doesnt work:
[code]
if (collided.Count() > 0)
{
Invader hitInvader = collided.ToList()[0];
Score scr = new Score(score);
//this.score += hitInvader.Score;
//score = 0;
if (invaderType == Type.Bug)
{
hitInvader.ScoreInvader(GameConstants.BugScore);
score = score + GameConstants.BugScore;
//invader.ScoreInvader(GameConstants.BugScore);
}
if (invaderType == Type.satelite)
{
hitInvader.ScoreInvader(GameConstants.SatelliteScore);
score = score + GameConstants.SatelliteScore;
}
if (invaderType == Type.Saucer)
{
hitInvader.ScoreInvader(GameConstants.SaucerScore);
//score = score + GameConstants.SaucerScore;
}
if (invaderType == Type.Spaceship)
{
hitInvader.ScoreInvader(GameConstants.SpaceshipScore);
//score = score + GameConstants.SpaceshipScore;
}
if (invaderType == Type.star)
{
hitInvader.ScoreInvader(GameConstants.StarScore);
//score = score + GameConstants.StarScore;
}
//invader.ScoreInvader(score);
invaders.Remove(hitInvader);
//Score scr = new Score(score);
// this.score += hitInvader.Score;
//invaders.Remove(hitInvader);
}
}//End foreach
return GameOver;
}//End method
[/code]
albert albertPosted Mar 30, 2011, 7:23 AM
sorry for my late reaction. But was here already late.
oke, I think I get it.
But then it is possible to have a switch in Invader, like this:
[code]
public int ScoreInvader(int score)
{
Score scr = new Score(score);
/*
Score = 0;
if (InvaderType == Type.Bug)
Score = GameConstants.BugScore;
else if (InvaderType == Type.satelite)
Score = GameConstants.SatelliteScore;
else if (InvaderType == Type.Saucer)
Score = GameConstants.SaucerScore;
else if (InvaderType == Type.Spaceship)
Score = GameConstants.SpaceshipScore;
else if (InvaderType == Type.star)
Score = GameConstants.StarScore;
*/
switch(InvaderType)
{
case Type.Bug:
//case Type.Bug:
score += GameConstants.BugScore;
break;
case Type.satelite:
score += GameConstants.SatelliteScore;
break;
case Type.Saucer:
score += GameConstants.SaucerScore;
break;
case Type.Spaceship:
score += GameConstants.SpaceshipScore;
break;
case Type.star:
score += GameConstants.StarScore;
break;
default:
break;
}
//this.Score = Score;
return score;
}//End method
[/code]
and then I call the method in Game, like this:
[code]
private bool CheckForInvaderCollision()
{
bool GameOver = false;
// Shot s = playerShots[shotNumber];
foreach (Shot shot in playerShots)
{
var collided = from invader in invaders
where invader.Area.Contains(shot.Location)
select invader;
if (collided.Count() > 0)
{
Invader hitInvader = collided.ToList()[0];
//score++;
invader.ScoreInvader(score);
score = 0;
Score scr = new Score(score);
// this.score += hitInvader.Score;
invaders.Remove(hitInvader);
}
}//End foreach
return GameOver;
}//End method
[/code]
But then I get the error:
Object reference not set to an instance of an object.
by this line:
invader.ScoreInvader(score);
but: private bool CheckForInvaderCollision(Invader invader)??
that cant be right, isnt?
what I forgot?
FroglegPosted Mar 29, 2011, 7:26 PM
Good to see you killing ants - pity they were'nt white ants.
albert albertPosted Mar 29, 2011, 7:18 PM
FroglegPosted Mar 29, 2011, 7:16 PM
score ++;
This sends it to Score clas
Score scr = new Score(score);
so:
score = score + GameConstants.BugScore;// hoping "GameConstants.BugScore is an integer
then send it to Score class
Score scr = new Score(score);
do this for each invader - make sure that GameConstants. whatever are integers and not null
albert albertPosted Mar 29, 2011, 6:34 PM
But how to manage now the difference scores for the invaders?
I had an if statement in Invader:
like this:
[code]
/*
public int ScoreInvader(int Score)
{
Score = 0;
if (InvaderType == Type.Bug)
Score = GameConstants.BugScore;
else if (InvaderType == Type.satelite)
Score = GameConstants.SatelliteScore;
else if (InvaderType == Type.Saucer)
Score = GameConstants.SaucerScore;
else if (InvaderType == Type.Spaceship)
Score = GameConstants.SpaceshipScore;
else if (InvaderType == Type.star)
Score = GameConstants.StarScore;
/*
switch(InvaderType)
{
case Type.Bug:
//case Type.Bug:
Score += GameConstants.BugScore;
break;
case Type.satelite:
Score += GameConstants.SatelliteScore;
break;
case Type.Saucer:
Score += GameConstants.SaucerScore;
break;
case Type.Spaceship:
Score += GameConstants.SpaceshipScore;
break;
case Type.star:
Score += GameConstants.StarScore;
break;
default:
break;
}
this.Score = Score;
return Score;
}//End method
*/
[/code]
FroglegPosted Mar 29, 2011, 6:03 PM
albert albertPosted Mar 29, 2011, 5:53 PM
THX man!!
The score is working.
But only it is counting 1. And not counting for eatch invader.
I mean there is no difference in score for eatch invader. The score is the same for eatch invader.
FroglegPosted Mar 29, 2011, 5:35 PM
private void InitializeScore()
{
//TheScore = new Score(ClientRectangle.Right - 400, 50);<<<<<<<<<
//TheHighScore.Read();
}
In Score.cs
change
public Score(int count)
{
Count = count;// to this
//Position.X = x;//comment out
//Position.Y = y;;//comment out
}
in Game.cs
chande to this
private bool CheckForInvaderCollision(/*Rectangle are*//*Rectangle are*/)
{
bool GameOver = false;
// Shot s = playerShots[shotNumber];
foreach (Shot shot in playerShots)
{
var collided = from invader in invaders
where invader.Area.Contains(shot.Location)
select invader;
if (collided.Count() > 0)
{
Invader hitInvader = collided.ToList()[0];
score ++;
Score scr = new Score(score);
invaders.Remove(hitInvader);
}
}//End foreach
return GameOver;
}//End method
albert albertPosted Mar 29, 2011, 2:58 PM
oke, I have put this line:
[code]
private bool CheckForInvaderCollision(/*Rectangle are*//*Rectangle are*/)
{
bool GameOver = false;
// Shot s = playerShots[shotNumber];
foreach (Shot shot in playerShots)
{
var collided = from invader in invaders
where invader.Area.Contains(shot.Location)
select invader;
if (collided.Count() > 0)
{
Invader hitInvader = collided.ToList()[0];
this.score += hitInvader.Score;
invaders.Remove(hitInvader);
}
}//End foreach
return GameOver;
}//End method
[/code]
And I see a big null. But the score still doesn't change :(.
FroglegPosted Mar 29, 2011, 2:23 PM
you are removing invaders but not implementing a score - start there
FroglegPosted Mar 29, 2011, 1:29 PM
albert albertPosted Mar 29, 2011, 1:13 PM
THX!!
albert albertPosted Mar 29, 2011, 10:20 AM
if (InvaderType == Type.Bug)
Score = GameConstants.BugScore;
and run(f5)
the line already been activated before I shoot.
albert albertPosted Mar 29, 2011, 9:47 AM
understand.
this is the project.
Niels
VulpesPosted Mar 29, 2011, 9:35 AM
albert albertPosted Mar 29, 2011, 9:28 AM
Mostly u use:
[code]
[/code]
Jaganathan BantheswaranPosted Mar 29, 2011, 9:23 AM
Please attach your code. If you post, it will be difficult to understand the logic flow.