当前位置:网站首页>Push box games C #
Push box games C #
2022-07-03 06:14:00 【Muyu】
Move the blog you wrote before
Classic game
c# This subject is taught by Mr. Zhao Dawei , And the scoring method is based on this project . And I've always been interested in games , and Visual Studio It's easy to operate , So when you know that you need to do a class ending project , I began to conceive of making a game .
- Using language :c#
- Production tools :·Visual Studio 2010·
# tuixiangzi

The rules of the game : Move my box through keyboard control , And when it collides with other boxes , Move with the hit box . And when you hit the terrain , You can't move , When the target box reaches the designated bottom line , Game over . Ideas : For overall display PictureBox Control to display a picture , Then operate through the keyboard .
Move
- At the time of conception , A game , The first thing I think about is mobile .
In the form program , Show a box that I can control , Then move it , Here I use PictureBox Control for . Changed his background color and name (myBox).

- Then it's time to move the box , While studying , Have studied textBox Of keyDown event , I wonder if I can control the movement of pictures in this way , however PictureBox There was no such incident , So I used a clever method ,
Set up a textBox Control , With the keyboard keyDown Event control textBox The movement of the , And control in mobile events PictureBox Location and textBox In the same position , also ,PictureBox Always in textBox Cover it above .
Here is the source code , Take moving left as an example 
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
// Set the travel distance
int rightLocal = myBox.Width - 1;
int bottomLocal = myBox.Width + 2;
// use switch Sentence to determine which key is pressed by the keyboard
switch (e.KeyCode)
{
case Keys.Left:
// Check for collisions If you don't hit it, you will return true
if (checkLeftCrash(myBox))
{
// Move to the left textBox1
textBox1.Left = textBox1.Left - rightLocal;
// Judge whether it hit the box
if (checkBoxLeftCrash(rightLocal, targetBox1, targetBox2))
{
// Judge whether the target box can be moved , If possible , Move directly
checkLeftMove(rightLocal, targetBox1);
}
else
{
// take textBox1 Adjust the position of
textBox1.Left = textBox1.Left + rightLocal;
break;
}
if (checkBoxLeftCrash(rightLocal, targetBox2, targetBox1))
{
checkLeftMove(rightLocal, targetBox2);
}
else
{
textBox1.Left = textBox1.Left + rightLocal;
break;
}
}
break;
}
myBox.Left = textBox1.Left;
myBox.Top = textBox1.Top;
showBuShu();
showStatus();
checkSuccess();
}
Map settings
- Then , I built a map , The method is clumsy , Set each one one one by one from the design page pictureBox, And then to PictureBox Add a picture of the map in . In fact, a simpler method can be used , From code load Incident ,foreach Traverse add PictureBox.
difficulty
- When setting up the map , I found that in the design view Size value , And in the code width, And height Is different .
for example , Set up size by 50,50 However width and height It will become other strange values . This has an impact on the later collision verification .
Design page add pictureBox The shortcomings of are reflected again , Using code generation will avoid this defect .
collision detection
- Collision detection is divided into three categories
Map edge detection
- The detection of this part is relatively simple , Just test , The four most marginal walls .
Other wall detection
- Inspection of other walls , You need to write the position of the wall one by one . so much trouble , Stupid .
Here is the code
// Take upward as an example
public Boolean checkUpCrash(PictureBox box)
{
Boolean b = true;
// edge
if (box.Top == 40)
{
b = false;
}
// Other walls
if ((box.Top == 80 && box.Left == 259) || (box.Top == 80 && box.Left == 296) || (box.Top == 120 && box.Left == 37) || (box.Top == 120 && box.Left == 111) || (box.Top == 120 && box.Left == 148) || (box.Top == 120 && box.Left == 185))
{
b = false;
}
return b;
}
- After being tortured by this stupid method , I came up with a slightly smarter idea .
If the walls were generated by code , Then you can generate a wall , Just put the position of this wall into an array , When it is necessary to determine the position collision , Traverse the array and then judge .
Target box collision detection
- First judge the movement of my box , Whether the position overlaps with the target box . If it overlaps , You can push this box .
- Next, judge whether the target box moves , Whether it will collide with the wall . If it hits the wall , Then this move will not take effect , Callback the position value .
The sample code
// Here, take the right as an example
// Push the box to the right
public void checkRightMove(int local,PictureBox box)
{
if (myBox.Left + local >= box.Left)
{
// Determine if there is a collision
if (box.Top < myBox.Top || box.Top > myBox.Top || box.Left < myBox.Left)
{
// Do nothing without collision
}
else
{
// The location of the target box is changed to the location of my box + Twice the moving length
if (checkRightCrash(box))
{
// The target box moves
box.Left = myBox.Left + local * 2;
}
else
{
// Position callback
textBox1.Left = textBox1.Left - local;
}
}
}
}
- There's another situation , When the two target boxes are next to each other , At this time, the collision , And it can't move .
Code
// Check the right collision when two boxes are next to each other
public Boolean checkBoxRightCrash(int local,PictureBox Fbox,PictureBox Sbox)
{
Boolean b = false;
// Determine if there is a collision
if (myBox.Left + local != Fbox.Left || myBox.Top!=Fbox.Top)
{
b = true;
}
else
{
if (Fbox.Left + local != Sbox.Left || Fbox.Top != Sbox.Top)
{
b = true;
}
}
return b;
}
Victory judgment
When all the target boxes reach the designated area , Game over , And prompt congratulations to pass .
// Victory judgment
public void checkSuccess()
{
if (((success1.Left == targetBox1.Left && success1.Top == targetBox1.Top) || (success2.Left == targetBox1.Left && success2.Top == targetBox1.Top)) && ((success1.Left == targetBox2.Left && success1.Top == targetBox2.Top) || (success2.Left == targetBox2.Left && success2.Top == targetBox2.Top)))
{
MessageBox.Show(" congratulations , It's over !");
timer1.Stop();
String conStr = "provider=microsoft.jet.oledb.4.0;data source=fx.mdb";
OleDbConnection conn = new OleDbConnection(conStr);
conn.Open();
String sql = "insert into ranklist(username,[time],stepnum) values('"+Login.username+"',"+time+","+bushu+")";
OleDbCommand cmd = new OleDbCommand(sql, conn);
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show(" Record added successfully ");
}
textBox1.Enabled = false;
textBox1.Focus();
}
}
Lack of summary
- The map setting is too troublesome , Can pass foreach simplify .
- Add PictureBox, The details are not accurate enough , It will affect the operation of the game .
- Simplify collision detection by using arrays .
- When writing methods ,Boolean Value setting is chaotic , Some collide with others true, Some collide with others false, It should be unified !
- When detecting multiple boxes next to each other , The writing is too clumsy , More practical methods should be considered .
边栏推荐
- Introduction to software engineering
- Mysql database
- ODL framework project construction trial -demo
- Core principles and source code analysis of disruptor
- 深入解析kubernetes controller-runtime
- Zhiniu stock -- 03
- 智牛股--03
- Kubesphere - build Nacos cluster
- Kubernetes notes (II) pod usage notes
- Es remote cluster configuration and cross cluster search
猜你喜欢

