Jump to content
  • Advertisement
Sign in to follow this  
advent_gamer

XNA Connect 4

This topic is 2995 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm working on a connect 4 game that is semi being based off of the game logic i used to create the same game with c++ and windows GDI. Currently I'm having issues with a few things. The first being i'm trying to find a way to draw a new sprite of the same texture each time the mouse is clicked on a column. So for instance i click in a column, the first "chip" is drawn in the appropriate box in the grid, then clicking again draws the same texture again but above the last texture in the grid. The next issue is with my current if statement. Since count is being incremented inside the if statement the else is automatically set to true and the if statement just continues. I need it to only say that iCount == 1 is true when the user clicks in the column again. The iCount variables are used to keep track of what box to draw in, in each column. So if the first box of column one is filled then iCount goes from 0 to 1. I guess this method will work with XNA i'm really not sure, like i said i'm basing the logic off of another project i did a while back. If you have some advice i could really use it >.<


using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RawrImAGameIThink
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

MouseState mouseStateCurrent, mouseStatePrevious;

int posx = 0;
int posy = 0;

//int chip[];
//int CurrentPositon[];
bool[] chip = new bool[42];
int[] CurrentPosition = new int[42];

bool isdown = true;

//counters to countrol with "box" the chip is drawn in
int iCount = 0; //first column
int iCount2 = 0; //second column
int iCount3 = 0; //third column
int iCount4 = 0; //fourth column
int iCount5 = 0; //fifth column
int iCount6 = 0; //sixth column
int iCount7 = 0; //seventh column


bool place = false;
int player = 1;

//Vector2 Position = new Vector2(posx, posy);
//Vector2 Position;
Vector2[] Position = new Vector2[42];
//Position[0]=(0,0);;


Texture2D blueChip;
Texture2D redChip;

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
//make the mouse visible
this.IsMouseVisible = true;

// TODO: Add your initialization logic here
graphics.PreferredBackBufferHeight = 600;
graphics.PreferredBackBufferWidth = 700;
graphics.ApplyChanges();

base.Initialize();
}

/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

spriteBatch = new SpriteBatch(GraphicsDevice);
redChip = Content.Load<Texture2D>("Images/RedChip");
blueChip = Content.Load<Texture2D>("Images/BlueChip");


// TODO: use this.Content to load your game content here
}

/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}

/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
GamePadState currentState = GamePad.GetState(PlayerIndex.One);
// MouseState mouseStateCurrent, mouseStatePrevious;



// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

UpdateMouse();

// TODO: Add your update logic here

base.Update(gameTime);
}

protected void UpdateMouse()
{
MouseState current_mouse = Mouse.GetState();

int mouseX = current_mouse.X;
int mouseY = current_mouse.Y;

if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{
isdown = true;
//===============================================
//first column//
if (player == 1)
{
if (mouseX < 100)
{
if (iCount == 0)
{

//set the array element to 1 to represent it being filled
//with a red chip
CurrentPosition[0] = 1;

//draw first chip
chip[0] = true;
//isdown = false;
iCount++;

}
else if (iCount == 1)
{
iCount++;
//posx = 1;
//posy = 500;

CurrentPosition[1] = 1;

//draw next chip
chip[1] = true;
}
else if (iCount == 2)
{
iCount++;

}
}
//player = 2;
//isdown = false;
}
//end first column//
//==================================================
}
}

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

mouseStateCurrent = Mouse.GetState();
if (player == 1)
{
if (chip[0] == true)
{
spriteBatch.Begin();
spriteBatch.Draw(redChip, Position[0] = new Vector2(1, 550), Color.White);
spriteBatch.End();
}
if (chip[1] == true)
{
spriteBatch.Begin();
spriteBatch.Draw(redChip, Position[1] = new Vector2(300, 300), Color.White);
spriteBatch.End();
}
}


/*
else if (chip[1] == true)
{
spriteBatch.Draw(redChip, Position = new Vector2(200, 200), Color.White);
}
*/


// TODO: Add your drawing code here

base.Draw(gameTime);
}
}
}


Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!