Hi everybody,
I have this:
[code]
private bool MoveInvaders()
{
bool directionChanged = false;
//if ((TimerCounter % TheSpeed) == 0)
//{
//foreach (Invader invader in invaders)
// {
// invader.Move(Direction.Right);
//}
if (direction == Direction.Right)
{
//invader.Move( Direction.Right);
var invaderGroups = from invader in invaders
where invader.Location.X > boundaries.Right - 800
orderby invader.Location.Y descending
group invader by invader.Location.X into invaderGroup
//select invaderGroup;
select invader;
//theInvader.Move(Direction.Left);
//where
//select invaderGroup;
if (invaderGroups.Count() > 0) directionChanged = true;
}
//End if
else
// if (direction == Direction.Left)
{
int pixxel = boundaries.Width - GameConstants.InvaderEdgeOfScreen;
var invaderGroups = from invader in invaders
where invader.Location.X < pixxel + invader.image.Width-100 //boundaries.Width - 300
orderby invader.Location.Y descending
//select invaderGroup;
group invader by invader.Location.Y into invaderGroup
select invader;
}
if (directionChanged)
{
if (direction == Direction.Right)
{
direction = Direction.Left;
}
else
direction = Direction.Right - boundaries.Width-100;
}
//}//End timercount
//End foreacht invader
return directionChanged;
}//End method
private void MoveInvaderReal()
{
if ((TimerCounter % TheSpeed) == 0)
{
//TimerCounter = 0;
foreach (Invader invader in invaders)//These method returns a list with the moving invaders.
{
invader.Move(direction);
}
}
TimerCounter++;
}
[/code]
U see there are 2 methods. But the invaders are moving from right - left and back to left. But then the invaders are going off screen and dont come back :(.
THX for helping me!!
Loading
albert albertPosted Mar 14, 2011, 5:30 AM
THX man. It realy looks nice.
I will use it in my own application.
By the way what is ur email?
Will be nice to exchange programming skills.
it is interesting that u use Picturebox. But I use Bitmap. I tried:
[code]
private void MoveInvaderDown()
{
//invader.Move(direction);
foreach (Invader invader in invaders)
{
if(invader.Location.X < boundaries.Width - 100)
{
// ((invader.Move(Direction.Down) && invader.Move(Direction.Left)));// direction = Direction.Down && Direction.Left;
}
}
}
[/code]
but that is ofcourse not possible because invader.Move is void.
But u use down and right or left in 1 statement. That is mutch more better.
These is my 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;
Game game;
public Bitmap image;
public Point Location;
//public Point Location { get; private set; }
public Type InvaderType { get; private set; }
public Bitmap[][] InvaderImageBlock;
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;
//image = InvaderImageBlock(0);
SetupImage();
this.image = InvaderImageBlock[(int)invaderType][0]; //Object is null.
if (image == null)
MessageBox.Show("is null");
}//End constructor.
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;
}//
public void SetupImage()
{
//MustDispose obj = new MustDispose;
try
{
//image = new Bitmap( InvaderImageBlock);
InvaderImageBlock = new Bitmap[5][];
InvaderImageBlock[(int)Type.Bug] = new Bitmap[4];
InvaderImageBlock[(int)Type.Bug][0] = (Properties.Resources.bug1);//Image1 - cell0
InvaderImageBlock[(int)Type.Bug][1] = (Properties.Resources.bug2);//Image2 - cell1
InvaderImageBlock[(int)Type.Bug][2] = (Properties.Resources.bug3);//Image3 - cell2
InvaderImageBlock[(int)Type.Bug][3] = (Properties.Resources.bug4);//Image4 - cell3
InvaderImageBlock[(int)Type.Saucer] = new Bitmap[4];
InvaderImageBlock[(int)Type.Saucer][0] = (Properties.Resources.flyingsaucer1);
InvaderImageBlock[(int)Type.Saucer][1] = (Properties.Resources.flyingsaucer2);
InvaderImageBlock[(int)Type.Saucer][2] = (Properties.Resources.flyingsaucer3);
InvaderImageBlock[(int)Type.Saucer][3] = (Properties.Resources.flyingsaucer4);
InvaderImageBlock[(int)Type.satelite] = new Bitmap[4];
InvaderImageBlock[(int)Type.satelite][0] = (Properties.Resources.satellite1);
InvaderImageBlock[(int)Type.satelite][1] = (Properties.Resources.satellite2);
InvaderImageBlock[(int)Type.satelite][2] = (Properties.Resources.satellite3);
InvaderImageBlock[(int)Type.satelite][3] = (Properties.Resources.satellite4);
InvaderImageBlock[(int)Type.Spaceship] = new Bitmap[4];
InvaderImageBlock[(int)Type.Spaceship][0] = (Properties.Resources.spaceship1);
InvaderImageBlock[(int)Type.Spaceship][1] = (Properties.Resources.spaceship2);
InvaderImageBlock[(int)Type.Spaceship][2] = (Properties.Resources.spaceship3);
InvaderImageBlock[(int)Type.Spaceship][3] = (Properties.Resources.spaceship4);
InvaderImageBlock[(int)Type.star] = new Bitmap[4];
InvaderImageBlock[(int)Type.star][0] = (Properties.Resources.star1);
InvaderImageBlock[(int)Type.star][1] = (Properties.Resources.star2);
InvaderImageBlock[(int)Type.star][2] = (Properties.Resources.star3);
InvaderImageBlock[(int)Type.star][3] = (Properties.Resources.star4);
//throw new System.Exception();
}
catch (OutOfMemoryException ex)
{
Win32Exception myEx = new Win32Exception();
MessageBox.Show(myEx.Message);
MessageBox.Show(myEx.HelpLink);
MessageBox.Show(myEx.Source);
}
}
public void Draw(Graphics g, int animationCell)
{
g.DrawImage(this.image, this.Location);
}//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;
default: break;
}
} //End method
//}
//return image;
//}//End method
public void UpdateInvaderImage(int animationCell)
{
image = InvaderImageBlock[(int)this.InvaderType][animationCell];
}
}
}
[/code]
If it is oke.
FroglegPosted Mar 13, 2011, 6:53 PM
No code if they reach bottom of form
For games have a look at XNA
albert albertPosted Mar 12, 2011, 1:34 PM
THX for helping
but If I put this:
[code]
private void MoveInvaderReal()
{
if ((TimerCounter % TheSpeed) == 0)
{
//TimerCounter = 0;
foreach (Invader invader in invaders)//These method returns a list with the moving invaders.
{
invader.Move(direction);
if (invader.Location.X < 0 | invader.Location.X < boundaries.Width - 30)
{
direction = Direction.Down;
}
invader.Move(direction);
}
}
TimerCounter++;
}
[/code]
Because u mean: boundaris.Width instead of: this.width isnt?
Then the invaders are goind directly down :(. But I want first that the invaders mover horizontal en then after I want to make the invaders going down.
But but it have to be possible that first u will see the invaders going from right-left en left-rigth. why not?
But they still move out of screen:(.
Do u have any other suggestions.
THX!!
FroglegPosted Mar 12, 2011, 1:10 PM
Do you have Direction.Down ?
private void MoveInvaderReal()
{
if ((TimerCounter % TheSpeed) == 0)
{
//TimerCounter = 0;
foreach (Invader invader in invaders)//These method returns a list with the moving invaders.
{
invader.Move(direction);
if (invader.Location.X < 0 | invader.Location.X < this.Width - 30)
{
direction = Direction.Down;
}
}
}
TimerCounter++;
}