well I am able to draw space ship a bullet and a bug. everything works ok so far but when the bullet moves the screen flickers, is there anyway to optimize my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Interval = 20;
timer1.Start();
}
public void Form1_Load(object sender, EventArgs e)
{
}
int x = 0, y = 0;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Left)
{
x-=5;
if(x<=-350)
{
x = -350;
}
}
if(e.KeyCode == Keys.Right)
{
x+=5;
if(x>=375)
{
x = 375;
}
}
if (e.KeyCode == Keys.Space)
{
y -= 5;
if (y <= -530)
{
y = 0;
}
}
Invalidate();
}
private void timer1_Tick(object sender, EventArgs e)
{
y -= 5;
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Bitmap ship = new Bitmap("ship.bmp", true);
Bitmap bullet = new Bitmap("bullet.bmp", true);
Bitmap bug_one = new Bitmap("bug_one.bmp", true);
Graphics g1 = this.CreateGraphics();
g1.DrawImage(ship, 350+x, 530);
g1.DrawImage(bullet, 375+x, 520 + y);
g1.DrawImage(bug_one, 350, 0);
g1.Dispose();
}
}
}