⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamemain.java

📁 钻头小子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (isSolid(x, pl_y))
	    {
	      	pl_x = old_pl_x;
	      	pl_y = old_pl_y;
	    }
      	/* Die, if pushed off the top: */
        if (pl_y - map_position < 6)
	    {
	      	do_die = 10;
	    }
	
      	/* Move the map: */
      	if (counter % 4 == 0)
      	{
      		map_position += ((level + 1) / 2) % 6;
      	}
      	counter = (counter + 1) % 8;
      	if (map_position >= (MAP_HEIGHT - SCREEN_HEIGHT) * 6)
		{
	  		level++;
	  		resetLevel();
		}
      	/* Handle zombie: */
      	if (counter % 2 == 0)
      	{
	      	if (zombie_alive)
	        {
		  		/* Move zombie: */
		  
		  		old_zombie_x = zombie_x;
		  		old_zombie_y = zombie_y;
		  
		  		if (zombie_dir == DIR_RIGHT)
		  		{
		    		zombie_x += level + 1;
		    	}
		  		else if (zombie_dir == DIR_LEFT)
		  		{
		    		zombie_x -= level + 1;
		    	}
		  		else if (zombie_dir == DIR_DOWN)
		  		{
		    		zombie_y += level + 1;
		    	}
		  		else if (zombie_dir == DIR_UP)
		  		{
		    		zombie_y -= level + 1;
		  		}
		  
		  		/* Keep on the screen! */
	
		  		if (zombie_x < 12)
		    	{
		      		zombie_x = 12;
		      		zombie_dir = DIR_RIGHT;
		    	}
		  		else if (zombie_x > ((MAP_WIDTH * 2) - 3) * 6)
		    	{
		      		zombie_x = ((MAP_WIDTH * 2) - 3) * 6;
		      		zombie_dir = DIR_LEFT;
		    	}
	
		  		if (zombie_y > SCREEN_HEIGHT * 6 + map_position)
		    	{
		      		zombie_y -= 2;
		      		zombie_dir = DIR_UP;
		    	}
	
		  		zombie_bump = false;
	
		  		/* Bump into things: */
	
		  		if (!zombie_mad)
		    	{
		      		x = mirror_x(zombie_x);
		      		if (isSolid(x, zombie_y))
					{
			  			zombie_x = old_zombie_x;
			  			zombie_y = old_zombie_y;
			  			zombie_bump = true;
					}
		    	}
		  		/* Change direction: */
		  		if ((rand(20)) == 0 || zombie_bump)
		    	{
		      		zombie_dir = rand(4);
		    	}
		  
		  		/* Fall off the top of the screen: */
		  		if (zombie_y < map_position)
		  		{
		    		zombie_alive = false;
				}
		  
		  		/* Handle fading in: */
		  		if (zombie_fadein > 0)
		  		{
		    		zombie_fadein--;
		  		}
		  
		  		if (zombie_fadein == 0)
		    	{
			  		/* Get mad! */
			  		if (!zombie_mad &&
			      		rand(100) == 0)
			    	{
			      		zombie_mad = true;
			    	}
			  
			  		/* Kill players: */
			  		if (checkRect(pl_x, pl_y, zombie_x, zombie_y))
			    	{
			      		/* DIE */
			      		do_die = 10;
					}
		    	}
	        }
	      	else
			{
		  		/* Add a new zombie now and then! */
	          	if ((map_position % 40) > 30)
	            {
	              	zombie_alive = true;
		      		zombie_fadein = 15;
		      		zombie_mad = false;
		      		
		      		/* Pick position: */
	
		      		zombie_y = ((map_position / 6) + SCREEN_HEIGHT) * 6;
			  		zombie_x = getEmptySlot(zombie_y);
			  		if (zombie_x == 0)
			  		{
			  			zombie_alive = false;
			  		}
		      		zombie_dir = DIR_UP;
		    	}
			}
		}
     	/* Handle potion: */

      	if (potion_alive)
        {
	  		/* Fall off the top of the screen: */
	  
	  		if (potion_y < map_position)
	  		{
	    		potion_alive = false;
			}
	  
	  		/* Give player potion!: */

	      	if (checkRect(pl_x, pl_y, potion_x, potion_y))
	        {
	          	makebreak += 3;
		  		potion_alive = false;
	    	}

	  		/* Move potion: */

	  		potion_x += potion_xm;
	  
	  		if (map[potion_y / 6][mirror_x(potion_x -
				          (3 * Math.abs(potion_xm))) / 6])
	  		{
	    		potion_x -= potion_xm;
	    		potion_xm = -potion_xm;
	  		}
        }
      	else
      	{
        	/* Add a make-or-break potion now and then! */

        	if (map_position > 30 && ((map_position + 30) % 60) == 29)
        	{
	  			potion_alive = true;

	  			/* Pick position: */

	  			potion_y = ((map_position / 6) + SCREEN_HEIGHT + 1) * 6;
	  			
	      		potion_x = getEmptySlot(potion_y);
	      		if (potion_x == 0)
	      		{
	      			potion_alive = false;
	      		}
	  			potion_xm = 1;
			}
      	}
      
      	if (do_die == 10)
      	{
        	/* Die, or game over? */
	
			if (lives > 0)
			{
				lives--;
			}
			else
			{
	  			done = true;
			}
      	}
	}

	private int getEmptySlot(int y)
	{
		y /= 6;
		int j = 0;
		int i = 2;
		while (i < MAP_WIDTH)
		{
		    if (!map[y][i])
		    {
		    	emptySlot[j] = i;
		    	j++;
		    }
		    i++;
		}
		if (j > 0)
		{
			j = emptySlot[rand(j)];
			if (rand(2) == 0)
			{
				j = Math.abs(MAP_WIDTH * 2 - j - 1);
			}
		}
		return j * 6;
	}
	
	private void draw(Graphics g)
	{
		g.setClip(0, 0, WIDTH, HEIGHT);
		g.setColor(0);
		g.fillRect(0, 0, WIDTH, HEIGHT);
      	/* Draw potion: */
      	if (potion_alive)
        {
        	g.drawImage(imgMakeBreak[counter % 4],
        		potion_x,
        		potion_y - map_position,
        		20);
		}
		
      	/* Draw zombie: */
      	if (zombie_alive)
        {
	    	int index = (counter <= 3) ? 0 : 1;
	  		if (zombie_fadein == 0)
	    	{
	      		if (!zombie_mad)
				{
					g.drawImage(imgZombie[index],
						zombie_x,
						zombie_y - map_position,
						20);
				}
	      		else
				{
					g.drawImage(imgZombie[2 + index],
						zombie_x,
						zombie_y - map_position,
						20);
				}
	    	}
	  		else
	    	{
	      		for (int i = (map_position % 2); i < 6; i = i + 2)
				{
		  			g.setClip(zombie_x, zombie_y + i, 6, 1);
		  			g.drawImage(imgZombie[index],
						zombie_x,
						zombie_y - map_position + i,
						20);
				}
	    	}
		}
      
		g.setClip(0, 0, WIDTH, HEIGHT);
		
      	/* Draw players: */
      	if (do_die == 0)
      	{
      		g.drawImage(imgPlayer[playerImageindex],
				  pl_x,
				  pl_y - map_position,
				  20);
      	}
//

		/* Draw map: */
		int b = (level - 1) % NUM_BRICKS;
      	for (int i = 0; i <= SCREEN_HEIGHT; i++)
		{
			int y = i * 6 - (map_position % 6);
	  		for (int j = 0; j < MAP_WIDTH; j++)
	    	{
	      		if (map[i + (map_position / 6)][j])
				{
					g.drawImage(imgBrick[b], j * 6, y, 20);
					g.drawImage(imgBrick[b], (MAP_WIDTH * 2 - j - 1) * 6, y, 20);
				}
	    	}
		}
		
      	g.fillRect(0, 0, WIDTH, 3);
      	g.fillRect(0, SCREEN_HEIGHT * 6, WIDTH, HEIGHT - SCREEN_HEIGHT * 6);
      	/* Draw lives: */
      	g.setColor(255, 0, 0);
      	for (int i = 1; i <= lives; i++)
		{
			g.fillRect(i * 10, 0, 3, 3);
		}
		
		int y = SCREEN_HEIGHT * 6 + 2;
      	drawNum(g, 25, y, makebreak);
		drawNum(g, WIDTH - 30, y, (map_position * 6) / (MAP_HEIGHT * 6) + ((level - 1) * 6));
    }
    
    private void drawNum(Graphics g, int x, int y, int n)
	{
  		if (n > 99)
  		{
    		n = 99;
    	}
  		else if (n < 0)
  		{
    		n =0;
    	}
    	
  		/* First digit: */
  		g.setClip(x, y, 6, 9);
  		g.drawImage(imgNumbers, x - 6 * (n / 10), y, 20);
  		
  		/* Second digit: */
  		x += 6;
  		g.setClip(x, y, 6, 9);
  		g.drawImage(imgNumbers, x - 6 * (n % 10), y, 20);
	}

    private int mirror_x(int real_x)
	{
  		return (real_x >= MAP_WIDTH * 6) ? 
  			(MAP_WIDTH * 2 - 1) * 6 - real_x : 
  			real_x;
	}
	
	private boolean isSolid(int x, int y)
	{
		if (y < 0 || x < 0)
		{
			return false;
		}
		int x1 = (x + 2) / 6;
		x = (x + 4) / 6;
		int y1 = (y + 2) / 6;
		y = (y + 4) / 6;
		if (x >=  MAP_WIDTH)
		{
			x = MAP_WIDTH - 1;
		}
		if (y >= MAP_HEIGHT)
		{
			y = MAP_HEIGHT - 1;
		}
		if (x1 >=  MAP_WIDTH)
		{
			x1 = MAP_WIDTH - 1;
		}
		if (y1 >= MAP_HEIGHT)
		{
			y1 = MAP_HEIGHT - 1;
		}
		return (map[y][x] || map[y1][x] || map[y][x1] || map[y1][x1]);
	}
	
	private void loadImage()
	{
		try{
			for (int i = 1; i <= 5; i++)
			{
				imgBrick[i - 1] = Image.createImage("/img/brick" + i + ".png");
			}
			for (int i = 1; i <= 4; i++)
			{
				imgZombie[i - 1] = Image.createImage("/img/zombie" + i + ".png");
				imgMakeBreak[i - 1] = Image.createImage("/img/makebreak" + i + ".png");
			}
			imgPlayer[0] = Image.createImage("/img/man1.png");
			imgPlayer[1] = Image.createImage("/img/man2.png");
			imgNumbers = Image.createImage("/img/numbers.png");
		}catch(Exception e){}
	}
	
	private void releaseImage()
	{
		for (int i = 0; i < 5; i++)
		{
			imgBrick[i] = null;
		}
		for (int i = 0; i < 4; i++)
		{
			imgZombie[i] = null;
			imgMakeBreak[i] = null;
		}
		imgPlayer[0] = null;
		imgPlayer[1] = null;
		imgNumbers = null;
	}
	
	private boolean checkRect(int x1, int y1, int x2, int y2)
	{
		return !(x1 < x2 - 6 || x1 > x2 + 6 ||
			y1 < y2 - 6 || y1 > y2 + 6);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -