当前位置:网站首页>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 .
边栏推荐
- Oauth2.0 - using JWT to replace token and JWT content enhancement
- Why should there be a firewall? This time xiaowai has something to say!!!
- Alibaba cloud OOS file upload
- The programmer shell with a monthly salary of more than 10000 becomes a grammar skill for secondary school. Do you often use it!!!
- Luogu problem list: [mathematics 1] basic mathematics problems
- [system design] proximity service
- 从小数据量 MySQL 迁移数据到 TiDB
- PMP笔记记录
- 从 Amazon Aurora 迁移数据到 TiDB
- Kubernetes notes (10) kubernetes Monitoring & debugging
猜你喜欢
Click cesium to obtain three-dimensional coordinates (longitude, latitude and elevation)
Maximum likelihood estimation, divergence, cross entropy
Kubesphere - set up redis cluster
Svn branch management
Kubernetes notes (VIII) kubernetes security
輕松上手Fluentd,結合 Rainbond 插件市場,日志收集更快捷
Jedis source code analysis (I): jedis introduction, jedis module source code analysis
Zhiniu stock -- 03
多线程与高并发(7)——从ReentrantLock到AQS源码(两万字大章,一篇理解AQS)
Fluentd is easy to use. Combined with the rainbow plug-in market, log collection is faster
随机推荐
PMP notes
Analysis of Clickhouse mergetree principle
深入解析kubernetes controller-runtime
Decision tree of machine learning
Introduction to software engineering
Printer related problem record
Svn branch management
Kubesphere - Multi tenant management
Es remote cluster configuration and cross cluster search
Kubernetes cluster environment construction & Deployment dashboard
Cesium Click to obtain the longitude and latitude elevation coordinates (3D coordinates) of the model surface
Kubernetes notes (IX) kubernetes application encapsulation and expansion
phpstudy设置项目可以由局域网的其他电脑可以访问
Beandefinitionregistrypostprocessor
最大似然估计,散度,交叉熵
Oauth2.0 - use database to store client information and authorization code
Solve the 1251 client does not support authentication protocol error of Navicat for MySQL connection MySQL 8.0.11
Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
BeanDefinitionRegistryPostProcessor
When PHP uses env to obtain file parameters, it gets strings