Hallo ICT'ers,
Ik ben bezig met een simpele pong game in C#. Nu ben ik een menu aan het maken. Hieronder zijn de GameStates die ik heb:
- MainMenu //Hoofdmenu met de opties om het spel te spelen en om de settings te wijzigen
- PrePlaying //In dit scherm kies je singleplayer of multiplayer
- Playing // Het spel zelf
- Pause // Spreekt voor zich
- Settings // Settings wijzigen
Als ik in het hoofdmenu op preplaying klik werkt alles. Maar als ik dan doorklik op playing staat alles vast. Iemand een idee? Ook is het soms zo dat als ik op preplaying klik dat ik dan ook meteen de volgende knop indruk. (Die staat op de volgende pagina op dezelfde plek) Is er een manier om dat tegen te gaan?
Hier is de code:
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;
namespace WindowsGame4
{
public class Pong : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//Gamestates
enum GameState
{
MainMenu,
Settings,
Playing,
Pause,
PrePlaying
}
GameState CurrentGameState = GameState.MainMenu;
//Screen Adjustments
int screenWidth = 800;
int screenHeight = 600;
//Menu buttons
cButton btnPrePlaying;
cButton btnSettings;
cButton btnOnePlayer;
cButton btnTwoPlayers;
//PrePlaying Settings
bool multiplayer = false;
//SpriteFonts
SpriteFont pong;
//Ball and Player Content
Texture2D ballTexture;
Rectangle ballRectangle;
Texture2D player1Texture;
Rectangle player1Rectangle;
Texture2D player2Texture;
Rectangle player2Rectangle;
Vector2 velocity;
public Pong()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
IsMouseVisible = true;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
graphics.PreferredBackBufferWidth = screenWidth;
graphics.PreferredBackBufferHeight = screenHeight;
graphics.IsFullScreen = true;
graphics.ApplyChanges();
//SpriteFonts
pong = Content.Load<SpriteFont>("pong");
//btnPrePlaying
btnPrePlaying = new cButton(Content.Load<Texture2D>("btnPlaying"), graphics.GraphicsDevice);
btnPrePlaying.setPosition(new Vector2(350, 250));
//btnSettings
btnSettings = new cButton(Content.Load<Texture2D>("btnSettings"), graphics.GraphicsDevice);
btnSettings.setPosition(new Vector2(350, 290));
//btnOnePlayer
btnOnePlayer = new cButton(Content.Load<Texture2D>("btnOnePlayer"), graphics.GraphicsDevice);
btnOnePlayer.setPosition(new Vector2(350, 250));
//btnTwoPlayers
btnTwoPlayers = new cButton(Content.Load<Texture2D>("btnTwoPlayers"), graphics.GraphicsDevice);
btnTwoPlayers.setPosition(new Vector2(350, 290));
//Ball and player content
ballTexture = Content.Load<Texture2D>("ball");
ballRectangle = new Rectangle(300, 300, 32, 32);
player1Texture = Content.Load<Texture2D>("player");
player1Rectangle = new Rectangle(20, 250, 5, 50);
player2Texture = Content.Load<Texture2D>("player");
player2Rectangle = new Rectangle(780, 250, 5, 50);
velocity.X = 3f;
velocity.Y = 3f;
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
MouseState mouse = Mouse.GetState();
switch (CurrentGameState)
{
case GameState.MainMenu:
if (btnPrePlaying.isClicked == true) CurrentGameState = GameState.PrePlaying;
btnPrePlaying.Update(mouse);
if (btnSettings.isClicked == true) CurrentGameState = GameState.Settings;
btnSettings.Update(mouse);
break;
case GameState.PrePlaying:
if (btnOnePlayer.isClicked == true) CurrentGameState = GameState.Playing;
btnOnePlayer.Update(mouse);
if (btnTwoPlayers.isClicked == true) CurrentGameState = GameState.Playing;
btnTwoPlayers.Update(mouse);
break;
case GameState.Settings:
break;
case GameState.Playing:
if (multiplayer == false)
{
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
player1Rectangle.Y -= 6;
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
player1Rectangle.Y += 6;
}
}
break;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
switch (CurrentGameState)
{
case GameState.MainMenu:
btnPrePlaying.Draw(spriteBatch);
btnSettings.Draw(spriteBatch);
break;
case GameState.PrePlaying:
spriteBatch.DrawString(pong, "Players", new Vector2(screenWidth / 2 - 10, 100), Color.Black);
btnOnePlayer.Draw(spriteBatch);
btnTwoPlayers.Draw(spriteBatch);
break;
case GameState.Settings:
break;
case GameState.Playing:
if (multiplayer == false)
{
spriteBatch.Begin();
spriteBatch.Draw(player1Texture, player1Rectangle, Color.White);
spriteBatch.Draw(ballTexture, ballRectangle, Color.White);
spriteBatch.End();
}
break;
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Toon Meer
Overigens schrijf ik al mijn commentaar in het Engels. Dit zijn geen scripts van internet.
Alvast bedankt!
//Edit
Ik ben er net met debuggen achter gekomen dat het al freezed op het PrePlaying menu zelf. Maar het probleem is nog niet opgelost.
//OPLOSSING//
Het is me ondertussen gelukt. Ik had SpriteBatch.Begin() en SpriteBatch.End() dubbel in de switch gezet.
Voor het snelle doorklikken van buttons heb ik een oplossing bedacht. Ik heb een extra boolean wasClicked. Ik kijk eerst of de linker mouse button de state "pressed" heeft. Als dat het geval is word wasCicked TRUE. Als die dan word released als hij nog op de button is word je doorverwezen. Als je met je muis van de button afgaat word wasClicked weer FALSE.