我正在用c#进行吃豆人游戏,而吃豆人会越过我遇到的所有障碍。
这是我的《吃豆人》游戏的完整代码:
using System;
using System.Media;
using System.Windows.Forms;
namespace Pac_Man {
public partial class Form1 : Form
{
变量:
bool goLeft;
bool goRight;
bool goUp;
bool goDown;
int score = 0;
int totalCoins = 160;
int playerSpeed = 4;
int ghost1 = 4;
int ghost2 = 4;
正在初始化:
public Form1()
{
InitializeComponent();
lblGame.Visible = false;
playSound();
}
播放声音:
private void playSound()
{
SoundPlayer simpleSound = new SoundPlayer(@"C:\Users\rsss-\OneDrive\Documentos\Visual Studio 2019\Projects\Pac_Man\Pac_Man\Sound\pacman_beginning.wav");
simpleSound.PlayLooping();
}
键已按下:
private void keyisdown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
goUp = true;
}
if (e.KeyCode == Keys.Down)
{
goDown = true;
}
if (e.KeyCode == Keys.Left)
{
goLeft = true;
}
if (e.KeyCode == Keys.Right)
{
goRight = true;
}
}
键已打开:
private void keyisup(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
goUp = false;
}
if (e.KeyCode == Keys.Down)
{
goDown = false;
}
if (e.KeyCode == Keys.Left)
{
goLeft = false;
}
if (e.KeyCode == Keys.Right)
{
goRight = false;
}
}
计时器:
private void timer1_Tick(object sender, EventArgs e)
{
int movement = playerSpeed;
if (goLeft)
{
foreach (Control x in this.Controls)
{
if (x is PictureBox && x.Tag == "obstacle" && x.Left == player.Left - playerSpeed && x.Top == player.Top)
{
movement = 0;
break;
}
}
player.Left -= movement;
movement = playerSpeed;
}
else if (goRight)
{
foreach (Control x in this.Controls)
{
if (x is PictureBox && x.Tag == "obstacle" && x.Left == player.Left + playerSpeed && x.Top == player.Top)
{
movement = 0;
break;
}
}
player.Left += movement;
movement = playerSpeed;
}
else if (goUp)
{
foreach (Control x in this.Controls)
{
if (x is PictureBox && x.Tag == "obstacle" && x.Top == player.Top - playerSpeed && x.Left == player.Left)
{
movement = 0;
break;
}
}
player.Top -= movement;
movement = playerSpeed;
}
else if (goDown)
{
foreach (Control x in this.Controls)
{
if (x is PictureBox && x.Tag == "obstacle" && x.Top == player.Top + playerSpeed && x.Left== player.Left)
{
movement = 0;
break;
}
}
player.Top += movement;
movement = playerSpeed;
}
if (player.Left > 641)
{
player.Left = 82;
}
else if (player.Left < 82)
{
player.Left = 641;
}
foreach (Control x in this.Controls)
{
if (x is PictureBox && x.Tag == "ghost")
{
if (((PictureBox)x).Bounds.IntersectsWith(player.Bounds))
{
gameOver();
MessageBox.Show("You Lost!" + "\n" + "Your score is " + score);
this.Close();
}
}
}
鬼魂运动:
redGhost.Left += ghost1;
orangeGhost.Left += ghost2;
blueGhost.Left -= ghost1;
pinkGhost.Left -= ghost2;
foreach (Control c in this.Controls)
{
if (c is PictureBox && c.Tag == "coin")
{
if (player.Bounds.IntersectsWith(c.Bounds))
{
this.Controls.Remove(c);
score++;
}
}
}
lblScore.Text = "Score: " + score;
if (score == totalCoins)
{
gameOver();
MessageBox.Show("You Win!" + "\n" + "You reach the maximum score: " + score);
this.Close();
}
}
游戏结束:
private void gameOver()
{
timer1.Stop();
lblGame.Visible = true;
lblGame.Text = "Game Over!";
}
} }
基本上,我希望角色碰到障碍物时不要朝那个方向移动。 有人可以帮助我吗? 谢谢。
答案 0 :(得分:0)
这是一种测试障碍物击打的不同且希望更简单的方法,我不知道它是否可以编译,但我希望您能理解
首先计算x和y的运动,然后将该运动应用于玩家边界的副本,并检查它是否与任何障碍物相交,是否确实脱离了循环,是否不测试下一个障碍物 如果找不到路口,请移动玩家
需要确保System.Drawing.Rectangle originalBounds = player.Bounds实际上创建了播放器边界的副本,否则播放器将在循环运行时前后移动
methodA() {
Transaction transaction = null;
try {
transaction = sessionFactory.getCurrentSession().beginTransaction();
new B(sessionFactory).methodB();
transaction.rollback();
} catch(Exception ex) {
if (transaction != null) {
transaction.rollback();
}
}
}
class B {
SessionFactory sessionFactory;
methodB() {
Session session = sessionFactory.getCurrentSession();
try {
session.createQuery(...)
.setParameter(...)
.list(); //Throws MySQLNonTransientConnectionException: No operations allowed after connection closed
} catch (Exception e) {
throw e;
}
}
}