+ Reply to Thread
Results 1 to 2 of 2

Thread: C# errors

  1. #1
    Senior Member Zoomy's Avatar
    Join Date
    Feb 2006
    Location
    United Kingdom.
    Posts
    814

    C# errors

    I'm doing a computer game development HNC at my college. I've been working my way through the tutorials but I've hit a block. I did everything the tutorial said to do, but I've run into errors. Can anyone help

    The game that's supposed to be coded is one where two ships shoot each other.

    Quote Originally Posted by My C# Work
    using System;
    using System.Collections.Generic;
    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.Net;
    using Microsoft.Xna.Framework.Storage;

    namespace Starfight
    {
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
    static float aspectRatio;
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    ship p1ship = new ship();
    ship p2ship = new ship();
    plasma p1plasma = new plasma();
    plasma p2plasma = new plasma();
    static Vector3 cameraPosition = new Vector3(0.0f, 250.0f, 500.0f);
    Texture2D bgpic;
    static int leftbound = -15000;
    static int rightbound = 15000;
    static int topbound = 15000;
    static int bottombound = -15000;
    }


    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()
    {
    p1ship.resetposition = new Vector3(5000.0f, 0.0f, 2000.0f);
    p2ship.resetposition = new Vector3(-5000.0f, 0.0f, 2000.0f);
    p1ship.reset();
    p2ship.reset();
    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()
    {
    spriteBatch = new SpriteBatch(GraphicsDevice);
    bgpic = Content.Load<Texture2D>("Textures\\background");
    p1ship.model = Content.Load<Model>("Models\\p1_wedge");
    p2ship.model = Content.Load<Model>("Models\\p2_pencil");
    p1plasma.model = Content.Load<Model>("Models\\bfg_proj");
    p2plasma.model = Content.Load<Model>("Models\\bfg_proj");
    aspectRatio = (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height;
    }


    /// <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)
    {
    if (Keyboard.GetState().IsKeyDown(Keys.Escape) == true) this.Exit();
    if (Keyboard.GetState().IsKeyDown(Keys.Left) == true) p1ship.rotate(1, gameTime);
    if (Keyboard.GetState().IsKeyDown(Keys.Right) == true) p1ship.rotate(2, gameTime);
    if (Keyboard.GetState().IsKeyDown(Keys.Up) == true) p1ship.thrust(gameTime);
    if (Keyboard.GetState().IsKeyDown(Keys.Down) == true) p1plasma.launch(p1ship);

    if (Keyboard.GetState().IsKeyDown(Keys.A) == true) p2ship.rotate(1, gameTime);
    if (Keyboard.GetState().IsKeyDown(Keys.D) == true) p2ship.rotate(2, gameTime);
    if (Keyboard.GetState().IsKeyDown(Keys.W) == true) p2ship.thrust(gameTime);
    if (Keyboard.GetState().IsKeyDown(Keys.S) == true) p2plasma.launch(p2ship);

    p1ship.Update(gameTime);
    p2ship.Update(gameTime);
    p1plasma.Update(gameTime);
    p2plasma.Update(gameTime);

    base.Update(gameTime);
    }

    /// <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)
    {
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue );

    //plasma class

    public class plasma
    {
    public Model model;
    public Vector3 position = Vector3.Zero;
    public Vector3 speed = Vector3.Zero;
    public float rotation = 0.0f;
    public int time;
    public float scale = 0.025f;
    public bool visible = false;

    public void launch(ship shooting)
    {
    position = shooting.position;
    rotation = shooting.rotation;
    speed.X = shooting.speed.X + (float)Math.Cos((rotation + Math.PI / 2)) * 25.0f;
    speed.Z = shooting.speed.Z - (float)Math.Sin((rotation + Math.PI / 2)) * 25.0f;
    time = 1500;
    visible = true;
    }

    public void Update(GameTime gametime)
    {
    position.X += speed.X * (float)gametime.ElapsedGameTime.TotalMilliseconds;
    position.Y += speed.Y * (float)gametime.ElapsedGameTime.TotalMilliseconds;
    position.Z += speed.Z * (float)gametime.ElapsedGameTime.TotalMilliseconds;
    if (position.X < leftbound) position.X = rightbound;
    if (position.X > rightbound) position.X = leftbound;
    if (position.Z < bottombound) position.Z = topbound;
    if (position.Z > topbound) position.Z = bottombound;
    time -= (int)gametime.ElapsedGameTime.TotalMilliseconds;
    if (time <= 0) visible = false;
    }

    public void Draw()
    {
    if (visible == true)
    {
    Matrix[] transforms = new Matrix[model.Bones.Count];
    model.CopyAbsoluteBoneTransformsTo(transforms);

    foreach (ModelMesh mesh in model.Meshes)
    {
    foreach (BasicEffect effect in mesh.Effects)
    {
    effect.EnableDefaultLighting();
    effect.World = transforms[mesh.ParentBone.Index];
    effect.World *= Matrix.CreateRotationY(rotation);
    effect.World *= Matrix.CreateTranslation(position);
    effect.World *= Matrix.CreateScale(scale);
    effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToR adians(45.0f), aspectRatio, 1.0f, 10000.0f);
    }
    mesh.Draw();
    }
    }
    }

    public void checkcollision(ship target)
    {
    if (visible == true)
    {
    BoundingSphere plasmasphere = new BoundingSphere();
    plasmasphere.Center = position;
    plasmasphere.Radius = 500;
    BoundingSphere shipsphere = new BoundingSphere();
    shipsphere.Center = target.position;
    shipsphere.Radius = 500;

    if (plasmasphere.Intersects(shipsphere))
    {
    visible = false;
    target.reset();
    }
    }
    }

    }

    //ship class

    public class ship
    {
    public Model model;
    public Vector3 position = Vector3.Zero;
    public Vector3 resetposition = Vector3.Zero;
    public Vector3 speed = Vector3.Zero;
    public float rotation = 0.0f;
    public float handling = 0.35f;
    public float acceleration = 0.01f;
    public float scale = 0.025f;

    public void Draw()
    {
    Matrix[] transforms = new Matrix[model.Bones.Count];
    model.CopyAbsoluteBoneTransformsTo(transforms);

    foreach (ModelMesh mesh in model.Meshes)
    {
    foreach (BasicEffect effect in mesh.Effects)
    {
    effect.EnableDefaultLighting();
    effect.World = transforms[mesh.ParentBone.Index];
    effect.World *= Matrix.CreateRotationY(rotation);
    effect.World *= Matrix.CreateTranslation(position);
    effect.World *= Matrix.CreateScale(scale);
    effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
    effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToR adians(45.0f), aspectRatio, 1.0f, 10000.0f);
    }
    mesh.Draw();
    }
    }

    public void thrust(GameTime gametime)
    {
    speed.X += (float)Math.Cos((rotation + Math.PI / 2)) * acceleration * (float)gametime.ElapsedGameTime.TotalMilliseconds;
    speed.Z -= (float)Math.Sin((rotation + Math.PI / 2)) * acceleration * (float)gametime.ElapsedGameTime.TotalMilliseconds;
    }

    public void rotate(int direction, GameTime gametime)
    {
    if (direction == 1) rotation += (float)gametime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(handling);
    else rotation -= (float)gametime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(handling);
    }

    public void Update(GameTime gametime)
    {
    position.X += speed.X * (float)gametime.ElapsedGameTime.TotalMilliseconds;
    position.Y += speed.Y * (float)gametime.ElapsedGameTime.TotalMilliseconds;
    position.Z += speed.Z * (float)gametime.ElapsedGameTime.TotalMilliseconds;
    if (position.X < leftbound) position.X = rightbound;
    if (position.X > rightbound) position.X = leftbound;
    if (position.Z < bottombound) position.Z = topbound;
    if (position.Z > topbound) position.Z = bottombound;
    }

    public void checkcollision(ship othership)
    {
    BoundingSphere thisshipsphere = new BoundingSphere();
    thisshipsphere.Center = position;
    thisshipsphere.Radius = 500;
    BoundingSphere othershipsphere = new BoundingSphere();
    othershipsphere.Center = othership.position;
    othershipsphere.Radius = 500;

    if (thisshipsphere.Intersects(othershipsphere))
    {
    speed.X = -speed.X;
    speed.Z = -speed.Z;
    }
    }

    public void reset()
    {
    position = resetposition;
    speed = Vector3.Zero;
    }
    }


    base.Draw(gameTime);
    }
    }
    }
    The red errors all read as "Expected class, delegate, enum, interface, or struct" and the green curly bracket has an error of "Type or namespace definition, or end-of-file expected".

    Thanks in advance.

  2. #2
    Senior Member
    Join Date
    May 2003
    Location
    Aus, Gold Coast :)
    Posts
    801
    Functions declared for the class must reside in the class structure. Therefore the constructor function public Game1 must be within the class.

    Code:
    public class Game1
    {
         public Game1()
         {
         }
    }

Quick Reply Quick Reply

If you are already a member, please login above.

What is the color of the sky?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: December 29th, 2008, 03:09 PM
  2. IE 6 Errors
    By nodnerb2 in forum Applications and Operating Systems
    Replies: 8
    Last Post: June 1st, 2004, 04:18 AM
  3. Still getting errors
    By iNeb in forum Suggestion Box
    Replies: 16
    Last Post: April 4th, 2003, 02:52 PM
  4. Errors??
    By Brainchild in forum IMO Community
    Replies: 7
    Last Post: April 2nd, 2003, 09:07 PM
  5. IE 6 errors
    By MDS in forum Applications and Operating Systems
    Replies: 7
    Last Post: February 15th, 2002, 07:52 PM

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright 2014 All Enthusiast, Inc