well I just like to program for fun.
c# bug invaders
I've just created the following similar C# Windows Forms project and experimented a bit with the paint function.
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;
namespace Lab35
{
public partial class Form1 : Form
{
Bitmap test_one;
Bitmap test_two;
Bitmap test_three;
int x = 50;
int y = 0;
int x2 = 120;
int y2 = 0;
int x3 = 360;
int y3 = 0;
bool bDir1 = true;
bool bDir2 = true;
bool bDir3 = true;
public Form1()
{
test_one = new Bitmap("test_one.bmp", true); //is 32*32 pixels
test_two = new Bitmap("test_two.bmp", true); //is 128*128 pixels
test_three = new Bitmap("test_three.bmp", true); //is 256*256 pixels
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
timer2.Interval = 20;
timer2.Start();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(this.BackColor);
e.Graphics.DrawImage(test_one, x, y);
e.Graphics.DrawImage(test_two, x2, y2);
e.Graphics.DrawImage(test_three, x3, y3);
//e.Graphics.Dispose(); //If enabled, causes issue where only window frame is displayed.
}
private void timer2_Tick(object sender, EventArgs e)
{
if (bDir1)
y += 5;
else
y -= 5;
if (bDir2)
y2 += 12;
else
y2 -= 12;
if (bDir3)
y3 += 23;
else
y3 -= 23;
if (y <= 0)
bDir1 = true;
if (y >= 200)
bDir1 = false;
if (y2 <= 0)
bDir2 = true;
if (y2 >= 200)
bDir2 = false;
if (y3 <= 0)
bDir3 = true;
if (y3 >= 200)
bDir3 = false;
Invalidate();
}
}
}
I don't get flickering when I set the DoubleBuffered property along with AllPaintingInWmPaint and UserPaint but it does mean I need to remove the call to e.Graphics.Dispose() otherwise I end up with only a window frame being painted.
As I understand, it shouldn't be necessary to call Dispose() on the PaintEventArgs event because we didn't create it. But can anyone comment on whether this is correct or if they're aware of any additional clean up that'd be needed?
well I am able to get a bullet to hit the bmp bug and draw a collision bmp but after I draw a collision bmp I want to erase the bmp collision bmp.
yes, erase the bmp collision bmp. That sounds like fantastic fun-phil. Have you thought about timers to manage an event like that? Maybe you could old school it and fade it out gently but with purpose.
Dev careful. Pixel on board.
how do use a timer to manage the event. I want to draw the collision sprite then draw a black sprite. my code only draws the black sprite.
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(this.BackColor);
e.Graphics.DrawImage(ship, 350 + x, 530);
e.Graphics.DrawImage(bullet, 375 + x, 520 + y);
e.Graphics.DrawImage(bug_one, 350, 0);
if (y <= -500)
{
e.Graphics.DrawImage(coll, 350, 0);
e.Graphics.DrawImage(black, 350, 0);
}
}
3 hours ago, phil67rpg said:... my code only draws the black sprite.
Does it? Are you sure? Or is the black sprite drawing over the coll sprite? Just like the bug_one sprite is there also but you don't see it.
@kseh has code above which makes use of a timer.
Let some frame draws happen with the coll sprite before you draw the black sprite so you can see them both. This will require some sort of logic when the changes happen. If you don't understand timers yet, then you could count frames instead for the trigger.
Dev careful. Pixel on board.
well I think the black sprite is drawing over the collision sprite. also I kind of know how to use timers but not in this instance.
I agree. the black sprite is drawing over the collision sprite, which is currently drawing over the bug_one sprite. Very good.
I would experiment with timers then and see what you come up with. What you want is to decide when to change the sprite image for the behavior or effect you are looking to achieve.
Dev careful. Pixel on board.