0% found this document useful (0 votes)
21 views9 pages

Pong Game Implementation in Python

The document outlines a Python class for a Pong game displayable in Ren'Py, defining properties such as paddle and ball dimensions, positions, and speeds. It includes methods for rendering the game, handling ball movement and bounces, and detecting collisions with paddles. Additionally, it manages game events, including player interactions and determining the winner, while providing a screen layout for the game interface.

Uploaded by

estebanvj.10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views9 pages

Pong Game Implementation in Python

The document outlines a Python class for a Pong game displayable in Ren'Py, defining properties such as paddle and ball dimensions, positions, and speeds. It includes methods for rendering the game, handling ball movement and bounces, and detecting collisions with paddles. Additionally, it manages game events, including player interactions and determining the winner, while providing a screen layout for the game interface.

Uploaded by

estebanvj.10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

init python:

class PongDisplayable([Link]):

def __init__(self):

[Link].__init__(self)

# The sizes of some of the images.

self.PADDLE_WIDTH = 12

self.PADDLE_HEIGHT = 95

self.PADDLE_X = 240

self.BALL_WIDTH = 15

self.BALL_HEIGHT = 15

self.COURT_TOP = 129

self.COURT_BOTTOM = 650

# Some displayables we use.

[Link] = Solid("#ffffff", xsize=self.PADDLE_WIDTH, ysize=self.PADDLE_HEIGHT)

[Link] = Solid("#ffffff", xsize=self.BALL_WIDTH, ysize=self.BALL_HEIGHT)

# If the ball is stuck to the paddle.

[Link] = True

# The positions of the two paddles.

[Link] = (self.COURT_BOTTOM - self.COURT_TOP) / 2

[Link] = [Link]
# The speed of the computer.

[Link] = 380.0

# The position, delta-position, and the speed of the

# ball.

[Link] = self.PADDLE_X + self.PADDLE_WIDTH + 10

[Link] = [Link]

[Link] = .5

[Link] = .5

[Link] = 350.0

# The time of the past render-frame.

[Link] = None

# The winner.

[Link] = None

def visit(self):

return [ [Link], [Link] ]

# Recomputes the position of the ball, handles bounces, and

# draws the screen.

def render(self, width, height, st, at):

# The Render object we'll be drawing into.

r = [Link](width, height)
# Figure out the time elapsed since the previous frame.

if [Link] is None:

[Link] = st

dtime = st - [Link]

[Link] = st

# Figure out where we want to move the ball to.

speed = dtime * [Link]

oldbx = [Link]

if [Link]:

[Link] = [Link]

else:

[Link] += [Link] * speed

[Link] += [Link] * speed

# Move the computer's paddle. It wants to go to [Link], but

# may be limited by it's speed limit.

cspeed = [Link] * dtime

if abs([Link] - [Link]) <= cspeed:

[Link] = [Link]

else:

[Link] += cspeed * ([Link] - [Link]) / abs([Link] - [Link])

# Handle bounces.
# Bounce off of top.

ball_top = self.COURT_TOP + self.BALL_HEIGHT / 2

if [Link] < ball_top:

[Link] = ball_top + (ball_top - [Link])

[Link] = -[Link]

if not [Link]:

[Link]("pong_beep.opus", channel=0)

# Bounce off bottom.

ball_bot = self.COURT_BOTTOM - self.BALL_HEIGHT / 2

if [Link] > ball_bot:

[Link] = ball_bot - ([Link] - ball_bot)

[Link] = -[Link]

if not [Link]:

[Link]("pong_beep.opus", channel=0)

# This draws a paddle, and checks for bounces.

def paddle(px, py, hotside):

# Render the paddle image. We give it an 800x600 area

# to render into, knowing that images will render smaller.

# (This isn't the case with all displayables. Solid, Frame,

# and Fixed will expand to fill the space allotted.)

# We also pass in st and at.


pi = [Link]([Link], width, height, st, at)

# [Link] returns a Render object, which we can

# blit to the Render we're making.

[Link](pi, (int(px), int(py - self.PADDLE_HEIGHT / 2)))

if py - self.PADDLE_HEIGHT / 2 <= [Link] <= py + self.PADDLE_HEIGHT / 2:

hit = False

if oldbx >= hotside >= [Link]:

[Link] = hotside + (hotside - [Link])

[Link] = -[Link]

hit = True

elif oldbx <= hotside <= [Link]:

[Link] = hotside - ([Link] - hotside)

[Link] = -[Link]

hit = True

if hit:

[Link]("pong_boop.opus", channel=1)

[Link] *= 1.10

# Draw the two paddles.

paddle(self.PADDLE_X, [Link], self.PADDLE_X + self.PADDLE_WIDTH)

paddle(width - self.PADDLE_X - self.PADDLE_WIDTH, [Link], width -


self.PADDLE_X - self.PADDLE_WIDTH)
# Draw the ball.

ball = [Link]([Link], width, height, st, at)

[Link](ball, (int([Link] - self.BALL_WIDTH / 2),

int([Link] - self.BALL_HEIGHT / 2)))

# Check for a winner.

if [Link] < -50:

[Link] = "eileen"

# Needed to ensure that event is called, noticing

# the winner.

[Link](0)

elif [Link] > width + 50:

[Link] = "player"

[Link](0)

# Ask that we be re-rendered ASAP, so we can show the next

# frame.

[Link](self, 0)

# Return the Render object.

return r

# Handles events.

def event(self, ev, x, y, st):


import pygame

# Mousebutton down == start the game by setting stuck to

# false.

if [Link] == [Link] and [Link] == 1:

[Link] = False

# Ensure the pong screen updates.

renpy.restart_interaction()

# Set the position of the player's paddle.

y = max(y, self.COURT_TOP)

y = min(y, self.COURT_BOTTOM)

[Link] = y

# If we have a winner, return him or her. Otherwise, ignore

# the current event.

if [Link]:

return [Link]

else:

raise [Link]()

screen pong():

default pong = PongDisplayable()


add "bg pong field"

add pong

text "Jugador":

xpos 240

xanchor 0.5

ypos 25

size 40

text "Eileen":

xpos (1280 - 240)

xanchor 0.5

ypos 25

size 40

if [Link]:

text "Click para iniciar":

xalign 0.5

ypos 50

size 40

label play_pong:

window hide # Hide the window and quick menu while in pong

$ quick_menu = False
call screen pong

$ quick_menu = True

window show

show eileen vhappy

if _return == "eileen":

e "I win!"

else:

e "You won! Congratulations."

You might also like