WinForm制作一个会跳跃的像素鸟,很灵活的跳起

WinForm制作一个会跳跃的像素鸟,很灵活的跳起

首页休闲益智像素鸟小游戏更新时间:2024-04-28

按空格键跳起动作:

private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == ' ' && bird.Top >= 0) // 空格键跳起 { jumpSpeed = -10; } }小鸟跳跃与检测碰撞;

private void Timer_Tick(object sender, EventArgs e) { // 小鸟跳跃 bird.Top = jumpSpeed; jumpSpeed ; // 移动障碍物并检测碰撞 foreach (PictureBox obstacle in obstacles) { obstacle.Left = obstacleSpeed; if (obstacle.Right < 0) { obstacles.Remove(obstacle); this.Controls.Remove(obstacle); AddObstacle(); break; } if (bird.Bounds.IntersectsWith(obstacle.Bounds)) { GameOver(); return; } }添加初始障碍物:

private void Form1_Load(object sender, EventArgs e) { // 添加初始障碍物 AddObstacle(); // 启动计时器 timer.Start(); }添加引用方法:

private void AddObstacle() { PictureBox obstacle = new PictureBox(); obstacle.Image = Properties.Resources.aa; obstacle.Size = new Size(50, 200); obstacle.Location = new Point(this.Width, this.Height - obstacle.Height); obstacles.Add(obstacle); this.Controls.Add(obstacle); } private void GameOver() { timer.Stop(); MessageBox.Show("Game Over!"); }声明变量与初始化:

private PictureBox bird; private Timer timer; private List<PictureBox> obstacles; private Random random; private int jumpSpeed; private int obstacleSpeed; public Form1() { InitializeComponent(); // 初始化小鸟和计时器 bird = new PictureBox(); bird.Image = Properties.Resources._0; bird.Size = new Size(50, 50); bird.Location = new Point(0, 0); this.Controls.Add(bird); timer = new Timer(); timer.Interval = 30; timer.Tick = Timer_Tick; // 初始化障碍物列表和随机数生成器 obstacles = new List<PictureBox>(2 ); random = new Random(); // 初始化跳跃速度和障碍物速度 jumpSpeed = -10; obstacleSpeed = -5; }

查看全文
大家还看了
也许喜欢
更多游戏

Copyright © 2024 妖气游戏网 www.17u1u.com All Rights Reserved