如何让游戏背景滚动起来

wxz280973534 2009-04-19 10:30:44
最近打算做一个飞机游戏练练手
现在遇到一个问题,就是当游戏开始的时候背景必须从上到下不断地滚动,
我的游戏界面是600*700
所以我打算用两张一摸一样的图片A,B(大小也是600*700)作为背景,
原理就是把A设置成背景,当A向下滚动时(这个时候窗口的上面就会有一小段是空的对吧),这时候让B图片也从上到下滚动,来填补这个空缺,
这样当A图片滚完一个周期,即A图从屏幕消失后,B图又可以占据整个窗口,然后接着向下滚动,这时候的A图也就相当于前面的B,紧跟在B的后面.
以此来达到图片滚动的效果.
现在不知道有没有其他比较好的方法可以代替这个方法,感觉这个方法很浪费资源
...全文
406 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
 2010-08-10
  • 打赏
  • 举报
回复
C++ 游戏中 的运动类 能够实现背景的滚动吗?
还有其它方法 帮忙写一下 参考一下下
wxz280973534 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yangqidong 的回复:]
不需要两个图片啊
Graphics.DrawImage有几个重载方法是可以指定绘制图片的哪一部分的
完全可以把图片上半部分绘制在窗体下半部,而把图片 下半部画在窗体上半部
[/Quote]

呵呵,谢谢,昨天断网前,我也想到了,用这个方法不错
  • 打赏
  • 举报
回复
确实可以,可是这句话不懂
using (Graphics g = Graphics.FromImage(img))
{
g.DrawImage(backImage, new Rectangle(0, h, this.Width, this.Height - h), 0, 0, backImage.Width, backImage.Height - h, GraphicsUnit.Pixel);
h += 1;
if (h < this.Height) g.DrawImage(backImage, new Rectangle(0, 0, this.Width, h), 0, backImage.Height - h, backImage.Width, h, GraphicsUnit.Pixel); else h = 0;
}
this.CreateGraphics().DrawImage(img, 0, 0);
可以帮我解释吗?
我的邮箱:czm19881110qq@163.com
谢谢拉
ztmdsbt 2009-04-20
  • 打赏
  • 举报
回复
我也在做飞机游戏...收藏啦~
linamong 2009-04-20
  • 打赏
  • 举报
回复
留名 以后需要相关内容的时候备查
happychou 2009-04-20
  • 打赏
  • 举报
回复
关注。。。
zzxap 2009-04-20
  • 打赏
  • 举报
回复
http://hi.baidu.com/pangula/blog/item/409e01d81c0dc22b10df9b53.html
我很懒 2009-04-20
  • 打赏
  • 举报
回复
重绘背景图片,看看gdi+方面资料吧!
itcrazyman 2009-04-20
  • 打赏
  • 举报
回复
mark up 帮顶
悔说话的哑巴 2009-04-20
  • 打赏
  • 举报
回复
他们都给了例子了,我就帮顶
wzuomin 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 chenqqabcdchenqqabcd 的回复:]
确实可以,可是这句话不懂
using (Graphics g = Graphics.FromImage(img))
{
g.DrawImage(backImage, new Rectangle(0, h, this.Width, this.Height - h), 0, 0, backImage.Width, backImage.Height - h, GraphicsUnit.Pixel);
h += 1;
if (h < this.Height) g.DrawImage(backImage, new Rectangle(0, 0, this.Width, h…
[/Quote]


建议好好看看MSDN上关于 Graphics.DrawImage() 的说明吧
wzuomin 2009-04-19
  • 打赏
  • 举报
回复
新建一个项目,选择vb或者C# Window窗体应用程序,把上面相应语法代码复制过去,运行即可.
wzuomin 2009-04-19
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Bitmap backImage = null;
private Timer tmr = null;
private Int32 h = 0;

public Form1()
{
InitializeComponent();

this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.FormBorderStyle = FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.MinimizeBox = false;

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "bmp,jpg|*.bmp;*.jpg";
if (ofd.ShowDialog() == DialogResult.OK)
{
backImage =(Bitmap ) Image.FromFile(ofd.FileName);
this.Size = backImage.Size;
tmr = new Timer();
tmr.Tick += tmrTick;
tmr.Interval = 10;
tmr.Start();
}
else
{
this.Close();
}
}

private void tmrTick(object sender, EventArgs e)
{
if (backImage != null)
{
using (Bitmap img = new Bitmap(this.Width, this.Height))
{
using (Graphics g = Graphics.FromImage(img))
{
g.DrawImage(backImage, new Rectangle(0, h, this.Width, this.Height - h), 0, 0, backImage.Width, backImage.Height - h, GraphicsUnit.Pixel);
h += 1;
if (h < this.Height) g.DrawImage(backImage, new Rectangle(0, 0, this.Width, h), 0, backImage.Height - h, backImage.Width, h, GraphicsUnit.Pixel); else h = 0;
}
this.CreateGraphics().DrawImage(img, 0, 0);
}
}
}

}
}

wzuomin 2009-04-19
  • 打赏
  • 举报
回复
随便写了段代码,lz参考一下吧.不知道有没有价值,呵呵.

Public Class Form1

Private backImage As Bitmap = Nothing
Private tmr As Timer = Nothing
Private h As Integer

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If tmr IsNot Nothing Then tmr.Stop()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.Fixed3D
Me.MaximizeBox = False
Me.MinimizeBox = False

Dim ofd As New OpenFileDialog
ofd.Filter = "bmp,jpg|*.bmp;*.jpg"
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
backImage = Image.FromFile(ofd.FileName)
Me.Size = backImage.Size
tmr = New Timer
AddHandler tmr.Tick, AddressOf tmrTick
tmr.Interval = 10
tmr.Start()
Else
Me.Close()
End If
End Sub

Private Sub tmrTick(ByVal sender As Object, ByVal e As EventArgs)
If backImage IsNot Nothing Then
Using img As New Bitmap(Me.Width, Me.Height)
Using g As Graphics = Graphics.FromImage(img)
g.DrawImage(backImage, New Rectangle(0, h, Me.Width, Me.Height - h), 0, 0, backImage.Width, backImage.Height - h, GraphicsUnit.Pixel)
h += 1
If h < Me.Height Then g.DrawImage(backImage, New Rectangle(0, 0, Me.Width, h), 0, backImage.Height - h, backImage.Width, h, GraphicsUnit.Pixel) Else h = 0
End Using
Me.CreateGraphics.DrawImage(img, 0, 0)
End Using
End If
End Sub

End Class

wzuomin 2009-04-19
  • 打赏
  • 举报
回复
呵呵,ls所说即是。
teerhu 2009-04-19
  • 打赏
  • 举报
回复
友情帮顶
yangqidong 2009-04-19
  • 打赏
  • 举报
回复
不需要两个图片啊
Graphics.DrawImage有几个重载方法是可以指定绘制图片的哪一部分的
完全可以把图片上半部分绘制在窗体下半部,而把图片 下半部画在窗体上半部

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