学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。
可以在线运行,效果如下(在新页面中打开链接):
代码如下(用到平台自定义的xdf库):
from xdf import * fill("space") player1 = box(0, 0, 200, 40, "red") player2 = box(0, 0, 200, 40, "blue") ball = circle(0, 0, 30, "green") txt = text("按空格键开始", 768/2, 1024/2, 100, "red", "center") isPlaying = False player1SpeedX = 0 player1X = 0 player1Y = 0 player2SpeedX = 0 player2X = 0 player2Y = 0 ballX = 0 ballY = 0 ballSpeedX = 0 ballSpeedY = 0 isLeftDown = False isRightDown = False ballR = 30 halfW = 100 halfH = 20 turnN = 0 def reset(): global player1X, player1Y, player2X, player2Y global ballX, ballY, ballSpeedX, ballSpeedY, isPlaying player1X = 768 / 2 player1Y = 20 + 10 player2X = 768 / 2 player2Y = 1024-20-10 ballX = 768/2 ballY = 1024/2 ballSpeedX = random(10) ballSpeedY = -10 txt.show() isPlaying = False def update(): ball.move(ballX, ballY) player1.move(player1X, player1Y) player2.move(player2X, player2Y) reset() update() def keydown(keyCode, key): global isPlaying, isLeftDown, isRightDown, txt if keyCode == 32: if isPlaying == False: isPlaying = True txt.hide() elif keyCode == 37: isLeftDown = True elif keyCode == 39: isRightDown = True def keyup(keyCode, key): global isLeftDown, isRightDown if keyCode == 37: isLeftDown = False elif keyCode == 39: isRightDown = False def updatePlayer2(): global isPlaying, isLeftDown, isRightDown global player2X, player2SpeedX, halfW if isPlaying: if isLeftDown: player2SpeedX -= 5 if isRightDown: player2SpeedX += 5 player2X += player2SpeedX player2SpeedX *= 0.9 if player2X < halfW: player2X = halfW player2SpeedX = 0 elif player2X > 768 - halfW: player2X = 768 - halfW player2SpeedX = 0 def updatePlayer1(): global isPlaying global ballSpeedY, ballX, player1X, player1SpeedX, halfW if isPlaying: if ballSpeedY < 0: a = random(1) if a > 0.5: dx = ballX - player1X if dx > 0: player1SpeedX += 5 elif dx < 0: player1SpeedX -= 5 player1X += player1SpeedX player1SpeedX *= 0.9 if player1X < halfW: player1X = halfW player1SpeedX = 0 elif player1X > 768 - halfW: player1X = 768 - halfW player1SpeedX = 0 def updateBall(): global isPlaying global ballSpeedX, ballSpeedY, ballX, ballY, ballR global player1X, player1Y, player2X, player2Y, player1SpeedX, player2SpeedX global halfW, halfH, turnN if isPlaying: ballX += ballSpeedX ballY += ballSpeedY if ballX <= ballR: ballX = ballR ballSpeedX = -ballSpeedX elif ballX >= 768 - ballR: ballX = 768 - ballR ballSpeedX = -ballSpeedX if ballSpeedY > 0: if ballY + ballR >= player2Y - halfH and ballX > player2X - halfW and ballX < player2X + halfW: ballSpeedY = -ballSpeedY ballSpeedX += player2SpeedX turnN+=1 if turnN >= 10: ballSpeedY *= 1.2 turnN = 0 else: if ballY - ballR <= player1Y + halfH and ballX > player1X - halfW and ballX < player1X + halfW: ballSpeedY = -ballSpeedY ballSpeedX += player1SpeedX if ballY <= player1Y + ballR or ballY >= player2Y-ballR: reset() return def loop(): updateBall() updatePlayer1() updatePlayer2() update()
也可以下载代码,本地python环境运行(用pygame封装了xdf库)。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。