WRITE A C CODE FOR THIS PROGRAM We will be recreating the ping pong game in a di
ID: 3857347 • Letter: W
Question
WRITE A C CODE FOR THIS PROGRAM
We will be recreating the ping pong game in a digital form using Raspberry PI 3, LED lights, push buttons, and 7-segment displays. The ping pong ball and the paddles will be represented by LED lights. The LED lights will turn on and off as they move left to right (knight rider) showing the ball’s movement. There will be 5 LED lights used. The first LED light represents player 1 and the fifth LED light represents player 2. When player 1 serves the ball, the first push button will be pushed and the light will turn on from the first LED to the fifth LED. Once it reaches the fifth light, if player 2 pushes the second button on time, then the light must move back from the fifth LED light to the first LED. The process will keep continuing until someone doesn’t push the push button on time. Whoever misses the ball, a point will be earned to the other player. When a point is earned, it will be displayed on the 7 segment display. There will be two 7 segment displays, one for each player. Whoever gets to 9 points first wins the game, and the program stops and resets.
Explanation / Answer
// Store the device width and height
$SCREEN_WIDTH = getScreenWidth()
$SCREEN_HEIGHT = getScreenHeight()
// Load the image
backgroundImg = new Image("bg.jpg")
// Create the ball
ball = new Ball( new Image("ball.png"), $SCREEN_WIDTH / 2, $SCREEN_HEIGHT / 2)
// Create the player
player = new Player(new Image("bat.png"), 30, $SCREEN_HEIGHT / 2)
player.setSpeed( 0, 1.0 )
player.setMaxSpeed( 0, 7.0 )
// Create the AI
cpuBat = new Computer( new Image("cpu_bat.png"), $SCREEN_WIDTH - 30, $SCREEN_HEIGHT / 2)
cpuBat.setSpeed( 0, 0.2 )
cpuBat.setMaxSpeed( 0, 5 )
cpuBat.setSpeed( 0, 0.2 )
cpuBat.setMaxSpeed( 0, 5 )
onEachFrame() do |delta|
// update
cpuBat.update( delta, ball )
player.update( delta )
ball.update( delta, player, cpuBat )
// draw
drawImage( backgroundImg, 0, 0 )
player.draw()
cpuBat.draw()
ball.draw()
drawGUI( player, cpuBat )
end
def drawGUI( player, cpuBat )
drawText( player.getScore(), $SCREEN_WIDTH / 4, $SCREEN_HEIGHT - 50 )
drawText( cpuBat.getScore(), $SCREEN_WIDTH-$SCREEN_WIDTH / 4, $SCREEN_HEIGHT - 50 )
end
class Sprite
def new(img,x, y)
@image = img
@score = 0
@x = x
@y = y
@dx = 0
@dy = 0
@w = img.getWidth()
@h = img.getHeight()
@w2 = @w / 2
@h2 = @h / 2
@speedX = 0
@speedY = 0
@maxXSpeed = 0
@maxYSpeed = 0
end
def setMove( dx, dy )
@dx = dx
@dy = dy
end
def getMoveX()
return @dx
end
def getMoveY()
return @dy
end
def getX()
return @x
end
def getY()
return @y
end
def getWidth()
return @w
end
def getHeight()
return @h
end
def setSpeed( x, y )
@speedX = x
@speedY = y
end
def setMaxSpeed( x, y )
@maxXSpeed = x
@maxYSpeed = y
end
def setXY( x, y )
@x = x
@y = y
end
def getScore()
return @score
end
def incrementScore()
@score = @score + 1
end
def limitVerticalPosition()
if @y < 0
@y = 0
@dy = 0
end
if @y > $SCREEN_HEIGHT
@y = $SCREEN_HEIGHT
@dy = 0
end
end
def updateMove( delta )
@x = @x + @dx*delta
@y = @y + @dy*delta
end
def moveUp()
@dy = @dy - @speedY
if @dy < -@maxYSpeed then
@dy = - @maxYSpeed
end
end
def moveDown()
@dy = @dy + @speedY
if @dy > @maxYSpeed then
@dy = @maxYSpeed
end
end
def slowDown()
@dy = @dy * 0.95
end
def draw()
drawImage( @image, @x, @y, true )
end
end
class Ball < Sprite
def new(img,x, y)
super( img, x, y )
reset()
end
def update( delta )
updateMove( delta )
end
def reset()
setXY( $SCREEN_WIDTH / 2, $SCREEN_HEIGHT / 2 )
setMove( RndMin(-5,5,2), RndMin(-5,5,2) )
end
def update( delta, player, cpu )
if getX() < 0
cpu.incrementScore()
reset()
end
if getX() > $SCREEN_WIDTH
player.incrementScore()
reset()
end
if getY() < 0 || getY() > $SCREEN_HEIGHT
setMove( getMoveX(), - getMoveY() )
setXY( getX(), getY().limit( 0, $SCREEN_HEIGHT ) )
end
updateCollisions( delta, player, cpu )
updateMove( delta )
end
def updateCollisions( delta, player, cpu )
if isRectOverlap(
getX() - getWidth()/2, getY() - getHeight()/2, getWidth(), getHeight(),
player.getX() - player.getWidth()/2,
player.getY() - player.getHeight()/2, player.getWidth(), player.getHeight(), true
) then
setMove( getMoveX().abs() + 0.5*delta, getMoveY() )
else if isRectOverlap(
getX() - getWidth()/2, getY() - getHeight()/2,
getWidth(), getHeight(),
cpu.getX() - cpu.getWidth()/2, cpu.getY() - cpu.getHeight()/2,
cpu.getWidth(), cpu.getHeight(), true
) then
setMove( -(getMoveX().abs() + 0.5*delta), getMoveY() )
end
end
end
class Player < Sprite
def new(img,x, y)
super( img, x, y )
end
def update( delta )
c = getControls()
if c.isKeyDown('up')
moveUp()
else if c.isKeyDown('down')
moveDown()
else
slowDown()
end
limitVerticalPosition()
updateMove( delta )
end
end
class Computer < Sprite
def new(img,x, y)
super( img, x, y )
end
def update( delta, ball )
if ball.getMoveX() > 0 then
if ball.getY() < getY()
moveUp()
end
if ball.getY() > getY()
moveDown()
end
else
slowDown()
end
limitVerticalPosition()
updateMove( delta )
end
end
def RndMin( min, max, minAmount )
rv = rand(min, max)
while rv.abs() < minAmount
rv = rand( -min, max )
end
return rv
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.