RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / user-669417

Slava Razrabovich's questions

Martin Hope
Slava Razrabovich
Asked: 2025-03-24 15:11:59 +0000 UTC

graphics.Clear(Color.FromArgb(18, 18, 18)) 错误:参数无效。 C# 图形

  • 5

错误发生在 graphics.Clear(Color.FromArgb(18, 18, 18)) 上,如果删除此行,则在 GridForm KeyDown() 中调用了 MoveRaycastObject() 方法,错误将发生在 DrawScene 的此行:graphics.DrawLine(grid_pen, 1, 1, 1, grid.size_y * grid.size_of_point); (相同的错误)很明显,有某个错误导致了这些错误

形式:

namespace Raycasting
{
    public partial class GridForm : Form
    {
        Scene scene;

        public GridForm()
        {
            InitializeComponent();
        }

        private void GridForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            scene = new Scene(graphics, new Grid());
            scene.DrawScene();
        }

        private void GridForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (keyToIndex.ContainsKey(e.KeyCode))
            {
                if(scene.raycastObjects.Count > keyToIndex[e.KeyCode])
                {
                    scene.SwitchCurrentRaycastObject(keyToIndex[e.KeyCode]);
                }
            }
            int hor = GetAxis(e, Keys.A, Keys.D);
            int ver = GetAxis(e, Keys.W, Keys.S);
            if(hor !=0 || ver != 0) scene.MoveRaycastObject(hor, ver);
        }

        private int GetAxis(KeyEventArgs e,Keys first_direction, Keys second_direction)
        {
            if (e.KeyCode == first_direction) return -1;
            else if (e.KeyCode == second_direction) return 1;
            return 0;
        }

        Dictionary<Keys, int> keyToIndex = new Dictionary<Keys, int>()
        {
            {Keys.D1,0},{Keys.D2,1},{Keys.D3,2},{Keys.D4,3},
            {Keys.D5,4},{Keys.D6,5},{Keys.D7,6},{Keys.D8,7},{Keys.D9,8}
        };
    }
}

场景:

using System.Drawing;

namespace Raycasting
{
    internal class Scene
    {
        public List<RaycastObject> raycastObjects { get; private set; }
        private Graphics graphics;
        private Brush raycastObject_brush;

        private Pen grid_pen;
        private Map map;
        private Grid grid;

        public int currentRaycastObject { get; private set; } = 0;

        public Scene(Graphics graphics, Grid grid, Pen grid_pen = null, Brush raycastObject_brush = null, params RaycastObject[] raycastObjects)
        {
            this.grid = grid;
            this.graphics = graphics;
            this.raycastObjects = (raycastObjects.Length==0) ? new List<RaycastObject>
            {
                new RaycastObject(),
                new RaycastObject(new Point(40, 40))
            } : raycastObjects.ToList();

            this.grid_pen ??= new Pen(Color.FromArgb(245, 193, 5));
            this.raycastObject_brush ??= new SolidBrush(Color.FromArgb(87, 255, 36));

            map = new Map(grid.size_of_point, grid.size_x, grid.size_y);
        }

        public void DrawScene()
        {
            DrawGrid();
            DrawRaycastObjects();
        }

        public void DrawGrid()
        {
            //Additional line , if you have some troubles with monitor
            graphics.DrawLine(grid_pen, 1, 1, 1, grid.size_y * grid.size_of_point);

            for(int i = 0; i <= grid.size_x; i++)
            {
                graphics.DrawLine(grid_pen, 0, i * grid.size_of_point, grid.size_x * grid.size_of_point, i * grid.size_of_point);
            }
            for (int i = 0; i <= grid.size_y; i++)
            {
                graphics.DrawLine(grid_pen, i * grid.size_of_point,0, i * grid.size_of_point, grid.size_y * grid.size_of_point);
            }
        }