Une exploration intéressante de l'interaction souris - pointeur

Kubernetes notes (I) kubernetes cluster architecture

JDBC connection database steps

Code generator - single table query crud - generator

Simple solution of small up main lottery in station B

Jedis source code analysis (I): jedis introduction, jedis module source code analysis

Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface

Kubernetes notes (IV) kubernetes network

Fluentd facile à utiliser avec le marché des plug - ins rainbond pour une collecte de journaux plus rapide

ThreadLocal的简单理解
随机推荐
Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
Redis cluster creation, capacity expansion and capacity reduction
BeanDefinitionRegistryPostProcessor
Understand the first prediction stage of yolov1
Decision tree of machine learning
Leetcode problem solving summary, constantly updating!
智牛股项目--05
Cesium entity (entities) entity deletion method
Sorry, this user does not exist!
Oauth2.0 - using JWT to replace token and JWT content enhancement
Zhiniu stock project -- 05
Une exploration intéressante de l'interaction souris - pointeur
Luogu problem list: [mathematics 1] basic mathematics problems
The programmer shell with a monthly salary of more than 10000 becomes a grammar skill for secondary school. Do you often use it!!!
Apifix installation
Code generator - single table query crud - generator
致即将毕业大学生的一封信
Jedis source code analysis (II): jediscluster module source code analysis
Get a screenshot of a uiscrollview, including off screen parts
Oracle database synonym creation