|
You could also use a PictureBox and move that around (vary it's location), but that uaully doesn't give the smoothests of animations.
The forms paint-event is described on MSDN[^], as well as the event[^] that's raised when the user presses a key.
Bastard Programmer from Hell
|
|
|
|
|
Hi,
I read about paint and managed to create the moving objects representing raindrops. But the problem i have now is of making these raindrops falls forever as long as they they don't come in contact with the player.
Right now they fall vertically from one end the screen and disappear on the other end. How could I create that makes them keep falling?
Thanks
|
|
|
|
|
Check if they exceeded the boundaries of your form, and reset them, possibly to a semi-random[^] location (varying horizontal starting point, vertical always from top of form).
Have you worked on catching user-input too? Simplest way would be by toggeling the KeyPreview[^]-property of the form, and handle the event that comes with it.
Can we expect an article on "writing your own game" when you're done?
Bastard Programmer from Hell
|
|
|
|
|
Hi, Y
You'll definitely get an article from this. Like you said i now stuck on catching the user input. Whats the best way to do this?
|
|
|
|
|
Qobacha wrote: You'll definitely get an article from this.
Cool, that'll help others who are investigating the topic
Qobacha wrote: Like you said i now stuck on catching the user input. Whats the best way to do this?
Toggle the KeyPreview[^] property, and handle the OnPreviewKeyDown[^] event.
Bastard Programmer from Hell
|
|
|
|
|
Hi,
Below is the code from the class that i was testing the movement of my balls with. I think the problem is on the "Move" method. In this example I made the ball to move in the opposite direction whenever it touches the top or bottom walls. But what I really want to do is for the ball start moving from the top again once it had reached the bottom. Any suggestion on the corrections I could make?
class Ball
{
public float x, y, xvel, yvel, radius;
public Brush brush;
public Ball(int gamewidth,int gameHeight,Random r)
{
x = r.Next(gamewidth);
y = r.Next(gameHeight);
xvel = r.Next(2) + 2;
yvel = r.Next(2) + 2;
radius = r.Next(10) + 5;
brush = new SolidBrush(Color.Blue);
}
public void Move(int gameHeight)
{
if (y - radius < 0 || y + radius > gameHeight)
{
yvel = -yvel;
}
y += yvel;
}
public void Draw(Graphics g)
{
g.FillEllipse(brush, new RectangleF(x - radius, y - radius, radius * 2, radius * 2));
}
}
|
|
|
|
|
Qobacha wrote:
if(y - radius < 0 || y + radius > gameHeight)
{
yvel = -yvel;
}
This is where it inverts the direction. Check if y is larger than the screens' height, and if so, set it to 0
Bastard Programmer from Hell
|
|
|
|
|
You were right i have been so stupid. I have set y to 0 and its working. Now the first part done. Starting on user input and collision detection between the user and balls
Thanks
|
|
|
|
|
Small things are easily overlooked. Good luck
Bastard Programmer from Hell
|
|
|
|
|
Its me again. I'm now working on the very last part of the exercise. The program should now detect the collision between the balls and the rectangle. How do i check if the ball and the square are in contact.
|
|
|
|
|
Qobacha wrote: How do i check if the ball and the square are in contact.
Rectangle.IntersectsWith[^] would be an option
Bastard Programmer from Hell
|
|
|
|
|
I have tried Rectangle.IntersectWith but without success. I'm thinking that it may not be applicable in my case because of the way i have written my code. because i get the error saying something like values of x and y are not static.
Can you check if my code given below and see if I can be able to use IntersectWith.
public partial class Form1 : Form
{
bool collided = false;
Player player;
List<Ball> balls;
const int fps = 60;
public Form1()
{
InitializeComponent();
balls = new List<Ball>();
Random r = new Random();
for(int i =0; i<1;i ++)
{
balls.Add(new Ball(Width, Height,r));
}
var task = new Task(Run);
task.Start();
player = new Player()
{
x = this.Width/2,
y = (Height*9/10),
xvel = 10,
brush = Brushes.Black,
};
}
protected void Run()
{
while (true)
{
for(int i = 0; i < balls.Count; i++)
{
balls[i].Move(this.Width);
}
this.Invalidate();
Thread.Sleep(1000 / fps);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.White);
for(int i = 0; i < balls.Count; i++)
{
balls[i].Draw(g);
}
player.DrawPlayer(g);
}
private void CheckCollision(PaintEventArgs e)
{
if (player.IntersectsWith(balls))
{
player.Intersect(balls);
if (!player.IsEmpty)
{
collided = true;
MessageBox.Show("collision detected");
}
}
}
}
public class Player
{
public float x, y, xvel;
public Brush brush;
public Player()
{
}
public void DrawPlayer(Graphics g)
{
g.FillRectangle(brush, new RectangleF(x, y, 30,30));
}
public void MovePlayerLeft(int gameWidth)
{
if (x > 0)
{
x -= xvel;
}
}
public void MovePlayerRight(int gameWidth)
{
if (x < gameWidth-47)
{
x += xvel;
}
}
}
public class Ball
{
public float x, y, yvel, radius;
public Brush brush;
public Ball(int gamewidth,int gameHeight,Random r)
{
x = r.Next(gamewidth);
y = r.Next(gameHeight);
yvel = r.Next(2) + 5;
radius = r.Next(10) + 5;
brush = new SolidBrush(Color.Blue);
}
public void Move(int gameHeight)
{
if (y + radius >= gameHeight)
{
y = 0;
}
y += yvel;
}
public void Draw(Graphics g)
{
g.FillEllipse(brush, new RectangleF(x-radius,y - radius, radius * 2, radius * 2));
}
}
|
|
|
|
|
Maybe the approach i took to develop the game was wrong from the start. Starting the new game using pictures as my objects maybe a better option.
modified 1-May-12 8:58am.
|
|
|
|
|
The IntersectsWith method will only work for a Rectangle , not for "any" class.
Yes, it might be easier to use controls, since they all have a location , a Width and Height . That means that you'll still have to check whether those boundaries intersect.
Bastard Programmer from Hell
|
|
|
|
|
Before I could give up what do you think of this attempt. Making new method to check the position of the ball against the player?
Here is my attempt not sure if it will work though.
private bool collision(Player player)
{
return !(player.x > x + radius
|| player.x + 30 < x
|| player.y > y + radius
|| player.y + 30 < y);
}
namespace MyFirstGameTutorial
{
public class Ball
{
Player square;
public float x, y, yvel, radius;
public Brush brush;
public Ball(int gamewidth,int gameHeight,Random r)
{
x = r.Next(gamewidth);
y = r.Next(gameHeight);
yvel = r.Next(2) + 2;
radius = r.Next(10) + 5;
brush = new SolidBrush(Color.Blue);
}
public void Move(int gameHeight)
{
if (y + radius >= gameHeight)
{
y = 0;
}
y += yvel;
}
public void Draw(Graphics g)
{
RectangleF circle = new RectangleF(x - radius, y - radius, radius * 2, radius * 2);
g.FillEllipse(brush, circle);
}
private bool collision(Player player)
{
return !(player.x > x + radius
|| player.x + 30 < x
|| player.y > y + radius
|| player.y + 30 < y);
}
}
}
|
|
|
|
|
Looks like it could work
Why the +30? (I'm not suggesting it's wrong or anything, just curious)
Bastard Programmer from Hell
|
|
|
|
|
You might want to consider moving over to WPF.
The learning curve is a little steep. However, once you get there, you will find it easier to create animations, styles for images etc.
A Beginner's WPF Animation Tutorial[^] article might help you get started.
In any case, C# offers a wrapper over GDI+ which could help you get started as well - Professional C# - Graphics with GDI+[^].
|
|
|
|
|
It would be easier to write a simple 2D XNA game. The game loop is already set up when creating the project and access to the graphics device and the keyboard are also set up automatically. He would just have to fill in the code to actually read the keyboard and updating the graphics (very simple when using the SpriteBatch) inside the game loop. He would have far less to learn and could get some decent results very quickly.
At least artificial intelligence already is superior to natural stupidity
|
|
|
|
|
Hi,
Unfortunately, I'm not allowed to use any existing game framework.
|
|
|
|
|
Thank
I'll check your links and see if I'll they'll help
|
|
|
|
|
I have a MenuStrip that contains ToolStripMenuItem's with submenus. In a few of the submenus, I have two ToolStripMenuItem's to move up or down a list of options with keyboard shortcuts. For example:
RootMenuItem (e.g. File)
SubItem0 (e.g. New)
SubItem1
SelectPrevious (Ctrl+Left)
SelectNext (Ctrl+Right)
---------------------------
Option 0
Option 1
...
Option N
SubItem2
SelectPrevious (Ctrl+Up)
SelectNext (Ctrl+Down)
--------------------------
Option 0
Option 1
...
Option M Now, all of the "Option N" ToolStripMenuItem's have their CheckOnClick property set to true, and they all have the same event handler. When I click the "SelectPrevious" or "SelectNext" options, everything works fine (Option N is unchecked, and Option (N - 1) or (N + 1) is checked, as appropriate). In addition, if the final option is selected SelectNext is disabled, and the same for SelectPrevious when the first option is selected.
Here is the problem: If I use any of the keyboard shortcuts for SelectPrevious or SelectNext, the next time I hover over the associated SubItem, then hover over a different SubItem, the entire menu is closed.
Here are the steps to reproduce:
1. RootMenuItem->SubItem1->SelectPrevious using the shortcut Ctrl+Left
2. Hover over RootMenuItem->SubItem1 until the submenu appears
3. Move the mouse over RootMenuItem->SubItem0 (or SubItem2)
After step 3, the entire menu closes. The next time I attempt to open the menu (without using a keyboard shortcut in between), everything works fine. Basically, every time I use the keyboard shortcuts any number of consecutive times, the next time I open the submenu that hosts a ToolStripMenuItem that was clicked via a keyboard shortcut, the menu closes and resets to some "good" state that allows it to work correctly the next time.
Has anyone else experienced this? Is there any way to fix it, or is it just a .Net bug? I'm using .Net 4.0 on Windows XP SP3, in case it matters. Thanks,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
I can't say I've seen this exact issue but the mention of shortcut keys and disabled menu items made me recall something similar.
I had a contextmenustrip where items were enabled or disabled dynamically in the Opening event. The shortcut keys were a bit erratic and eventually I discovered my mistake.
What I hadn't understood was that a shortcut calls the item's click event handler directly but only if the item is enabled. The shortcut did NOT trigger the menu Opening event which I was relying on to set the item enabled states correctly.
Is your problem caused by some logical fault related to assumed enable states of menu items?
Alan.
|
|
|
|
|
Thanks for posting a response! However, I don't think that is the issue in this case. The only events I am subscribed to are the click events on leaf items (definition: leaf items are menu items without submenus). All of the buttons in all of my menus are coming out with the appropriate checked and enabled states. The problem is just that something is happening to tweak the menu state when I use the shortcut keys instead of clicking the button through the GUI. None of my code is behaving differently depending on whether the menu items are clicked via shortcut keys, using the keyboard, or with the mouse. This has to be a .Net anomaly, but I was hoping someone else had seen this issue and knew a work-around.
My next step is to comment out each line of code in my click event handler, to see if removing any of the commands makes this work so I can be more specific on what the problem is. Thanks for your time,
Sounds like somebody's got a case of the Mondays
-Jeff
|
|
|
|
|
Hi.
I have a big problem. I need some help with my cheat detector application which I created in C#. This application scan and detect almost all cheat for Counter Strike 1.6, but a cheat it can't be detected with my application because it doesn't modify the size of modules from process hl.exe and it doesn't appear with a strange *.dll
The cheat is called Leis (Silent Aim) and it is just an executable with some files (*.cfg). For the cheat to work , the process hl.exe must be opened and the video renderer of hl must be OpenGL. The cheat is an executable (called leis.exe) which once opened it inject the code into *.dll (i think it is opengl32.dll) and it close. So, it became hard to detect this cheat with my application.
I tried:
* with Checksum (MD5 / CRC32 / Tiger and other) of modules from hl.exe and checksum of process hl.exe - doesn't work, because its the same checksum even though the cheat is activate or not.
* with memory adress - doesn't work.
* with debugger to see the dll necessary for his work: cprytbase.dll , sspicli.dll, sechost.dll , lpk.dll, user32.dll, gdi32.dll, usp10.dll, rpcrt4.dll, advapi32.dll, kernelbase.dll, msvcrt.dll, kernel32.dll, ntdll.dll, imm32.dll, msctf.dll, opengl32.dll, glu32.dll, ddraw.dll , dciman32.dll , setupapy.dll, cfgmgr32.dll, oleaut32.dll, ole32.dll, devobj.dll and user32.dll.
In common with Counter Strike 1.6 it has: ntdll.dll, kernel32.dll, msvcrt.dll, sechost.dll, ole32.dll and opengl32.dll.
Any ideas? Please, I need some help to finish my project
I want to specify that I am still a beginner in C# .
Sorry for my bad english.
Thank you in advance.
modified 19-Apr-12 12:30pm.
|
|
|
|
|
amx_tiger wrote: once opened it inject the code into *.dll
Do a diff between a regular and an infected opengl32 library. Sounds like that's the signature you'd want to detect, not hl.exe.
Bastard Programmer from Hell
|
|
|
|
|