Created
May 26, 2025 23:37
-
-
Save jaimezambranachacon/ea41cebb954eeece798c8f88b811c506 to your computer and use it in GitHub Desktop.
Juego Pong con el patron Singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import turtle | |
import time | |
# === Singleton: GameController === | |
class GameController: | |
_instance = None | |
def __new__(cls): | |
if cls._instance is None: | |
cls._instance = super().__new__(cls) | |
cls._instance.__initialized = False | |
return cls._instance | |
def __init__(self): | |
if not self.__initialized: | |
self.screen = turtle.Screen() | |
self.player1 = Paddle(-350, 0) | |
self.player2 = Paddle(350, 0) | |
self.ball = Ball() | |
self.score = Score() | |
self.__initialized = True | |
def start_game(self): | |
self.screen.title("Pong con Singleton") | |
self.screen.bgcolor("black") | |
self.screen.setup(width=800, height=600) | |
self.screen.tracer(0) | |
self._setup_controls() | |
while True: | |
self.screen.update() | |
self.ball.move() | |
self._check_collisions() | |
time.sleep(0.016) # 60 FPS | |
def _setup_controls(self): | |
self.screen.listen() | |
self.screen.onkeypress(self.player1.up, "w") | |
self.screen.onkeypress(self.player1.down, "s") | |
self.screen.onkeypress(self.player2.up, "Up") | |
self.screen.onkeypress(self.player2.down, "Down") | |
def _check_collisions(self): | |
# Lógica de colisiones | |
if self.ball.distance(self.player1) < 50 and self.ball.xcor() < -340: | |
self.ball.bounce_x() | |
if self.ball.distance(self.player2) < 50 and self.ball.xcor() > 340: | |
self.ball.bounce_x() | |
# Puntos | |
if self.ball.xcor() > 390: | |
self.score.player1 += 1 | |
self.ball.reset() | |
elif self.ball.xcor() < -390: | |
self.score.player2 += 1 | |
self.ball.reset() | |
# === Clases del Juego === | |
class Paddle(turtle.Turtle): | |
def __init__(self, x, y): | |
super().__init__() | |
self.speed(0) | |
self.shape("square") | |
self.color("white") | |
self.shapesize(stretch_wid=5, stretch_len=1) | |
self.penup() | |
self.goto(x, y) | |
def up(self): | |
y = self.ycor() | |
if y < 250: | |
self.sety(y + 20) | |
def down(self): | |
y = self.ycor() | |
if y > -240: | |
self.sety(y - 20) | |
class Ball(turtle.Turtle): | |
def __init__(self): | |
super().__init__() | |
self.speed(0) | |
self.shape("circle") | |
self.color("white") | |
self.penup() | |
self.dx = 3 | |
self.dy = 3 | |
def move(self): | |
self.setx(self.xcor() + self.dx) | |
self.sety(self.ycor() + self.dy) | |
# Rebote en bordes | |
if self.ycor() > 290 or self.ycor() < -290: | |
self.bounce_y() | |
def bounce_x(self): | |
self.dx *= -1 | |
def bounce_y(self): | |
self.dy *= -1 | |
def reset(self): | |
self.goto(0, 0) | |
self.dx *= -1 | |
class Score(turtle.Turtle): | |
def __init__(self): | |
super().__init__() | |
self.speed(0) | |
self.color("white") | |
self.penup() | |
self.hideturtle() | |
self.player1 = 0 | |
self.player2 = 0 | |
self.update() | |
def update(self): | |
self.clear() | |
self.goto(0, 260) | |
self.write(f"Jugador 1: {self.player1} Jugador 2: {self.player2}", | |
align="center", font=("Courier", 24, "normal")) | |
# === Inicio del Juego === | |
if __name__ == "__main__": | |
game = GameController() | |
game.start_game() |
Author
jaimezambranachacon
commented
May 26, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment