2013-06-22 3 views
0

좋아요, 그래서 저는 팩맨을 팩맨으로 만들었습니다. 이제 나는 내 몬스터를 게임에 넣으려고합니다.C# 그래픽 개체가 서로 그리기

나는 움직이는 방법과 모든 것을 가진 클래스를 만들었습니다. 이제 문제는 다른 그래픽 객체에 괴물과 팩맨을 그려야한다는 것입니다. 그것들은 모두 내 패널의 크기이고 코드 만 실행하면 팩맨이 나타나지만 괴물은 작동하지만 팩맨이 움직이는 그래픽 개체 아래에 있습니다.

이 괴물과 팩맨 모두 그림의 코드 : 둘은 지금 당장 만 대신 보여 것을, 그들이 같은 그래픽 그리기하게 이의를 제기하거나 방법 또는 :

private void timMove_Tick(object sender, EventArgs e) 
    { 
     Bitmap bitmap = new Bitmap(panBox.Width, panBox.Height); 
     Graphics g = Graphics.FromImage(bitmap); 
     foreach (Monster mon in monsters) 
     { 
      mon.move(g); 
     } 
     foreach (Pacman pac in characters) 
     { 
      pac.move(g); 
     } 
     Graphics g2 = panBox.CreateGraphics(); 
     g2.DrawImage(bitmap, 0, 0);    
    } 

내 질문입니다 팩맨?

+0

오 그래서 내 질문이 무엇인지, 둘 다 그냥 대신 지금 만 팩맨의 보여 그들이 같은 그래픽 그리기 할 객체, 또는하는 중 방법이다. – user2511562

+0

그들은 같은 그래픽을 사용하고있는 것처럼 보입니다 ... 실제 코드입니까? 그렇다면 move()에서 실제로 드로잉이 어떻게 수행됩니까? –

답변

1

일반적으로 클래스 구조에 대해 생각해야한다고 생각합니다. 나는 출발점으로이 같은 것을 사용하는 것이 좋습니다 것입니다 :

abstract class GameObject 
{ 
    public abstract void Move(); 
    public abstract void Draw(Graphics g); 
} 

class Pacman : GameObject 
{ 
    public override void Move() 
    { 
     //Update the Position of Pacman, check for collisions, ... 
    } 

    public override void Draw(Graphics g) 
    { 
     //Draw Pacman at his x and y coordinates 
    } 
} 

class Monster : GameObject 
{ 
    public override void Move() 
    { 
     //Update the Position of the Monster, ... 
    } 

    public override void Draw(Graphics g) 
    { 
     //Draw the Monster at his current position 
    } 
} 

class GameClass 
{ 
    private Pacman _pacman; 
    private Monster _monster; 
    private List<GameObject> _gameobjects = new List<GameObject>(); 

    public GameClass() 
    { 
     _pacman = new Pacman(); 
     _monster = new Monster(); 
     _gameobjects.Add(_pacman); 
     _gameobjects.Add(_monster); 
    } 


    private void TimerTick() 
    { 
     //update all GameObjects 
     foreach (var gameobject in _gameobjects) 
     { 
      gameobject.Move(); 
     } 
     //Draw every single GameObject to the Bitmap 
     Bitmap bitmap = new Bitmap(panBox.Width, panBox.Height); 
     using (Graphics g = Graphics.FromImage(bitmap)) 
     { 
      foreach (var gameobject in _gameobjects) 
      { 
       gameobject.Draw(g); 
      } 
     } 
     //Draw the Bitmap to the screen 
     using (Graphics g = panBox.CreateGraphics()) 
     { 
      g.DrawImage(bitmap, 0, 0); 
     } 
    } 

당신은 일반적으로 상속/Polimorphism을보고 수업을해야합니다. Polimorphism : https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/polymorphism 클래스 : https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/classes