gpt4 book ai didi

c# - 从动态列表 PictureBox 移动图像

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:32 27 4
gpt4 key购买 nike

感谢 Stack Overflow,我正在开始编程。我正在用c#做的游戏,由几只蜜蜂在桌面上飞来飞去,我必须给它点击,SCORE会增加一定的时间。我做了以下操作:

  • 动态创建列表 PictureBox(在运行时):OK
  • 用这些 GIF 图像随机加载 PictureBox:OK

![蜜蜂游戏][1]

在这部分我卡住了:

  • 随机放置这些 PictureBox(在表单底部)。
  • 使 PictureBox 随机移动(或多或少标记的路线)。
  • 每个 PictureBox 单击事件,更改 PictureBox 图像并隐藏(visible = false),并增加 SCORE + 1。

我需要帮助。我的代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MiPrimerJuego
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int x = 20;
int y = 600;
List<System.Windows.Forms.PictureBox> objeto = new List<PictureBox>();
for (int i = 0; i < 10; i++, x += 90)
{
PictureBox pBox = new PictureBox();
pBox.Height = 80;
pBox.Width = 50;
pBox.Location = new System.Drawing.Point(x, y);
objeto.Add(pBox);
pBox.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pBox);

var rand = new Random();
var files = Directory.GetFiles(Application.StartupPath + @"/Images", "*.gif");
pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
}
}
}
}

最佳答案

将for循环外的rand和files以及函数/void外的objeto声明为私有(private)成员变量:

    private List<PictureBox> objeto = new List<PictureBox>();

private void button1_Click(object sender, EventArgs e)
{
var files = Directory.GetFiles(Application.StartupPath + @"/Images", "*.gif");
int x = 20;
int y = 600;
var rand = new Random();
for (int i = 0; i < 10; i++)
{
x += 90;
PictureBox pBox = new PictureBox();
pBox.Height = 80;
pBox.Width = 50;
pBox.Location = new System.Drawing.Point(x, y);
objeto.Add(pBox);
pBox.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pBox);

pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
}
}
}

关于c# - 从动态列表 PictureBox 移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25215047/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com