private function checkHit():void { //trace (bubbleArray.length) for (var r:uint=0; r<bubbleArray.length; r++) { for (var c:uint=0; c<TOTALCOL; c++) { if (bubbleArray[r][c]) { if (bullet.hitTestObject(bubbleArray[r][c])) { GameSound.hitSound.play(); _vx = 0; _vy = 0; row = r; col = c; setPosition(); setBullet(); break; return; } } } } }是什么意思
时间: 2024-04-26 16:25:21 浏览: 14
这段代码是用来检测当前子弹是否与泡泡发生了碰撞,如果发生了碰撞,则播放 `GameSound.hitSound` 声音,并暂停子弹的运动。接着记录碰撞的泡泡的位置信息 `(row, col)`,并将子弹的位置移动到该泡泡的中心位置。
然后重新布置子弹,并在 `(row, col)` 位置上消除泡泡。这里通过遍历整个 `bubbleArray` 数组来检测每个泡泡是否与子弹发生碰撞。如果检测到碰撞,则直接退出循环,并返回到上一级调用函数的位置。
相关问题
android打地鼠游戏
好的,这是一个简单的Android打地鼠游戏示例:
1. 首先,在Android Studio中创建一个新的Android项目。
2. 在XML布局文件中添加一个ImageView和若干个Button,分别表示地鼠和打击地鼠的锤子。
3. 在Java代码中,使用Random类生成地鼠出现的位置,并在ImageView中显示地鼠图片。
4. 给锤子Button添加点击事件,当点击时判断锤子的位置是否与地鼠重叠,如果重叠则打中地鼠,否则不做任何操作。
5. 设置一个计时器,每隔一定时间生成一个地鼠,直到游戏结束。
6. 在游戏结束时,显示得分并询问是否重新开始游戏。
下面是示例代码:
activity_main.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/moleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/mole" />
<Button
android:id="@+id/hammerBtn1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginLeft="50dp"
android:layout_marginBottom="50dp"
android:background="@drawable/hammer" />
<Button
android:id="@+id/hammerBtn2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="50dp"
android:background="@drawable/hammer" />
<Button
android:id="@+id/hammerBtn3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="50dp"
android:layout_marginBottom="50dp"
android:background="@drawable/hammer" />
</RelativeLayout>
```
MainActivity.java:
```
public class MainActivity extends AppCompatActivity {
private ImageView moleImage;
private Button hammerBtn1, hammerBtn2, hammerBtn3;
private int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
moleImage = findViewById(R.id.moleImage);
hammerBtn1 = findViewById(R.id.hammerBtn1);
hammerBtn2 = findViewById(R.id.hammerBtn2);
hammerBtn3 = findViewById(R.id.hammerBtn3);
hammerBtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkHit(hammerBtn1);
}
});
hammerBtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkHit(hammerBtn2);
}
});
hammerBtn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkHit(hammerBtn3);
}
});
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
showMole();
}
});
}
}, 0, 2000);
}
private void showMole() {
Random random = new Random();
int x = random.nextInt(getWindowManager().getDefaultDisplay().getWidth() - moleImage.getWidth());
int y = random.nextInt(getWindowManager().getDefaultDisplay().getHeight() - moleImage.getHeight());
moleImage.setX(x);
moleImage.setY(y);
moleImage.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
moleImage.setVisibility(View.GONE);
}
}, 1000);
}
private void checkHit(Button hammerBtn) {
Rect moleRect = new Rect();
moleImage.getHitRect(moleRect);
Rect hammerRect = new Rect();
hammerBtn.getHitRect(hammerRect);
if (Rect.intersects(moleRect, hammerRect)) {
score++;
Toast.makeText(this, "打中了!", Toast.LENGTH_SHORT).show();
moleImage.setVisibility(View.GONE);
}
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("游戏结束");
builder.setMessage("得分:" + score + "\n是否重新开始游戏?");
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
score = 0;
recreate();
}
});
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
}
}
```
这个示例中只有一个地鼠,可以根据需要修改代码以添加更多的地鼠,或者增加游戏难度、增加音效等。
用c++编写一个打飞机小游戏
好的,以下是一个简单的打飞机小游戏的C++代码:
```c++
#include<iostream>
#include<Windows.h>
#include<conio.h>
#include<time.h>
using namespace std;
const int width = 40; //游戏边框宽度
const int height = 20; //游戏边框高度
int x, y; //飞机位置坐标
int ex, ey; //敌机位置坐标
int bulletx, bullety; //子弹位置坐标
int score; //得分
void gotoxy(int x, int y) //光标定位函数
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void drawBorder() //画游戏边框
{
for (int i = 0; i < height; i++) {
gotoxy(0, i);
cout << "|";
gotoxy(width, i);
cout << "|";
}
for (int i = 0; i < width; i++) {
gotoxy(i, 0);
cout << "-";
gotoxy(i, height);
cout << "-";
}
}
void drawPlane() //画飞机
{
gotoxy(x, y);
cout << " /\\ ";
gotoxy(x, y + 1);
cout << " / \\ ";
gotoxy(x, y + 2);
cout << "/____\\";
}
void drawEnemy() //画敌机
{
gotoxy(ex, ey);
cout << " /\\ ";
gotoxy(ex, ey + 1);
cout << " / \\ ";
gotoxy(ex, ey + 2);
cout << "/____\\";
}
void drawBullet() //画子弹
{
gotoxy(bulletx, bullety);
cout << "|";
}
void movePlane() //移动飞机
{
if (_kbhit()) { //判断是否有按键
char key = _getch(); //获取按键
switch (key) {
case 'a': //向左移动
if (x > 1) {
x--;
}
break;
case 'd': //向右移动
if (x < width - 7) {
x++;
}
break;
case 'w': //向上移动
if (y > 1) {
y--;
}
break;
case 's': //向下移动
if (y < height - 3) {
y++;
}
break;
case ' ': //发射子弹
bulletx = x + 2;
bullety = y - 1;
break;
}
}
}
void moveEnemy() //移动敌机
{
ey++;
if (ey >= height - 2) { //敌机飞出屏幕
ey = 1;
ex = rand() % (width - 5) + 1; //重新生成敌机位置
}
}
void moveBullet() //移动子弹
{
if (bullety > 0) {
bullety--;
}
else { //子弹飞出屏幕
bulletx = 0;
bullety = 0;
}
}
void checkHit() //检测碰撞
{
if (bulletx >= ex && bulletx <= ex + 4 && bullety == ey + 2) { //子弹击中敌机
score += 10; //得分加10分
bulletx = 0;
bullety = 0;
ex = rand() % (width - 5) + 1; //重新生成敌机位置
ey = 1;
}
if ((x >= ex && x <= ex + 4 && y >= ey && y <= ey + 2) || (x + 4 >= ex && x + 4 <= ex + 4 && y >= ey && y <= ey + 2)) { //飞机撞上敌机
gotoxy(width / 2 - 4, height / 2);
cout << "Game Over!";
gotoxy(width / 2 - 6, height / 2 + 1);
cout << "Your Score: " << score;
Sleep(3000);
exit(0);
}
}
void showScore() //显示得分
{
gotoxy(width - 10, height + 1);
cout << "Score: " << score;
}
int main()
{
srand((unsigned)time(NULL)); //初始化随机数生成器
x = width / 2 - 3;
y = height - 4;
ex = rand() % (width - 5) + 1;
ey = 1;
score = 0;
while (true) {
system("cls"); //清屏
drawBorder();
drawPlane();
drawEnemy();
drawBullet();
movePlane();
moveEnemy();
moveBullet();
checkHit();
showScore();
Sleep(50); //延时50毫秒
}
return 0;
}
```
该游戏使用了Windows.h库和conio.h库,需要在Windows系统下运行。运行时,玩家可以使用键盘上的“a”、“d”、“w”、“s”来控制飞机的移动,使用空格键来发射子弹,避免被敌机撞击。游戏中,敌机会不断地从屏幕上方飞过来,玩家需要及时发射子弹来击中敌机,得分才能不断增加。若敌机撞到飞机,游戏结束,显示得分。