首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何让球被击中时变为蓝色,未命中时变为红色?(Pong)

如何让球被击中时变为蓝色,未命中时变为红色?(Pong)
EN

Stack Overflow用户
提问于 2015-11-23 11:54:10
回答 1查看 46关注 0票数 0

在使用HTML和JS编写的乒乓球游戏中,如何使球在击球时变为蓝色,在未命中时变为红色?

代码语言:javascript
复制
	// Draw Circle
	Main.Context.beginPath();							// start the circle

	// When ball crosses the paddle width, 
	// check to see if paddle intersects path
	if ( (Main.CX-Main.CRAD == 25) && (Main.XINC == -1) ) {

		// if ball hits paddle, change increment (both X & Y)
		if ( (Main.CY>Main.MY) && (Main.CY<(Main.MY+50)) ){

			Main.XINC = Main.XINC * (-1);
			Main.YINC = Main.YINC * (-1);

			Main.HITS = Main.HITS + 1;

		} else Main.MISSES = Main.MISSES + 1;

	}
	
	// If we hit a wall in x coordinate, then change x direction
	if ( (Main.CX < 0+Main.CRAD) || (Main.CX > 600-Main.CRAD)) 
		Main.XINC = Main.XINC * (-1);
	Main.CX = Main.CX + (Main.XINC);	
		
	// If we hit a wall in y coordinate, then change y direction
	if ( (Main.CY < 0+Main.CRAD) || (Main.CY > 600-Main.CRAD)) 
		Main.YINC = Main.YINC * (-1);
	Main.CY = Main.CY + Main.YINC;	

	Main.Context.arc(Main.CX, Main.CY, Main.CRAD, 0, 2 * Math.PI);			// draw the circle
	Main.Context.stroke();								// fill the circle

EN

回答 1

Stack Overflow用户

发布于 2015-11-23 12:23:13

在更改球方向的条件语句中,您将更改颜色。您可以通过更改填充颜色来执行此操作。在改变球的方向之前做这件事会更有意义。

代码语言:javascript
复制
Main.Context.fillStyle="blue";
Main.context.fill();

如果球没有命中,你也会这样做,但用红色代替蓝色。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33863502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档