i have been trying to make a basic space invaders style game but am having trouble drawing the sprites to the screen. everything is fine until i need to move the sprite based on the input from the joystick i can move the sprite but when i make call to clearscreen and draw the screen flickers. i was wondering if there was a better way to accomplish moving a graphic and redrawing it to the screen.
I have spent the day working on this and I have learned a lot but I can not get the ship to move I have changed a bit and now I am lost on how to pick up the joystick movements I can place the ship on the screen and start the shots but no ship movement
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
//using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
namespace SWgameTest2
{
public partial class Program
{
// Reference to the Desktop window
// Window mainWindow;
// Image that represents this window
private Bitmap screen = new Bitmap(SystemMetrics.ScreenWidth, SystemMetrics.ScreenHeight);
// Game variables
private DispatcherTimer gameTimer, shotTimer;
private DateTime shotTime;
private int shotIndex;
private int[] shotX = new int[10];
private int[] shotY = new int[10];
private int[] shotDir = new int[10];
private Bitmap shot1 = Resources.GetBitmap(Resources.BitmapResources.shot1);
private Starfield starfield;
private Ship ship;
private ArrayList enemyList = new ArrayList();
private int cursorXPos;
private int cursorYPos;
private uint Width;
private uint Height;
// private int lastYpos;
// private int lastXpos;
// Stops loops from throwing errors on exit
private bool exiting = false;
// private Joystick.JoystickEventHandler joystick_JoystickPressed;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
// jPos = joystick.GetJoystickPostion;
// mainWindow = display.WPFWindow;
// button.ButtonPressed += new GTM.GHIElectronics.Button.ButtonEventHandler(button_ButtonPressed);
joystick.JoystickPressed += new GTM.GHIElectronics.Joystick.JoystickEventHandler(joystick_JoystickPressed);
// joystick.GetJoystickPostion += new GTM.GHIElectronics.Joystick.Position(joy_Posit);
Width = display.Width - 7;
Height = display.Height - 7;
// DrawHomeScreen();
cursorXPos = (int)Width / 1;
cursorYPos = (int)Height / 1;
// lastXpos = -1;
// lastYpos = -1;
// Timer timedEvent = new Timer(updateJoystick, null, 0, 10);
// Canvas canvas = new Canvas();
// canvas.Width = SystemMetrics.ScreenWidth;
// canvas.Height = SystemMetrics.ScreenHeight;
// canvas.TouchDown += OnTouchDown;
// this.Child = canvas;
starfield = new Starfield();
ship = new Ship();
// Set starting position
ship.X = cursorXPos / 2;
// ship.X = this.Width / 2;
ship.Y = cursorYPos - ship.Height - 5;
// Add five enemy ships
int i;
for (i = 0; i < 5; i++)
{
enemyList.Add(new EnemyShip());
}
// Clear all shot arrays
for (i = 0; i < shotY.Length; i++)
{
shotX[i] = 0;
shotY[i] = 0;
shotDir[i] = 0;
}
// Runs the game at about 13 FPS
gameTimer = new DispatcherTimer();
gameTimer.Tick += GameLoop;
gameTimer.Interval = new TimeSpan(0, 0, 0, 0, 75);
gameTimer.Start();
// Fires 4 shots a second
shotTimer = new DispatcherTimer();
shotTimer.Tick += FireLoop;
shotTimer.Interval = new TimeSpan(0, 0, 0, 0, 250);
Debug.Print("Program Started");
}
/// <summary>
/// Set the joystick object and event handlers.
/// </summary>
/// <param name="device"></param>
///
/// <summary>
/// Clear the joystick object and event handlers.
/// </summary>
// private void ClearJoystick()
// {
// if (joystick == null)
// return;
// joystick = null;
// }
/// <summary>
/// Main loop that runs the game.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void GameLoop(object sender, EventArgs e)
{
if (exiting) return;
screen.Clear();
starfield.ToScreen(screen);
ShotLoop();
EnemyLoop();
ShipLoop();
screen.Flush();
}
/// <summary>
/// Moves enemy ships.
/// </summary>
public void EnemyLoop()
{
EnemyShip enemy;
int i;
for (i = 0; i < enemyList.Count; i++)
{
enemy = (EnemyShip)enemyList[i];
enemy.LastXY(enemy.X, enemy.Y);
enemy.Y += enemy.SpeedY * enemy.DirY;
if (enemy.Y > this.Height)
enemy.Random();
enemy.ToScreen(screen);
}
}
/// <summary>
/// Moves the player's ship.
/// </summary>
public void ShipLoop()
{
ship.LastXY(ship.X, ship.Y);
if (ship.DirX != 0)
{
int newX = ship.X + (ship.SpeedX * ship.DirX);
if (newX >= 0 && newX <= cursorXPos)
ship.X += ship.SpeedX * ship.DirX;
}
if (ship.DirY != 0)
{
int newY = ship.Y + (ship.SpeedY * ship.DirY);
if (newY >= 0 && newY <= cursorYPos)
ship.Y += ship.SpeedY * ship.DirY;
}
ship.ToScreen(screen);
}
/// <summary>
/// Moves shots and checks for collisions with enemy ships.
/// </summary>
public void ShotLoop()
{
EnemyShip enemy;
int i, j;
for (i = 0; i < shotY.Length; i++)
{
if (shotDir[i] != 0)
{
int lastShotY = shotY[i];
shotY[i] += 10 * shotDir[i];
screen.DrawImage(shotX[i], shotY[i], shot1, 0, 0, shot1.Width, shot1.Height);
// Did we hit an enemy?
for (j = 0; j < enemyList.Count; j++)
{
enemy = (EnemyShip)enemyList[j];
// Check the current position
if (shotX[i] > enemy.X - 14 && shotX[i] < enemy.X + 14 && shotY[i] > enemy.Y - 15 && shotY[i] < enemy.Y + 15)
{
shotDir[i] = 0;
enemy.Random();
break;
}
// Check the previous position
else if (shotX[i] > enemy.X - 14 && shotX[i] < enemy.X + 14 && lastShotY > enemy.Y - 15 && lastShotY < enemy.Y + 15)
{
shotDir[i] = 0;
enemy.Random();
break;
}
}
// Are we off screen?
if (shotY[i] < 0 || shotY[i] > this.Height)
shotDir[i] = 0;
}
}
}
/// <summary>
/// Continuous loop that adds shots.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FireLoop(object sender, EventArgs e)
{
if (exiting) return;
AddShot(ship, -1);
}
/// <summary>
/// Handles the onboard button up events.
/// </summary>
/// <param name="e"></param>
// protected override void OnButtonUp(ButtonEventArgs e)
// {
// Stop all directional movement
// ship.DirX = ship.DirY = 0;
// Stop the shot timer
// if (shotTimer.IsEnabled)
// shotTimer.Stop();
// }
/// <summary>
/// Add a shot.
/// </summary>
/// <param name="ship"></param>
/// <param name="dir"></param>
public void AddShot(Ship ship, int dir)
{
shotX[shotIndex] = ship.X;
shotY[shotIndex] = ship.Y;
shotDir[shotIndex] = dir;
shotIndex++;
if (shotIndex > shotY.Length - 1) shotIndex = 0;
}
/// <summary>
/// Handles the joystick button down events.
/// </summary>
/// <param name="sender"></param>
/// <param name="state"></param>
private void joystick_JoystickPressed(Joystick sender, Joystick.JoystickState state)
{
// switch ((int)state.Eq)
// {
// Trigger pulled
// case 0:
// First shot before the timer takes effect
if (DateTime.Now >= shotTime.AddMilliseconds(250))
{
AddShot(ship, -1);
shotTime = DateTime.Now;
}
shotTimer.Start();
// break;
}
/// <summary>
/// Handles the joystick button up events.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
// private void joystick_JoystickPressed(Joystick sender, Joystick.JoystickState state);
// {
// switch ((int)args.ChangedButton)
// {
// Stop the shot timer
// case 0:
// if (shotTimer.IsEnabled);
// }
// shotTimer.Stop();
//}
// break;
// }
// }
/// <summary>
/// Handles the joystick x and y movement events.
/// </summary>
/// <param name="sender"></param>
/// <param name="state"></param>
///
private void joy_Posit(Joystick sender, Joystick.Position state)
{
// if (ship == null) return;
int x = cursorXPos;
int y = cursorYPos;
if (x < -18)
{
ship.SpeedX = 1 + ((x * -1) / 32);
ship.DirX = -1;
}
else if (x > 22)
{
ship.SpeedX = 1 + (x / 32);
ship.DirX = 1;
}
else
{
ship.SpeedX = 6;
ship.DirX = 0;
}
if (x > 22)
{
ship.SpeedY = 1 + (x / 32);
ship.DirY = 1;
}
else if (x < -18)
{
ship.SpeedY = 1 + ((x * -1) / 32);
ship.DirY = -1;
}
else
{
ship.SpeedY = 1;
ship.DirY = 0;
}
}
}
}