Psycop Posted December 19, 2012 at 07:00 PM Report #487841 Posted December 19, 2012 at 07:00 PM (edited) Boa Tarde Como pequeno exercicio de programação em C# estou a fazer um pequeno jogo "pong" em XNA. Já tenho uma grande parte da estrutura feita, mas estou com um problema que não consigo resolver, que são os seguintes: 1 - Colisões com os bastões e seguinte recolocação da bola. 2 - verificar colisões com os lados direito e esquerdo assim como a recolocação da bola no lado contrário ao lado em que colidiu assim como a soma dos pontos. O código que tenho é o seguinte: 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 Tetris { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; // Imagens dos objetos do jogo Texture2D texBastaoDireita; Texture2D texBastaoEsquerda; Texture2D texBola; //variaveis para contagem de pontos dos jogadores int pontosDir = 0; int pontosEsq = 0; SpriteFont Arial; Vector2 posBola = new Vector2(5.0f, 5.0f); KeyboardState keysboardState; float speed = 5.0f; // Vector que Guarda Informação da Posição da Bola. Vector2 bolaSpeed = new Vector2 (150.0f, 150.0f); //Posição do bastão Vector2 posBastaoEsquerda = new Vector2(5,320); Vector2 posBastaoDireita = new Vector2(780, 320); public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); // Carrega as texturas dos bastões e da bola texBastaoEsquerda = Content.Load<Texture2D>("bastao"); texBastaoDireita = Content.Load<Texture2D>("bastao"); texBola = Content.Load<Texture2D>("bola"); Arial = Content.Load<SpriteFont>("Arial"); } protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } protected override void Update(GameTime gameTime) { // Mover a Bola em Relação à velocidade / tempo decorrido posBola += bolaSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; int MaxX = graphics.GraphicsDevice.Viewport.Width - texBola.Width; int MinX = 0; int MaxY = graphics.GraphicsDevice.Viewport.Height - texBola.Height; int MinY = 0; //Maximos para os bastões int MaxbastaoY = graphics.GraphicsDevice.Viewport.Height - texBastaoDireita.Height; int MinbastaoY = 0; // Verificar a colisão dos bastões com as extremidades //Bastão Direito if (posBastaoDireita.Y > MaxbastaoY) { posBastaoDireita.Y *= -1; posBastaoDireita.Y = MaxbastaoY; } else if (posBastaoDireita.Y < MinbastaoY) { posBastaoDireita.Y *= +1; posBastaoDireita.Y = MinbastaoY; } //Bastão Esquerda if (posBastaoEsquerda.Y > MaxbastaoY) { posBastaoEsquerda.Y *= -1; posBastaoEsquerda.Y = MaxbastaoY; } else if (posBastaoEsquerda.Y < MinbastaoY) { posBastaoEsquerda.Y *= -1; posBastaoEsquerda.Y = MinbastaoY; } // Verificar a colisão da bola com as extremidades /* if (posBola.X > MaxX) { bolaSpeed.X *= -1; posBola.X = MaxbastaoY; } else if (posBola.X < MinX) { bolaSpeed.X *= -1; posBola.X = MinX; } */ if (posBola.Y > MaxY) { bolaSpeed.Y *= -1; posBola.Y = MaxY; } else if (posBola.Y < MinY) { bolaSpeed.Y *= -1; posBola.Y = MinY; } //Detectar Colisões Bola / Bastão //Bastão Direito if (posBola.X + texBola.Height >= posBastaoDireita.X) { bolaSpeed.X *= -1; posBola.X = MaxbastaoY; } //Bastão Esquerdo if (posBola.X - texBola.Height <= posBastaoEsquerda.X) { bolaSpeed.X *= -1; posBola.X = MaxbastaoY; } // Se a bola saiu pela lateral, soma os pontos e reposiciona a bola // saiu pela direita if (posBola.X > MaxX) { pontosDir = pontosDir + 1; posBola *= new Vector2(5.0f, 5.0f); } //saiu pela Esquerda if (posBola.X < MinX) { pontosEsq = pontosEsq + 1; posBola = new Vector2(775.0f, 5.0f); } // Declaramos o teclado keysboardState = Keyboard.GetState(); //Bastao Esquerda // Tecla para cima if (keysboardState.IsKeyDown(Keys.Up)) { posBastaoEsquerda.Y -= speed; } // Tecla para baixo if (keysboardState.IsKeyDown(Keys.Down)) { posBastaoEsquerda.Y += speed; } //Bastao Direita // Tecla para esquerda if (keysboardState.IsKeyDown(Keys.Left)) { posBastaoDireita.Y -= speed; } // Tecla para direita if (keysboardState.IsKeyDown(Keys.Right)) { posBastaoDireita.Y += speed; } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // Pinta o fundo da tela de preto graphics.GraphicsDevice.Clear(Color.Black); // Desenha as texturas na tela spriteBatch.Begin(); spriteBatch.Draw(texBola, posBola, Color.White); spriteBatch.Draw(texBastaoEsquerda, posBastaoEsquerda, Color.White); spriteBatch.Draw(texBastaoDireita, posBastaoDireita, Color.White); spriteBatch.DrawString(Arial, pontosEsq.ToString() + ":" + pontosDir.ToString(), new Vector2(380, 20), Color.White); spriteBatch.End(); base.Draw(gameTime); } } } Alguém me pode ajudar a entender o que estou a fazer mal e perceber como posso fazer correctamente? Neste momento queria perceber e colocar toda a mecânica do jogo a funcionar e só depois partir para a divisão do jogo e acções em classes específicas. Cumps Edited December 19, 2012 at 07:01 PM by Psycop
Sponsor Posted December 20, 2012 at 11:34 AM Report #487918 Posted December 20, 2012 at 11:34 AM Talvez fosse mais fácil usares rectangulos para determinares as colisões. Os rectangulos são os bounds das texturas.
Psycop Posted December 27, 2012 at 03:08 AM Author Report #488558 Posted December 27, 2012 at 03:08 AM (edited) Olá Estou a tentar fazer um MENU de um jogo e estou com uma dificuldade que é retroceder para o MainMenu quando a tecla "ESC" for pressionada estando num qualquer outro Case do Switch (Easy, Medium ou Hard). Para as actividades dos botões tenho a classe Button: 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 Pong_The_Game_1._01 { class cButton { Texture2D texture; Vector2 position; Rectangle rectangle; Color colour = new Color(255, 255, 255, 255); public Vector2 size; public cButton(Texture2D newTexture, GraphicsDevice graphics) { texture = newTexture; //screenW = 800, screenH = 600 //imdW = 100, imdH = 20 size = new Vector2(graphics.Viewport.Width / 5, graphics.Viewport.Height / 15); } bool down; public bool isClicked; public void update(MouseState mouse) { rectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y); Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1); if (mouseRectangle.Intersects(rectangle)) { if (mouse.LeftButton == ButtonState.Pressed) { isClicked = true; } else { isClicked = false; } } } public void setPosition(Vector2 newPosition) { position = newPosition; } public void Draw(SpriteBatch spriteBatch) { spriteBatch.Draw(texture, rectangle, colour); } } } Que depois uso no Game1: 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 Pong_The_Game_1._01 { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont Arial; enum GameState { MainMenu, Easy, Medium, Hard } GameState CurrentGameState = GameState.MainMenu; int screenWidth = 800, screenHeight = 600; cButton btnPlay_Easy; cButton btnPlay_Medium; cButton btnPlay_Hard; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); graphics.PreferredBackBufferWidth = screenWidth; graphics.PreferredBackBufferHeight = screenHeight; graphics.ApplyChanges(); IsMouseVisible = true; Arial = Content.Load<SpriteFont>("Arial"); btnPlay_Easy = new cButton(Content.Load<Texture2D>("easy"), graphics.GraphicsDevice); btnPlay_Easy.setPosition(new Vector2(320, 300)); btnPlay_Medium = new cButton(Content.Load<Texture2D>("medium"), graphics.GraphicsDevice); btnPlay_Medium.setPosition(new Vector2(320, 350)); btnPlay_Hard = new cButton(Content.Load<Texture2D>("hard"), graphics.GraphicsDevice); btnPlay_Hard.setPosition(new Vector2(320, 400)); } protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } protected override void Update(GameTime gameTime) { MouseState mouse = Mouse.GetState(); switch (CurrentGameState) { case GameState.MainMenu: { if (btnPlay_Easy.isClicked == true) CurrentGameState = GameState.Easy; btnPlay_Easy.update(mouse); if (btnPlay_Medium.isClicked == true) CurrentGameState = GameState.Medium; btnPlay_Medium.update(mouse); if (btnPlay_Hard.isClicked == true) CurrentGameState = GameState.Hard; btnPlay_Hard.update(mouse); break; } case GameState.Easy: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); ///O Que pretendo é que depois de estar dentro do GameState.Easy seja possivel voltar ao GameState.MainMenu pressionando a tecla "ESC". Como poderei fazer isto? break; } case GameState.Medium: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); break; } case GameState.Hard: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); break; } } base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // Desenha as texturas na tela spriteBatch.Begin(); switch (CurrentGameState) { case GameState.MainMenu: { spriteBatch.Draw(Content.Load<Texture2D>("background_pong_the_game"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White); btnPlay_Easy.Draw(spriteBatch); btnPlay_Medium.Draw(spriteBatch); btnPlay_Hard.Draw(spriteBatch); break; } case GameState.Easy: { //Desenhar Nome do Jogo ///O Que pretendo é que depois de estar dentro do GameState.Easy seja possivel voltar ao GameState.MainMenu pressionando a tecla "ESC". Como poderei fazer isto? break; } case GameState.Medium: { //Desenhar Nome do Jogo graphics.GraphicsDevice.Clear(Color.Black); spriteBatch.DrawString(Arial, "Pong_The_Game_Medium", new Vector2(220, 10), Color.White); } case GameState.Hard: { //Desenhar Nome do Jogo graphics.GraphicsDevice.Clear(Color.Black); break; } } spriteBatch.End(); base.Draw(gameTime); } } } Alguém me pode ajudar a resolver esta questão? Cumps Edited December 27, 2012 at 03:12 AM by Psycop
Sponsor Posted December 27, 2012 at 09:47 AM Report #488570 Posted December 27, 2012 at 09:47 AM (edited) No Update colocas este código: (neste caso é a tecla back) if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); Edited December 27, 2012 at 10:07 PM by apocsantos
Psycop Posted December 27, 2012 at 12:50 PM Author Report #488586 Posted December 27, 2012 at 12:50 PM (edited) No Update colocas este código: (neste caso é a tecla back) if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); Bom Dia Coquei isso no update e não funciona. Andei a experimentar umas coisas como isto: if (state.IsKeyDown(Keys.Escape)) { this.GameState.MainMenu; } A verdade é que se tiver this.Exit; ao clicar no "ESC" a aplicação termina, mas o que eu pretendo é voltar ao MainMenu. Como posso fazer isso visto que a expressão this.GameState.MainMenu; não é válida? Cumps Edited December 27, 2012 at 01:00 PM by Psycop
Sponsor Posted December 27, 2012 at 01:10 PM Report #488591 Posted December 27, 2012 at 01:10 PM Colocas uma variável no código para por exemplo te indicar por exemplo que queres voltar ao menu anterior. Quando no update verificas a tecla ESC dizes que essa variavel é true. Depois no Draw de acordo com essa variavel ou desenhas o menu ou a outra coisa. Não sei como tens estruturado o teu jogo...
Psycop Posted December 27, 2012 at 01:12 PM Author Report #488592 Posted December 27, 2012 at 01:12 PM (edited) Colocas uma variável no código para por exemplo te indicar por exemplo que queres voltar ao menu anterior. Quando no update verificas a tecla ESC dizes que essa variavel é true. Depois no Draw de acordo com essa variavel ou desenhas o menu ou a outra coisa. Não sei como tens estruturado o teu jogo... A estrutura do meu jogo é a que está postada 3 post's acima... Estou um pouco perdido com esta situação e não estou a conseguir resolver... Cumps Edited December 27, 2012 at 01:14 PM by Psycop
Sponsor Posted December 27, 2012 at 01:16 PM Report #488593 Posted December 27, 2012 at 01:16 PM (edited) Então no update metes o CurrentGameState = GameSatate.MainMenu quando a tecla for pressionada Edited December 27, 2012 at 01:17 PM by Marco_5
Psycop Posted December 27, 2012 at 05:25 PM Author Report #488638 Posted December 27, 2012 at 05:25 PM (edited) Mais uma vez aqui estou eu com dificuldades em implementar um Menu e Sub_Menus. O código do Game1.cs é o seguinte: //Iclusão de Menu's e retrocesso para o MainMenu 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 Pong_The_Game_1._01 { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont Arial; //Classe Bola Bola_Easy bola_Easy; Bola_Medium bola_Medium; Bola_Hard bola_Hard; //Classe Player Player player1; Player player2; //Textura a ser utilizada na classe Bola Texture2D imgBola; //Textura a ser utilizada na classe Player Texture2D imgPlayer; //Textura para gerar os pontos dos jogadores //Texture2D imgNumeros; //Textura da "rede" Texture2D imgRede; //Array que irá receber cada "fatia" da imagem com seu respectivo número Rectangle[] pontos = null; //Pontos dos jogadores_Easy int pontosPlayer1_Easy; int pontosPlayer2_Easy; //Pontos dos jogadores_Medium int pontosPlayer1_Medium; int pontosPlayer2_Medium; //Pontos dos jogadores_Hard int pontosPlayer1_Hard; int pontosPlayer2_Hard; enum GameState { MainMenu, Play, Controls, Credits, Easy, Medium, Hard } GameState CurrentGameState = GameState.MainMenu; int screenWidth = 800, screenHeight = 600; cButton btnPlay_Play; cButton btnPlay_Controls; cButton btnPlay_Credits; cButton btnPlay_Easy; cButton btnPlay_Medium; cButton btnPlay_Hard; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { bola_Easy = new Bola_Easy(); bola_Medium = new Bola_Medium(); bola_Hard = new Bola_Hard(); player1 = new Player(); player2 = new Player(); pontosPlayer1_Easy = 0; pontosPlayer2_Easy = 0; pontosPlayer1_Medium = 0; pontosPlayer2_Medium = 0; pontosPlayer1_Hard = 0; pontosPlayer2_Hard = 0; base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); graphics.PreferredBackBufferWidth = screenWidth; graphics.PreferredBackBufferHeight = screenHeight; graphics.ApplyChanges(); IsMouseVisible = true; Arial = Content.Load<SpriteFont>("Arial"); //Carregando as imagens que serão utilizadas no jogo imgBola = Content.Load<Texture2D>("bola"); imgPlayer = Content.Load<Texture2D>("player"); imgRede = Content.Load<Texture2D>("rede"); //Definições da Bola_Easy //Aqui definimos as propriedades da bola como Tamanho, Posição inicial na tela //velocidade e qual a textura a ser utilizada. //Note tambem que utilizamos a classe Windows.ClientBound para posicionarmos //nossa bola_Easy no centro da tela bola_Easy.Tamanho = new Vector2(imgBola.Width, imgBola.Height); bola_Easy.Posicao = new Vector2((window.ClientBounds.Width / 2) - (bola_Easy.Tamanho.X / 2), (window.ClientBounds.Height / 2) - (bola_Easy.Tamanho.Y / 2)); //nossa bola_Medium no centro da tela bola_Medium.Tamanho = new Vector2(imgBola.Width, imgBola.Height); bola_Medium.Posicao = new Vector2((window.ClientBounds.Width / 2) - (bola_Medium.Tamanho.X / 2), (window.ClientBounds.Height / 2) - (bola_Medium.Tamanho.Y / 2)); //nossa bola_Hard no centro da tela bola_Hard.Tamanho = new Vector2(imgBola.Width, imgBola.Height); bola_Hard.Posicao = new Vector2((window.ClientBounds.Width / 2) - (bola_Hard.Tamanho.X / 2), (window.ClientBounds.Height / 2) - (bola_Hard.Tamanho.Y / 2)); //velocidade Bola_Easy bola_Easy.Velocidade = new Vector2(5.0f, 5.0f); //velocidade Bola_Medium bola_Medium.Velocidade = new Vector2(8.0f, 8.0f); //velocidade Bola_Hard bola_Hard.Velocidade = new Vector2(10.0f, 10.0f); bola_Easy.Textura = imgBola; bola_Medium.Textura = imgBola; bola_Hard.Textura = imgBola; //Player 1 //Assim como a bola, definimos também os dados de cada player player1.Tamanho = new Vector2(imgPlayer.Width, imgPlayer.Height); player1.Posicao = new Vector2(10f, (window.ClientBounds.Height / 2 - imgPlayer.Height / 2)); player1.Velocidade = new Vector2(0, 5); player1.Textura = imgPlayer; //Player 2 player2.Tamanho = new Vector2(imgPlayer.Width, imgPlayer.Height); player2.Posicao = new Vector2((window.ClientBounds.Width - 10 - imgPlayer.Width), (window.ClientBounds.Height / 2 - imgPlayer.Height / 2)); player2.Velocidade = new Vector2(0, 5); player2.Textura = imgPlayer; pontos = new Rectangle[10]; //Carrega um array com a definição dos numeros //Aqui realizamos o preenchimento de nosso array com seus //respectivos valores para termos a posição certa de cada //fatia dentro da imagem original for (int i = 0; i < 10; i++) { pontos[i] = new Rectangle(i * 45, 0, 45, 75); } btnPlay_Play = new cButton(Content.Load<Texture2D>("play"), graphics.GraphicsDevice); btnPlay_Play.setPosition(new Vector2(320, 300)); btnPlay_Controls = new cButton(Content.Load<Texture2D>("controls"), graphics.GraphicsDevice); btnPlay_Controls.setPosition(new Vector2(320, 350)); btnPlay_Credits = new cButton(Content.Load<Texture2D>("credits"), graphics.GraphicsDevice); btnPlay_Credits.setPosition(new Vector2(320, 400)); btnPlay_Easy = new cButton(Content.Load<Texture2D>("easy"), graphics.GraphicsDevice); btnPlay_Easy.setPosition(new Vector2(320, 300)); btnPlay_Medium = new cButton(Content.Load<Texture2D>("medium"), graphics.GraphicsDevice); btnPlay_Medium.setPosition(new Vector2(320, 350)); btnPlay_Hard = new cButton(Content.Load<Texture2D>("hard"), graphics.GraphicsDevice); btnPlay_Hard.setPosition(new Vector2(320, 400)); } protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } protected override void Update(GameTime gameTime) { MouseState mouse = Mouse.GetState(); KeyboardState keyboardstate = Keyboard.GetState(); switch (CurrentGameState) { case GameState.MainMenu: { if (btnPlay_Play.isClicked == true) CurrentGameState = GameState.Play; btnPlay_Play.update(mouse); if (btnPlay_Controls.isClicked == true) CurrentGameState = GameState.Controls; btnPlay_Controls.update(mouse); if (btnPlay_Credits.isClicked == true) CurrentGameState = GameState.Credits; btnPlay_Credits.update(mouse); break; } case GameState.Play: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Menu dentro do Play switch (CurrentGameState) { case GameState.Play: { if (btnPlay_Easy.isClicked == true) CurrentGameState = GameState.Easy; btnPlay_Easy.update(mouse); if (btnPlay_Medium.isClicked == true) CurrentGameState = GameState.Medium; btnPlay_Medium.update(mouse); if (btnPlay_Hard.isClicked == true) CurrentGameState = GameState.Hard; btnPlay_Hard.update(mouse); break; } case GameState.Easy: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Retroceder para o Play if (keyboardstate.IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.Play; break; } break; } case GameState.Medium: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Retroceder para o MainMenu if (keyboardstate.IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.Play; break; } break; } case GameState.Hard: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Retroceder para o MainMenu if (keyboardstate.IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.Play; break; } break; } } //Retroceder para o MainMenu if (keyboardstate.IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.MainMenu; break; } break; } case GameState.Controls: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); break; } case GameState.Credits: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); break; } } //Bola_Easy bola_Easy.checaColisao_Easy(player1.Retangulo, player2.Retangulo); bola_Easy.mover_Easy(Window, ref pontosPlayer1_Easy, ref pontosPlayer2_Easy); movePlayers(); if (pontosPlayer1_Easy > 9 || pontosPlayer2_Easy > 9) { pontosPlayer1_Easy = 0; pontosPlayer2_Easy = 0; } //Bola_Medium bola_Medium.checaColisao_Medium(player1.Retangulo, player2.Retangulo); bola_Medium.mover_Medium(Window, ref pontosPlayer1_Medium, ref pontosPlayer2_Medium); movePlayers(); if (pontosPlayer1_Medium > 9 || pontosPlayer2_Medium > 9) { pontosPlayer1_Medium = 0; pontosPlayer2_Medium = 0; } //Bola_Hard bola_Hard.checaColisao_Hard(player1.Retangulo, player2.Retangulo); bola_Hard.mover_Hard(Window, ref pontosPlayer1_Hard, ref pontosPlayer2_Hard); movePlayers(); if (pontosPlayer1_Hard > 9 || pontosPlayer2_Hard > 9) { pontosPlayer1_Hard = 0; pontosPlayer2_Hard = 0; } base.Update(gameTime); } public void movePlayers() { KeyboardState keys = Keyboard.GetState(); //Player 1 if (keys.IsKeyDown(Keys.A)) { if (player1.Posicao.Y < 0) player1.Posicao = new Vector2(player1.Posicao.X, 0); player1.Posicao -= player1.Velocidade; } if (keys.IsKeyDown(Keys.Z)) { if (player1.Posicao.Y > window.ClientBounds.Height - player1.Textura.Height) player1.Posicao = new Vector2(player1.Posicao.X, (window.ClientBounds.Height - imgPlayer.Height)); player1.Posicao += player1.Velocidade; } //Player 2 if (keys.IsKeyDown(Keys.Up)) { if (player2.Posicao.Y < 0) player2.Posicao = new Vector2(player2.Posicao.X, 0); player2.Posicao -= player2.Velocidade; } if (keys.IsKeyDown(Keys.Down)) { if (player2.Posicao.Y > window.ClientBounds.Height - player2.Textura.Height) player2.Posicao = new Vector2(player2.Posicao.X, (window.ClientBounds.Height - imgPlayer.Height)); player2.Posicao += player2.Velocidade; } //Voltar ao Menu Inicial if (keys.IsKeyDown(Keys.O)) { } } protected override void Draw(GameTime gameTime) { // Desenha as texturas na tela spriteBatch.Begin(); switch (CurrentGameState) { case GameState.MainMenu: { spriteBatch.Draw(Content.Load<Texture2D>("background_pong_the_game"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White); btnPlay_Play.Draw(spriteBatch); btnPlay_Controls.Draw(spriteBatch); btnPlay_Credits.Draw(spriteBatch); break; } case GameState.Play: { //graphics.GraphicsDevice.Clear(Color.White); spriteBatch.Draw(Content.Load<Texture2D>("background_pong_the_game"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White); btnPlay_Easy.Draw(spriteBatch); btnPlay_Medium.Draw(spriteBatch); btnPlay_Hard.Draw(spriteBatch); break; switch (CurrentGameState) { case GameState.Easy: { break; } case GameState.Medium: { break; } case GameState.Hard: { break; } break; } } case GameState.Controls: { break; } case GameState.Credits: { break; } } spriteBatch.End(); base.Draw(gameTime); } } } A minha dificuldade actual é que após entrar no Sub_Menu "Play" os botões "Easy", "Medium" e "Hard" Não são apresentados, ficando a tela limpa sem qualquer coisa impressa. No entanto o MENU principal e as opções "Play", "Controls" e "Credits" aparecem correctamente... Já verifiquei todo o código e não consigo encontrar nenhum erro ou problema. Alguém tem umas dicas para me dar? Cumps Edited December 27, 2012 at 05:26 PM by Psycop
Sponsor Posted December 27, 2012 at 05:59 PM Report #488648 Posted December 27, 2012 at 05:59 PM (edited) Viva, primeiro no Draw estas a carregar Texturas e não é indicado fazeres isso ai... Depois o que é o "Play"? É ojogo é o menu?! Explique a ordem do seu jogo a ordem que são apresentados os ecras do seu jogo! Edited December 27, 2012 at 05:59 PM by Marco_5
Psycop Posted December 27, 2012 at 06:03 PM Author Report #488649 Posted December 27, 2012 at 06:03 PM (edited) Olá Marco... Como já deve ter percebido eu sou inexperiente em XNA, assim como um aprendiz de C#. O Play tem como objectivo levar a um Sub_menu. Ou seja: MENU: - Play: - Easy - Medium - Hard - Controls: - Credits: Tal como disseste que é errado fazer o load no Draw, corrigi essa situação da seguinte forma: //Iclusão de Menu's e retrocesso para o MainMenu 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 Pong_The_Game_1._01 { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont Arial; //Classe Bola Bola_Easy bola_Easy; Bola_Medium bola_Medium; Bola_Hard bola_Hard; //Classe Player Player player1; Player player2; //Textura a ser utilizada na classe Bola Texture2D imgBola; //Textura a ser utilizada na classe Player Texture2D imgPlayer; //Textura para gerar os pontos dos jogadores //Texture2D imgNumeros; Texture2D background; //Textura da "rede" Texture2D imgRede; //Array que irá receber cada "fatia" da imagem com seu respectivo número Rectangle[] pontos = null; //Pontos dos jogadores_Easy int pontosPlayer1_Easy; int pontosPlayer2_Easy; //Pontos dos jogadores_Medium int pontosPlayer1_Medium; int pontosPlayer2_Medium; //Pontos dos jogadores_Hard int pontosPlayer1_Hard; int pontosPlayer2_Hard; enum GameState { MainMenu, Play, Controls, Credits, Easy, Medium, Hard } GameState CurrentGameState = GameState.MainMenu; int screenWidth = 800, screenHeight = 600; cButton btnPlay_Play; cButton btnPlay_Controls; cButton btnPlay_Credits; cButton btnPlay_Easy; cButton btnPlay_Medium; cButton btnPlay_Hard; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { bola_Easy = new Bola_Easy(); bola_Medium = new Bola_Medium(); bola_Hard = new Bola_Hard(); player1 = new Player(); player2 = new Player(); pontosPlayer1_Easy = 0; pontosPlayer2_Easy = 0; pontosPlayer1_Medium = 0; pontosPlayer2_Medium = 0; pontosPlayer1_Hard = 0; pontosPlayer2_Hard = 0; base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); graphics.PreferredBackBufferWidth = screenWidth; graphics.PreferredBackBufferHeight = screenHeight; graphics.ApplyChanges(); IsMouseVisible = true; Arial = Content.Load<SpriteFont>("Arial"); //Carregando as imagens que serão utilizadas no jogo imgBola = Content.Load<Texture2D>("bola"); imgPlayer = Content.Load<Texture2D>("player"); imgRede = Content.Load<Texture2D>("rede"); //Definições da Bola_Easy //Aqui definimos as propriedades da bola como Tamanho, Posição inicial na tela //velocidade e qual a textura a ser utilizada. //Note tambem que utilizamos a classe Windows.ClientBound para posicionarmos //nossa bola_Easy no centro da tela bola_Easy.Tamanho = new Vector2(imgBola.Width, imgBola.Height); bola_Easy.Posicao = new Vector2((window.ClientBounds.Width / 2) - (bola_Easy.Tamanho.X / 2), (window.ClientBounds.Height / 2) - (bola_Easy.Tamanho.Y / 2)); //nossa bola_Medium no centro da tela bola_Medium.Tamanho = new Vector2(imgBola.Width, imgBola.Height); bola_Medium.Posicao = new Vector2((window.ClientBounds.Width / 2) - (bola_Medium.Tamanho.X / 2), (window.ClientBounds.Height / 2) - (bola_Medium.Tamanho.Y / 2)); //nossa bola_Hard no centro da tela bola_Hard.Tamanho = new Vector2(imgBola.Width, imgBola.Height); bola_Hard.Posicao = new Vector2((window.ClientBounds.Width / 2) - (bola_Hard.Tamanho.X / 2), (window.ClientBounds.Height / 2) - (bola_Hard.Tamanho.Y / 2)); //velocidade Bola_Easy bola_Easy.Velocidade = new Vector2(5.0f, 5.0f); //velocidade Bola_Medium bola_Medium.Velocidade = new Vector2(8.0f, 8.0f); //velocidade Bola_Hard bola_Hard.Velocidade = new Vector2(10.0f, 10.0f); bola_Easy.Textura = imgBola; bola_Medium.Textura = imgBola; bola_Hard.Textura = imgBola; //Player 1 //Assim como a bola, definimos também os dados de cada player player1.Tamanho = new Vector2(imgPlayer.Width, imgPlayer.Height); player1.Posicao = new Vector2(10f, (window.ClientBounds.Height / 2 - imgPlayer.Height / 2)); player1.Velocidade = new Vector2(0, 5); player1.Textura = imgPlayer; //Player 2 player2.Tamanho = new Vector2(imgPlayer.Width, imgPlayer.Height); player2.Posicao = new Vector2((window.ClientBounds.Width - 10 - imgPlayer.Width), (window.ClientBounds.Height / 2 - imgPlayer.Height / 2)); player2.Velocidade = new Vector2(0, 5); player2.Textura = imgPlayer; pontos = new Rectangle[10]; //Carrega um array com a definição dos numeros //Aqui realizamos o preenchimento de nosso array com seus //respectivos valores para termos a posição certa de cada //fatia dentro da imagem original for (int i = 0; i < 10; i++) { pontos[i] = new Rectangle(i * 45, 0, 45, 75); } btnPlay_Play = new cButton(Content.Load<Texture2D>("play"), graphics.GraphicsDevice); btnPlay_Play.setPosition(new Vector2(320, 300)); btnPlay_Controls = new cButton(Content.Load<Texture2D>("controls"), graphics.GraphicsDevice); btnPlay_Controls.setPosition(new Vector2(320, 350)); btnPlay_Credits = new cButton(Content.Load<Texture2D>("credits"), graphics.GraphicsDevice); btnPlay_Credits.setPosition(new Vector2(320, 400)); btnPlay_Easy = new cButton(Content.Load<Texture2D>("easy"), graphics.GraphicsDevice); btnPlay_Easy.setPosition(new Vector2(320, 300)); btnPlay_Medium = new cButton(Content.Load<Texture2D>("medium"), graphics.GraphicsDevice); btnPlay_Medium.setPosition(new Vector2(320, 350)); btnPlay_Hard = new cButton(Content.Load<Texture2D>("hard"), graphics.GraphicsDevice); btnPlay_Hard.setPosition(new Vector2(320, 400)); background = Content.Load<Texture2D>("background_pong_the_game"); } protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } protected override void Update(GameTime gameTime) { MouseState mouse = Mouse.GetState(); KeyboardState keyboardstate = Keyboard.GetState(); switch (CurrentGameState) { case GameState.MainMenu: { if (btnPlay_Play.isClicked == true) CurrentGameState = GameState.Play; btnPlay_Play.update(mouse); if (btnPlay_Controls.isClicked == true) CurrentGameState = GameState.Controls; btnPlay_Controls.update(mouse); if (btnPlay_Credits.isClicked == true) CurrentGameState = GameState.Credits; btnPlay_Credits.update(mouse); break; } case GameState.Play: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Menu dentro do Play switch (CurrentGameState) { case GameState.Play: { if (btnPlay_Easy.isClicked == true) CurrentGameState = GameState.Easy; btnPlay_Easy.update(mouse); if (btnPlay_Medium.isClicked == true) CurrentGameState = GameState.Medium; btnPlay_Medium.update(mouse); if (btnPlay_Hard.isClicked == true) CurrentGameState = GameState.Hard; btnPlay_Hard.update(mouse); break; } case GameState.Easy: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Retroceder para o Play if (keyboardstate.IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.Play; break; } break; } case GameState.Medium: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Retroceder para o MainMenu if (keyboardstate.IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.Play; break; } break; } case GameState.Hard: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Retroceder para o MainMenu if (keyboardstate.IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.Play; break; } break; } } //Retroceder para o MainMenu if (keyboardstate.IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.MainMenu; break; } break; } case GameState.Controls: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); break; } case GameState.Credits: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); break; } } //Bola_Easy bola_Easy.checaColisao_Easy(player1.Retangulo, player2.Retangulo); bola_Easy.mover_Easy(Window, ref pontosPlayer1_Easy, ref pontosPlayer2_Easy); movePlayers(); if (pontosPlayer1_Easy > 9 || pontosPlayer2_Easy > 9) { pontosPlayer1_Easy = 0; pontosPlayer2_Easy = 0; } //Bola_Medium bola_Medium.checaColisao_Medium(player1.Retangulo, player2.Retangulo); bola_Medium.mover_Medium(Window, ref pontosPlayer1_Medium, ref pontosPlayer2_Medium); movePlayers(); if (pontosPlayer1_Medium > 9 || pontosPlayer2_Medium > 9) { pontosPlayer1_Medium = 0; pontosPlayer2_Medium = 0; } //Bola_Hard bola_Hard.checaColisao_Hard(player1.Retangulo, player2.Retangulo); bola_Hard.mover_Hard(Window, ref pontosPlayer1_Hard, ref pontosPlayer2_Hard); movePlayers(); if (pontosPlayer1_Hard > 9 || pontosPlayer2_Hard > 9) { pontosPlayer1_Hard = 0; pontosPlayer2_Hard = 0; } base.Update(gameTime); } public void movePlayers() { KeyboardState keys = Keyboard.GetState(); //Player 1 if (keys.IsKeyDown(Keys.A)) { if (player1.Posicao.Y < 0) player1.Posicao = new Vector2(player1.Posicao.X, 0); player1.Posicao -= player1.Velocidade; } if (keys.IsKeyDown(Keys.Z)) { if (player1.Posicao.Y > window.ClientBounds.Height - player1.Textura.Height) player1.Posicao = new Vector2(player1.Posicao.X, (window.ClientBounds.Height - imgPlayer.Height)); player1.Posicao += player1.Velocidade; } //Player 2 if (keys.IsKeyDown(Keys.Up)) { if (player2.Posicao.Y < 0) player2.Posicao = new Vector2(player2.Posicao.X, 0); player2.Posicao -= player2.Velocidade; } if (keys.IsKeyDown(Keys.Down)) { if (player2.Posicao.Y > window.ClientBounds.Height - player2.Textura.Height) player2.Posicao = new Vector2(player2.Posicao.X, (window.ClientBounds.Height - imgPlayer.Height)); player2.Posicao += player2.Velocidade; } //Voltar ao Menu Inicial if (keys.IsKeyDown(Keys.O)) { } } protected override void Draw(GameTime gameTime) { // Desenha as texturas na tela spriteBatch.Begin(); switch (CurrentGameState) { case GameState.MainMenu: { spriteBatch.Draw(background, new Rectangle(0, 0, 800, 600), Color.White); btnPlay_Play.Draw(spriteBatch); btnPlay_Controls.Draw(spriteBatch); btnPlay_Credits.Draw(spriteBatch); break; } case GameState.Play: { //graphics.GraphicsDevice.Clear(Color.White); spriteBatch.Draw(background, new Rectangle(0, 0, 800, 600), Color.White); btnPlay_Easy.Draw(spriteBatch); btnPlay_Medium.Draw(spriteBatch); btnPlay_Hard.Draw(spriteBatch); break; switch (CurrentGameState) { case GameState.Easy: { break; } case GameState.Medium: { break; } case GameState.Hard: { break; } } break; } case GameState.Controls: { break; } case GameState.Credits: { break; } } spriteBatch.End(); base.Draw(gameTime); } } } É esta estrutura que estou a tentar implementar. Antes de passar a optimizar e corrigir tudo o que não está "tão" bem feito queria ter tudo a funcionar e então depois explorar e optimizar os métodos. Edited December 27, 2012 at 06:48 PM by Psycop
Psycop Posted December 27, 2012 at 08:33 PM Author Report #488681 Posted December 27, 2012 at 08:33 PM Após algumas mudanças no código, continuo com o mesmo problema no Sub_Menu "Play" onde as opções continuam sem aparecer. O código é o seguinte: //Iclusão de Menu's e retrocesso para o MainMenu 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 Pong_The_Game_1._01 { public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont Arial; //Classe Bola Bola_Easy bola_Easy; Bola_Medium bola_Medium; Bola_Hard bola_Hard; //Classe Player Player player1; Player player2; //Textura a ser utilizada na classe Bola Texture2D imgBola; //Textura a ser utilizada na classe Player Texture2D imgPlayer; //Texture2D Imagns Background; Texture2D background; Texture2D controls_background; Texture2D credits_background; //Textura da "rede" Texture2D imgRede; //Array que irá receber cada "fatia" da imagem com seu respectivo número Rectangle[] pontos = null; //Pontos dos jogadores_Easy int pontosPlayer1_Easy; int pontosPlayer2_Easy; //Pontos dos jogadores_Medium int pontosPlayer1_Medium; int pontosPlayer2_Medium; //Pontos dos jogadores_Hard int pontosPlayer1_Hard; int pontosPlayer2_Hard; enum GameState { MainMenu, Play_Menu, Play, Controls, Credits, Easy, Medium, Hard } GameState CurrentGameState = GameState.MainMenu; int screenWidth = 800, screenHeight = 600; cButton btnPlay_Play; cButton btnPlay_Controls; cButton btnPlay_Credits; cButton btnPlay_Easy; cButton btnPlay_Medium; cButton btnPlay_Hard; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { bola_Easy = new Bola_Easy(); bola_Medium = new Bola_Medium(); bola_Hard = new Bola_Hard(); player1 = new Player(); player2 = new Player(); pontosPlayer1_Easy = 0; pontosPlayer2_Easy = 0; pontosPlayer1_Medium = 0; pontosPlayer2_Medium = 0; pontosPlayer1_Hard = 0; pontosPlayer2_Hard = 0; base.Initialize(); } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); graphics.PreferredBackBufferWidth = screenWidth; graphics.PreferredBackBufferHeight = screenHeight; graphics.ApplyChanges(); IsMouseVisible = true; Arial = Content.Load<SpriteFont>("Arial"); //Carregando as imagens que serão utilizadas no jogo imgBola = Content.Load<Texture2D>("bola"); imgPlayer = Content.Load<Texture2D>("player"); imgRede = Content.Load<Texture2D>("rede"); //Definições da Bola_Easy //Aqui definimos as propriedades da bola como Tamanho, Posição inicial na tela //velocidade e qual a textura a ser utilizada. //Note tambem que utilizamos a classe Windows.ClientBound para posicionarmos //nossa bola_Easy no centro da tela bola_Easy.Tamanho = new Vector2(imgBola.Width, imgBola.Height); bola_Easy.Posicao = new Vector2((window.ClientBounds.Width / 2) - (bola_Easy.Tamanho.X / 2), (window.ClientBounds.Height / 2) - (bola_Easy.Tamanho.Y / 2)); //nossa bola_Medium no centro da tela bola_Medium.Tamanho = new Vector2(imgBola.Width, imgBola.Height); bola_Medium.Posicao = new Vector2((window.ClientBounds.Width / 2) - (bola_Medium.Tamanho.X / 2), (window.ClientBounds.Height / 2) - (bola_Medium.Tamanho.Y / 2)); //nossa bola_Hard no centro da tela bola_Hard.Tamanho = new Vector2(imgBola.Width, imgBola.Height); bola_Hard.Posicao = new Vector2((window.ClientBounds.Width / 2) - (bola_Hard.Tamanho.X / 2), (window.ClientBounds.Height / 2) - (bola_Hard.Tamanho.Y / 2)); //velocidade Bola_Easy bola_Easy.Velocidade = new Vector2(5.0f, 5.0f); //velocidade Bola_Medium bola_Medium.Velocidade = new Vector2(8.0f, 8.0f); //velocidade Bola_Hard bola_Hard.Velocidade = new Vector2(10.0f, 10.0f); bola_Easy.Textura = imgBola; bola_Medium.Textura = imgBola; bola_Hard.Textura = imgBola; //Player 1 //Assim como a bola, definimos também os dados de cada player player1.Tamanho = new Vector2(imgPlayer.Width, imgPlayer.Height); player1.Posicao = new Vector2(10f, (window.ClientBounds.Height / 2 - imgPlayer.Height / 2)); player1.Velocidade = new Vector2(0, 5); player1.Textura = imgPlayer; //Player 2 player2.Tamanho = new Vector2(imgPlayer.Width, imgPlayer.Height); player2.Posicao = new Vector2((window.ClientBounds.Width - 10 - imgPlayer.Width), (window.ClientBounds.Height / 2 - imgPlayer.Height / 2)); player2.Velocidade = new Vector2(0, 5); player2.Textura = imgPlayer; pontos = new Rectangle[10]; //Carrega um array com a definição dos numeros //Aqui realizamos o preenchimento de nosso array com seus //respectivos valores para termos a posição certa de cada //fatia dentro da imagem original for (int i = 0; i < 10; i++) { pontos[i] = new Rectangle(i * 45, 0, 45, 75); } btnPlay_Play = new cButton(Content.Load<Texture2D>("play"), graphics.GraphicsDevice); btnPlay_Play.setPosition(new Vector2(320, 300)); btnPlay_Controls = new cButton(Content.Load<Texture2D>("controls"), graphics.GraphicsDevice); btnPlay_Controls.setPosition(new Vector2(320, 350)); btnPlay_Credits = new cButton(Content.Load<Texture2D>("credits"), graphics.GraphicsDevice); btnPlay_Credits.setPosition(new Vector2(320, 400)); btnPlay_Easy = new cButton(Content.Load<Texture2D>("easy"), graphics.GraphicsDevice); btnPlay_Easy.setPosition(new Vector2(320, 300)); btnPlay_Medium = new cButton(Content.Load<Texture2D>("medium"), graphics.GraphicsDevice); btnPlay_Medium.setPosition(new Vector2(320, 350)); btnPlay_Hard = new cButton(Content.Load<Texture2D>("hard"), graphics.GraphicsDevice); btnPlay_Hard.setPosition(new Vector2(320, 400)); background = Content.Load<Texture2D>("background_pong_the_game"); controls_background = Content.Load<Texture2D>("controls_background"); credits_background = Content.Load<Texture2D>("credits_background"); } protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } protected override void Update(GameTime gameTime) { MouseState mouse = Mouse.GetState(); KeyboardState keyboardstate = Keyboard.GetState(); switch (CurrentGameState) { case GameState.MainMenu: { if (btnPlay_Play.isClicked == true) CurrentGameState = GameState.Play; btnPlay_Play.update(mouse); if (btnPlay_Controls.isClicked == true) CurrentGameState = GameState.Controls; btnPlay_Controls.update(mouse); if (btnPlay_Credits.isClicked == true) CurrentGameState = GameState.Credits; btnPlay_Credits.update(mouse); break; } case GameState.Play: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Menu dentro do Play switch (CurrentGameState) { case GameState.Play: { if (btnPlay_Easy.isClicked == true) CurrentGameState = GameState.Easy; btnPlay_Easy.update(mouse); if (btnPlay_Medium.isClicked == true) CurrentGameState = GameState.Medium; btnPlay_Medium.update(mouse); if (btnPlay_Hard.isClicked == true) CurrentGameState = GameState.Hard; btnPlay_Hard.update(mouse); break; } case GameState.Easy: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); break; } case GameState.Medium: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); break; } case GameState.Hard: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); break; } } break; } case GameState.Controls: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Retroceder para o MainMenu if (keyboardstate.IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.MainMenu; break; } break; } case GameState.Credits: { //Desenhar Nome do Jogo //spriteBatch.DrawString(Arial, "Pong_The_Game", new Vector2(260, 10), Color.White); //Retroceder para o MainMenu if (keyboardstate.IsKeyDown(Keys.Escape)) { CurrentGameState = GameState.MainMenu; break; } break; } } //Bola_Easy bola_Easy.checaColisao_Easy(player1.Retangulo, player2.Retangulo); bola_Easy.mover_Easy(Window, ref pontosPlayer1_Easy, ref pontosPlayer2_Easy); movePlayers(); if (pontosPlayer1_Easy > 9 || pontosPlayer2_Easy > 9) { pontosPlayer1_Easy = 0; pontosPlayer2_Easy = 0; } //Bola_Medium bola_Medium.checaColisao_Medium(player1.Retangulo, player2.Retangulo); bola_Medium.mover_Medium(Window, ref pontosPlayer1_Medium, ref pontosPlayer2_Medium); movePlayers(); if (pontosPlayer1_Medium > 9 || pontosPlayer2_Medium > 9) { pontosPlayer1_Medium = 0; pontosPlayer2_Medium = 0; } //Bola_Hard bola_Hard.checaColisao_Hard(player1.Retangulo, player2.Retangulo); bola_Hard.mover_Hard(Window, ref pontosPlayer1_Hard, ref pontosPlayer2_Hard); movePlayers(); if (pontosPlayer1_Hard > 9 || pontosPlayer2_Hard > 9) { pontosPlayer1_Hard = 0; pontosPlayer2_Hard = 0; } base.Update(gameTime); } public void movePlayers() { KeyboardState keys = Keyboard.GetState(); //Player 1 if (keys.IsKeyDown(Keys.A)) { if (player1.Posicao.Y < 0) player1.Posicao = new Vector2(player1.Posicao.X, 0); player1.Posicao -= player1.Velocidade; } if (keys.IsKeyDown(Keys.Z)) { if (player1.Posicao.Y > window.ClientBounds.Height - player1.Textura.Height) player1.Posicao = new Vector2(player1.Posicao.X, (window.ClientBounds.Height - imgPlayer.Height)); player1.Posicao += player1.Velocidade; } //Player 2 if (keys.IsKeyDown(Keys.Up)) { if (player2.Posicao.Y < 0) player2.Posicao = new Vector2(player2.Posicao.X, 0); player2.Posicao -= player2.Velocidade; } if (keys.IsKeyDown(Keys.Down)) { if (player2.Posicao.Y > window.ClientBounds.Height - player2.Textura.Height) player2.Posicao = new Vector2(player2.Posicao.X, (window.ClientBounds.Height - imgPlayer.Height)); player2.Posicao += player2.Velocidade; } //Voltar ao Menu Inicial if (keys.IsKeyDown(Keys.O)) { } } protected override void Draw(GameTime gameTime) { // Desenha as texturas na tela spriteBatch.Begin(); switch (CurrentGameState) { case GameState.MainMenu: { spriteBatch.Draw(background, new Rectangle(0, 0, 800, 600), Color.White); btnPlay_Play.Draw(spriteBatch); btnPlay_Controls.Draw(spriteBatch); btnPlay_Credits.Draw(spriteBatch); break; } case GameState.Play: { //graphics.GraphicsDevice.Clear(Color.White); spriteBatch.Draw(background, new Rectangle(0, 0, 800, 600), Color.White); btnPlay_Easy.Draw(spriteBatch); btnPlay_Medium.Draw(spriteBatch); btnPlay_Hard.Draw(spriteBatch); switch (CurrentGameState) { case GameState.Easy: { graphics.GraphicsDevice.Clear(Color.Black); //Posicionamos nossa "rede" no centro da tela spriteBatch.Draw(imgRede, new Vector2((window.ClientBounds.Width * .5f), 0), Color.White); //Desenhar Nome do Jogo spriteBatch.DrawString(Arial, "Pong_The_Game_Easy", new Vector2(220, 10), Color.White); //Desenhamos a pontuação do player 1 e do player 2 spriteBatch.DrawString(Arial, pontosPlayer1_Easy.ToString() + ":" + pontosPlayer2_Easy.ToString(), new Vector2(380, 50), Color.White); //Desenhamos nossa "bola" apesar de ser quadrada..rsrs spriteBatch.Draw(bola_Easy.Textura, bola_Easy.Retangulo, Color.White); //Desenhamos os Player, um de cada lado spriteBatch.Draw(player1.Textura, player1.Retangulo, Color.White); spriteBatch.Draw(player2.Textura, player2.Retangulo, Color.White); break; } case GameState.Medium: { //Posicionamos nossa "rede" no centro da tela spriteBatch.Draw(imgRede, new Vector2((window.ClientBounds.Width * .5f), 0), Color.White); //Desenhar Nome do Jogo graphics.GraphicsDevice.Clear(Color.Black); spriteBatch.DrawString(Arial, "Pong_The_Game_Medium", new Vector2(220, 10), Color.White); //Desenhamos a pontuação do player 1 e do player 2 spriteBatch.DrawString(Arial, pontosPlayer1_Medium.ToString() + ":" + pontosPlayer2_Medium.ToString(), new Vector2(380, 50), Color.White); //Desenhamos nossa "bola" apesar de ser quadrada..rsrs spriteBatch.Draw(bola_Medium.Textura, bola_Medium.Retangulo, Color.White); //Desenhamos os Player, um de cada lado spriteBatch.Draw(player1.Textura, player1.Retangulo, Color.White); spriteBatch.Draw(player2.Textura, player2.Retangulo, Color.White); break; } case GameState.Hard: { //Posicionamos nossa "rede" no centro da tela spriteBatch.Draw(imgRede, new Vector2((window.ClientBounds.Width * .5f), 0), Color.White); //Desenhar Nome do Jogo graphics.GraphicsDevice.Clear(Color.Black); spriteBatch.DrawString(Arial, "Pong_The_Game_Hard", new Vector2(220, 10), Color.White); //Desenhamos a pontuação do player 1 e do player 2 spriteBatch.DrawString(Arial, pontosPlayer1_Hard.ToString() + ":" + pontosPlayer2_Hard.ToString(), new Vector2(380, 50), Color.White); //Desenhamos nossa "bola" apesar de ser quadrada..rsrs spriteBatch.Draw(bola_Hard.Textura, bola_Hard.Retangulo, Color.White); //Desenhamos os Player, um de cada lado spriteBatch.Draw(player1.Textura, player1.Retangulo, Color.White); spriteBatch.Draw(player2.Textura, player2.Retangulo, Color.White); break; } } break; } case GameState.Controls: { spriteBatch.Draw(controls_background, new Rectangle(0, 0, 800, 600), Color.White); break; } case GameState.Credits: { spriteBatch.Draw(credits_background, new Rectangle(0, 0, 800, 600), Color.White); break; } } spriteBatch.End(); base.Draw(gameTime); } } } O que poderei estar eu a fazer de errado?
Sponsor Posted December 28, 2012 at 10:37 AM Report #488772 Posted December 28, 2012 at 10:37 AM (edited) Tu tens isto: case GameState.Play: { //graphics.GraphicsDevice.Clear(Color.White); spriteBatch.Draw(background, new Rectangle(0, 0, 800, 600), Color.White); btnPlay_Easy.Draw(spriteBatch); btnPlay_Medium.Draw(spriteBatch); btnPlay_Hard.Draw(spriteBatch); switch (CurrentGameState) { case GameState.Easy: { graphics.GraphicsDevice.Clear(Color.Black); //Posicionamos nossa "rede" no centro da tela spriteBatch.Draw(imgRede, new Vector2((window.ClientBounds.Width * .5f), 0), Color.White); //Desenhar Nome do Jogo spriteBatch.DrawString(Arial, "Pong_The_Game_Easy", new Vector2(220, 10), Color.White); //Desenhamos a pontuação do player 1 e do player 2 spriteBatch.DrawString(Arial, pontosPlayer1_Easy.ToString() + ":" + pontosPlayer2_Easy.ToString(), new Vector2(380, 50), Color.White); //Desenhamos nossa "bola" apesar de ser quadrada..rsrs spriteBatch.Draw(bola_Easy.Textura, bola_Easy.Retangulo, Color.White); //Desenhamos os Player, um de cada lado spriteBatch.Draw(player1.Textura, player1.Retangulo, Color.White); spriteBatch.Draw(player2.Textura, player2.Retangulo, Color.White); break; } case GameState.Medium: { //Posicionamos nossa "rede" no centro da tela spriteBatch.Draw(imgRede, new Vector2((window.ClientBounds.Width * .5f), 0), Color.White); //Desenhar Nome do Jogo graphics.GraphicsDevice.Clear(Color.Black); spriteBatch.DrawString(Arial, "Pong_The_Game_Medium", new Vector2(220, 10), Color.White); //Desenhamos a pontuação do player 1 e do player 2 spriteBatch.DrawString(Arial, pontosPlayer1_Medium.ToString() + ":" + pontosPlayer2_Medium.ToString(), new Vector2(380, 50), Color.White); //Desenhamos nossa "bola" apesar de ser quadrada..rsrs spriteBatch.Draw(bola_Medium.Textura, bola_Medium.Retangulo, Color.White); //Desenhamos os Player, um de cada lado spriteBatch.Draw(player1.Textura, player1.Retangulo, Color.White); spriteBatch.Draw(player2.Textura, player2.Retangulo, Color.White); break; } case GameState.Hard: { //Posicionamos nossa "rede" no centro da tela spriteBatch.Draw(imgRede, new Vector2((window.ClientBounds.Width * .5f), 0), Color.White); //Desenhar Nome do Jogo graphics.GraphicsDevice.Clear(Color.Black); spriteBatch.DrawString(Arial, "Pong_The_Game_Hard", new Vector2(220, 10), Color.White); //Desenhamos a pontuação do player 1 e do player 2 spriteBatch.DrawString(Arial, pontosPlayer1_Hard.ToString() + ":" + pontosPlayer2_Hard.ToString(), new Vector2(380, 50), Color.White); //Desenhamos nossa "bola" apesar de ser quadrada..rsrs spriteBatch.Draw(bola_Hard.Textura, bola_Hard.Retangulo, Color.White); //Desenhamos os Player, um de cada lado spriteBatch.Draw(player1.Textura, player1.Retangulo, Color.White); spriteBatch.Draw(player2.Textura, player2.Retangulo, Color.White); break; } } break; Se o CurrentGameState estiver no Play não esta nos outros estados... Para que dentro de um caso do switch tornas a fazer o switch com a mesma variável? Edited December 28, 2012 at 01:20 PM by apocsantos geshi
Psycop Posted December 28, 2012 at 03:08 PM Author Report #488805 Posted December 28, 2012 at 03:08 PM Olá O segundo switch seria para o sub_menu... No entanto que estado deveria ter o CurrentGameState? Não entendi bem a estrutura do que indicas. Cumps
Sponsor Posted December 28, 2012 at 03:14 PM Report #488807 Posted December 28, 2012 at 03:14 PM Tens de ter uma variavel que te indica se estas no menu principal, no menu secundário, em jogo. E depois tens de ter outra que te indique, dentro do menu secundário, por exemplo se já escolheu, se voltou para trás...
Psycop Posted December 28, 2012 at 03:21 PM Author Report #488809 Posted December 28, 2012 at 03:21 PM Nesse caso, teria de ter uma 2ª variavel dentro do play certo? onde deve incidir essa variável? cumps
HappyHippyHippo Posted December 28, 2012 at 03:24 PM Report #488810 Posted December 28, 2012 at 03:24 PM só uma pergunta que, apesar seria uma solução para o teu problema, dependendo da tua resposta pode ser "usável" : sabes o que é uma máquina de estados ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
Sponsor Posted December 28, 2012 at 03:36 PM Report #488812 Posted December 28, 2012 at 03:36 PM só uma pergunta que, apesar seria uma solução para o teu problema, dependendo da tua resposta pode ser "usável" : sabes o que é uma máquina de estados ? Isso mesmo. Se der uma leitura sobre isso vai simplificar as ideias...
Psycop Posted December 28, 2012 at 03:42 PM Author Report #488815 Posted December 28, 2012 at 03:42 PM só uma pergunta que, apesar seria uma solução para o teu problema, dependendo da tua resposta pode ser "usável" : sabes o que é uma máquina de estados ? Pois, provavelmente é esse o meu problema. Eu não sei o que é uma máquina de estados. Vou ler algo sobre isso. Recomendas ler algo em especial? Cumps
HappyHippyHippo Posted December 28, 2012 at 03:48 PM Report #488816 Posted December 28, 2012 at 03:48 PM a razão de ter dito que dependendo da tua resposta, é que é um conceito muito genérico. a maior parte da documentação existente online não é direccionada a programação pura porque é algo usado em imensas coisas (principalmente em electrónica) podes dar uma vista de olhos na wikipédia, mas o melhor é fazeres o que estas a fazer, como estás a fazer e depois testar máquinas de estados com pequenas aplicações de teste. IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now