You are on page 1of 5

UNIVERSITY OF THE EAST CALOOCAN

COLLEGE OF ENGINEERING
Computer Engineering Department

FINAL PROJECT

Microprocessor System
NCP 426 – 2CP

Submitted to:

Engr. CABREROS, ARRIANE D.


CpE Professor

Submitted by:

Sibomet, Harold F.
20131200185

April 1, 2019
Code:

import turtle # Pen


import os pen = turtle.Turtle()
pen.speed(0)
wn = turtle.Screen() pen.shape("square")
wn.title("Pong") pen.color("white")
wn.bgcolor("black") pen.penup()
wn.setup(width=800, height=600) pen.hideturtle()
wn.tracer(0) pen.goto(0, 260)
pen.write("Player A: 0 Player B: 0",
# Score align="center", font=("Courier", 24,
score_a = 0 "normal"))
score_b = 0
# Functions
# Paddle A def paddle_a_up():
paddle_a = turtle.Turtle() y = paddle_a.ycor()
paddle_a.speed(0) y += 20
paddle_a.shape("square") paddle_a.sety(y)
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=5,stret def paddle_a_down():
ch_len=1) y = paddle_a.ycor()
paddle_a.penup() y -= 20
paddle_a.goto(-350, 0) paddle_a.sety(y)

# Paddle B def paddle_b_up():


paddle_b = turtle.Turtle() y = paddle_b.ycor()
paddle_b.speed(0) y += 20
paddle_b.shape("square") paddle_b.sety(y)
paddle_b.color("white")
paddle_b.shapesize(stretch_wid=5,stret def paddle_b_down():
ch_len=1) y = paddle_b.ycor()
paddle_b.penup() y -= 20
paddle_b.goto(350, 0) paddle_b.sety(y)

# Ball # Keyboard bindings


ball = turtle.Turtle() wn.listen()
ball.speed(0) wn.onkeypress(paddle_a_up, "w")
ball.shape("square") wn.onkeypress(paddle_a_down, "s")
ball.color("white") wn.onkeypress(paddle_b_up, "Up")
ball.penup() wn.onkeypress(paddle_b_down,
ball.goto(0, 0) "Down")
ball.dx = 2
ball.dy = 2 # Main game loop
while True:
wn.update()
ball.goto(0, 0)
# Move the ball ball.dx *= -1
ball.setx(ball.xcor() + ball.dx)
elif ball.xcor() < -350:
score_b += 1
ball.sety(ball.ycor() + ball.dy) pen.clear()
pen.write("Player A: {} Player B:
# Border checking {}".format(score_a, score_b),
align="center", font=("Courier", 24,
# Top and bottom "normal"))
if ball.ycor() > 290: ball.goto(0, 0)
ball.sety(290) ball.dx *= -1
ball.dy *= -1
os.system("afplay bounce.wav&") # Paddle and ball collisions
if ball.xcor() < -340 and ball.ycor() <
elif ball.ycor() < -290: paddle_a.ycor() + 50 and ball.ycor() >
ball.sety(-290) paddle_a.ycor() - 50:
ball.dy *= -1 ball.dx *= -1
os.system("afplay bounce.wav&") os.system("afplay bounce.wav&")

# Left and right elif ball.xcor() > 340 and ball.ycor() <
if ball.xcor() > 350: paddle_b.ycor() + 50 and ball.ycor() >
score_a += 1 paddle_b.ycor() - 50:
pen.clear() ball.dx *= -1
pen.write("Player A: {} Player B: os.system("afplay bounce.wav&")
{}".format(score_a, score_b),
align="center", font=("Courier", 24,
"normal"))

Output:
Discussion:

In this project we used Thonny Python to build a ping-pong game, we do it by


building it at the top of turtle module. Turtle module lets you do some basic graphics, it’s
great for starting a game, after that we create a window and give it a name “Pong” with
a black background color and a size of 800 width and 600 height pixels. After that we
add a window tracer, the function of this is to stop the window from updating. After that
we create a loop, and every time the loop runs it updates the string.

We named every objects in the game, first is the paddle A. After naming, we set
the speed, shape-size, color and draw line or guideline for our first object. After that we
just applied the same speed, shape-size, color and draw line to our second object which
we named paddle B. Ball is the name of our third object, we also copied the color, but we
input different speed and shape-size. After that we define the functions of each object in
this system.

The only function of paddle A and paddle B is to move up or down. We bind both
paddle A and paddle B to our keyboard. After that we create the loop of the ball, every
time the ball moves, it moves 2 pixels from x to y or y to x, and every time the ball goes
off the screen it goes back in the middle and move diagonally. After that we create a
paddle and ball collision, so that the ball bounces to the paddle A and paddle B. After that
we create a scoring so that we can see what the score is.
Conclusion:

I have learned a lot of things in this project. I have learned how to codes using the
Thonny Python software, and I managed to get familiarized on its system. I also learned
a lot about Raspberry Pi, in its components and each of its functions. We as a group faced
difficulties doing this project, we’ve had a hard time finding which or where in our codes
is wrong and needs to be corrected. Afterall we managed to over come the difficulties by
giving each other a help and cooperation.

We have achieved our objectives to learn the Thonny Python software, to codes
and to get familiarized with the Raspberry Pi’s components and each of its functions. I’m
still amazed knowing that the Raspberry Pi has a small size but it has the capability to run
like a personal computer.

You might also like