小编Zac*_*tén的帖子

使用Timer更新点的图形

在我提出我的问题之前,我对任何不一致表示歉意.我是相当新的.我正在制作一款现在看起来像这样的游戏(图片并不重要): 我的游戏

红点应该向右移动,他们用计时器做到这一点.这很好用.图形不会更新,所以我必须来回拖动窗口的一侧,看看我的点在移动.我该怎么做才能解决这个问题?

我的mainclass中的paintcomponent方法:

    public void paintComponent(Graphics g){
    super.paintComponent(g);
    for (int x = 0; x < SomeInts.amount; x++){
        for (int y = 0; y < SomeInts.amount; y++){
            tile[x][y].colorBody(g, x, y);              
            Tower temp;
            for (int i = 0; i < towers.size(); i++){        
                temp = towers.get(i);
                temp.colorBody(g, tile[x][y].getSize());
                temp.guard.colorBody(g, tile[x][y].getSize());                        
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的红点课.也称为Guard类:

public class Guard {
int x, y, size = 10, towerx, towery;
Timer timer;
public Guard(int towerx1, int towery1){
    towerx = towerx1;
    towery = towery1;
    x = towerx …
Run Code Online (Sandbox Code Playgroud)

java graphics swing timer paintcomponent

4
推荐指数
1
解决办法
178
查看次数

仅来自基类的函数

我正在制作这个游戏,但我遇到了结构问题.我创建了一个名为Structure的类,其他类如Traps,Shelter,Fireplace继承自此类.游戏中的图块有自己的类(Tile),并且在该图块上有结构列表.我可以成功地在列表中包含的tile上构建结构.当我尝试从Traps等类中访问函数时,问题就出现了.它不起作用.我只能使用基类Structure中的函数.

列表中的列表:

class Tile
{
     public List<Structure> Structures = new List<Structure>();
}
Run Code Online (Sandbox Code Playgroud)

我如何建造陷阱或其他建筑物:

        bool anyFireplace = Bundle.map.tile[Bundle.player.X, Bundle.player.Y].Structures.OfType<Shelter>().Any();
        if (!anyFireplace)
        {
            woodlogsCost = 4;
            if (Bundle.player.Woodlogs - woodlogsCost >= 0)
            {
                Bundle.map.tile[Bundle.player.X, Bundle.player.Y].Structures.Add(new Shelter(Bundle.player.X, Bundle.player.Y));
                Bundle.player.Woodlogs -= woodlogsCost;
            }
        }
Run Code Online (Sandbox Code Playgroud)

当我绘制结构时(这是我的问题所在,请注意注释)

foreach (Structure s in Bundle.map.tile[x, y].Structures)
{
   if (s is Fireplace)
{
   //This is the function from base class Strucure
   s.ColorBody(g, 10, x - minx, y - miny, 0, Brushes.Firebrick);

   // The function that I wan´t to use but …
Run Code Online (Sandbox Code Playgroud)

c# winforms

3
推荐指数
1
解决办法
70
查看次数

优化我的国际象棋比赛c#

我第一次在这里问一个问题,如果我做错了,请纠正我.

我国际象棋的图片:http: //img842.imageshack.us/img842/2695/65744343.png

每次我移动一块它都会滞后大约1秒钟.每件和瓷砖都有一个图像,正好有96个图像.每次我移动一块它都会用黑色清除所有内容,然后更新图形.

在国际象棋的早期阶段,我没有任何图像,而是使用了不同的颜色,只有几件没有明显的滞后,这件作品瞬间移动.

        public void updateGraphics(PaintEventArgs e, Graphics g, Bitmap frame)
    {
        g = Graphics.FromImage(frame);
        g.Clear(Color.Black);


        colorMap(g);

        g.Dispose();
        e.Graphics.DrawImageUnscaled(frame, 0, 0);
    }
Run Code Online (Sandbox Code Playgroud)

函数colorMap(g)看起来像这样:

        private void colorMap(Graphics g)
    {
        for (int y = 0; y < SomeInts.amount; y++)
        {
            for (int x = 0; x < SomeInts.amount; x++)
            {
                //Tiles
                Bundle.tile[x, y].colorBody(g, x, y);

                //Pieces
                player1.colorAll(g);
                player2.colorAll(g);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

colorAll函数执行每个colorBody(g)函数,如下所示:

        public void colorBody(Graphics g)
    {
        //base.colorBody() does the following: body = new Rectangle(x * SomeInts.size + …
Run Code Online (Sandbox Code Playgroud)

c# optimization chess lag

0
推荐指数
1
解决办法
646
查看次数

标签 统计

c# ×2

chess ×1

graphics ×1

java ×1

lag ×1

optimization ×1

paintcomponent ×1

swing ×1

timer ×1

winforms ×1