        public bool AddRaycastObject(RaycastObject raycastObject)
        {
            if(raycastObjects.Contains(raycastObject))
            {
                return false;
            }
            raycastObjects.Add(raycastObject);
            return true;
        }

        public void DrawRaycastObjects()
        {
            foreach(var ro in raycastObjects)
            {
                DrawRaycastObject(ro);
            }
        }

        public void DrawRaycastObject(RaycastObject raycastObject)
        {
            graphics.FillEllipse(raycastObject_brush, raycastObject.position.X, raycastObject.position.Y, raycastObject.size.X,raycastObject.size.Y);
        }

        public void MoveRaycastObject(int horizontal_axis, int vertical_axis)
        {
            Point current_pos_on_map = new Point(raycastObjects[currentRaycastObject].position.X /map.size_x, raycastObjects[currentRaycastObject].position.Y /map.size_y);
            Point new_pos_on_map = new Point((current_pos_on_map.X/map.size_x) + horizontal_axis, (current_pos_on_map.Y/map.size_y) + vertical_axis);
            new_pos_on_map.X = Math.Clamp(current_pos_on_map.X, 0, map.size_x);
            new_pos_on_map.Y = Math.Clamp(current_pos_on_map.Y, 0, map.size_y);

            map.MoveRaycastObjectOnMap(current_pos_on_map, new_pos_on_map);
            raycastObjects[currentRaycastObject].position = new_pos_on_map;

            graphics.Clear(Color.FromArgb(18, 18, 18));
            DrawScene();
        }

        public void SwitchCurrentRaycastObject(int next_raycastObject)
        {
            currentRaycastObject = next_raycastObject;
        }
    }
    
    internal class Grid
    {
        public int size_of_point { get; private set; }
        public int size_x { get; private set; }
        public int size_y { get; private set; }

        public Grid(int size_of_point = 20,int size_x = 20,int size_y = 20)
        {
            this.size_of_point = size_of_point;
            this.size_x = size_x;
            this.size_y = size_y;
        }
    }
}

地图:

namespace Raycasting
{
    class Map
    {
        public int size_of_point { get; private set; }
        public int size_x { get; private set; }
        public int size_y { get; private set; }

        private char[,] map;

        private char filling_char { get; set; }
        private char block_char { get; set; }
        private char raycast_object_char { get; set; }

        public Map(int size_of_point,int size_x,int size_y,char filling_char = '.',char block_char = '#', char raycast_object_char = 'O')
        {
            this.size_of_point = size_of_point;
            this.size_x = size_x;
            this.size_y = size_y;
            this.filling_char = filling_char;
            this.block_char = block_char;
            this.raycast_object_char = raycast_object_char;
            ClearMap();
        }

        private void ClearMap()
        {
            map = new char[size_x, size_y];
            for (int x =0;x < size_x;x++)
            {
                for (int y = 0; y < size_y; y++)
                {
                    map[x, y] = filling_char;
                }
            }
        }

        public void SetBlock(Point position)
        {
            map[position.X, position.Y] = block_char;
        }

        public void MoveRaycastObjectOnMap(Point formerPos,Point new_position)
        {
            RemoveRaycastObjectFromMap(formerPos);
            SetRaycastObjectOnMap(new_position);
        }
        public void SetRaycastObjectOnMap(Point position)
        {
            map[position.X, position.Y] = raycast_object_char;
        }
        public void RemoveRaycastObjectFromMap(Point position)
        {
            map[position.X, position.Y] = filling_char;
        }
    }
}

射线投射对象:

namespace Raycasting
{
    class RaycastObject
    {
        public Point position { get; set; }
        public Point size { get;private set; }
        public RaycastObject()
        {
            position = new Point(0, 0);
            size = new Point(20, 20);
        }

        public RaycastObject(Point position)
        {
            this.position = position;
            size = new Point(20, 20);
        }

        public RaycastObject(Point position, Point size)
        {
            this.position = position;
            this.size = size;
        }
    }
}
c#
  • 2 个回答
  • 38 Views

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5