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."