chào anh chị và các bạn, mình đang làm đồ án game về game bắn tank…và đang gặp khó khắn về vẽ các khối gạch, bê tông trong game
ý tưởng của mình là:
load file map từ file txt sau đó vẽ lên 1 picturebox.
Nhưng rắc rối là khi du chuyển form chứa picturebox này thì các ảnh vừa được load bị mất hết…
:D:D:D:D
Bạn up code lên đây đi, hoặc send qua mail mình : nguyendauit@gmail.com
Mình sẽ xem rồi góp ý cho.
Hi vọng giúp đc bạn
[MENTION=7355]10520100[/MENTION]
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 Mapbattlecity
{
public partial class Form1 : Form
{
StreamReader reader = new StreamReader(@"g:\map1.txt");
string rows;
int j = 0;
bool left;
bool right;
bool up;
bool dow;
Timer time = new Timer();
// int speed = 0;
Point position = new Point(520 / 2 - 80, 520 - 40);
Graphics graphics;
Image picgach = Image.FromFile(@"G:\Img\Map\1.bmp");
Image picduong = Image.FromFile(@"G:\Img\Map\0.bmp");
Image picbetong = Image.FromFile(@"G:\Img\Map\2.bmp");
Image pictuong = Image.FromFile(@"G:\Img\Map\8.bmp");
Image tankvang = Image.FromFile(@"G:\Img\Defender\201.bmp");
public Form1()
{
InitializeComponent();
graphics = pictureBox1.CreateGraphics();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
up = true;
}
if (e.KeyCode == Keys.Down)
{
dow = true;
}
if (e.KeyCode == Keys.Left)
{
left = true;
}
if (e.KeyCode == Keys.Right)
{
right = true;
}
if (e.KeyCode == Keys.Right && e.KeyCode == Keys.Down)
{
right = false;
dow = false;
}
if (e.KeyCode == Keys.Right && e.KeyCode == Keys.Up)
{
right = false;
up = false;
}
if (e.KeyCode == Keys.Left && e.KeyCode == Keys.Down)
{
left = false;
dow = false;
}
if (e.KeyCode == Keys.Left && e.KeyCode == Keys.Up)
{
left = false;
up = false;
}
}
public void velocity(object o, EventArgs e)
{
graphics.DrawImage(tankvang, position);
if (up)
{
position.Y -= 1;
}
if (dow)
{
position.Y += 1;
}
if (left)
{
position.X -= 1;
}
if (right)
{
position.X += 1;
}
Invalidate();
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
up = false;
}
if (e.KeyCode == Keys.Down)
{
dow = false;
}
if (e.KeyCode == Keys.Left)
{
left = false;
}
if (e.KeyCode == Keys.Right)
{
right = false;
}
}
public void vemap(object o,EventArgs e)
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
while ((rows = reader.ReadLine()) != null)
{
for (int i = 0; i < rows.Length; i++)
{
char k = rows*;
switch (k)
{
case '0':
graphics.DrawImage(picduong, i * 20, j * 20);
break;
case '1':
graphics.DrawImage(picgach, new Point(i * 20, j * 20));
break;
case '2':
graphics.DrawImage(picbetong, new Point(i * 20, j * 20));
break;
case '9':
graphics.DrawImage(pictuong, new Point(i * 20, j * 20));
break;
}
}
j++;
}
}
private void Form1_Load(object sender, EventArgs e)
{
time.Interval = 10;
time.Tick += new EventHandler(velocity);
Timer map = new Timer();
map.Interval = 10;
map.Tick +=new EventHandler(vemap);
time.Start();
map.Start();
}
}
}
![]()
Thứ 1 : giải quyết yêu cầu của em
Trong hàm vemap
em thêm this.pictureBox1.Refresh(); nhé
a k có project nên chưa test đc. Có gì thì nt lại cho a.
Thứ 2 : A góp ý, thay vì em tạo ra 1 picture box để vẽ lên nó.
Thì a nghĩ em chỉ cần tạo 1 bitmap static vs kích thước bằng form game của em !
Mọi thao tác vẽ của em, sẽ vẽ lên bitmap đó.
Sau 1 thời gian nhất định (dùng timmer) em lấy chính bitmap đó vẽ lên Form
gửi anh cái project :baffle::baffle: [MENTION=7355]10520100[/MENTION]
Mapbattlecity.rar (157 KB)
Em send tiếp cho a mấy cái resource kia a mới test đc chứ ?
map, image,v.v…
Hay pm nick skype : daoanhnguyen1992 để nc cũng đc. A connect qua TV sửa cho tiện !