def init(): #Initialize all the constants: screen = constants.initialize() #Making the FPS clock: clock = pygame.time.Clock() FPS = 60 #Creating paddle 1 score: paddle1Score = Score(screen) paddle1Score.x = 100 #Creating paddle 2 score: paddle2Score = Score(screen) paddle2Score.color = constants.colors["RED"] #Making 2 paddles: paddle1 = Paddle() paddle1.x = 10 paddle1.color = constants.colors["BLUE"] paddle2 = Paddle() paddle2.x = 780 paddle2.color = constants.colors["RED"] # Making the ball: ball = Ball() ball.dx = ball.speed ball.dy = ball.speed #The ball starts at the center: ball.x = constants.cx ball.y = constants.cy #The ball's intital color is blue ball.color = constants.colors["PURPLE"] #Creating the title screen's text: title_text = Text(screen) title_text.text = "Welcome to Saabit Pong Game. Difficulty keys: Easy: 1, Medium: 2, Hard: 3" #Creating the end game screen's text endScreen_text = Text(screen) endScreen_text.text = "Game Over. Press 'P' to play again" return ScreenState(screen, title_text, endScreen_text, paddle1, paddle2, ball, paddle1Score, paddle2Score, clock, FPS)
from turtle import Screen from paddle import Paddle from ball import Ball from scoreboard import Scoreboard import time screen = Screen() screen.title("Pong developed by Christian Norton") screen.setup(height=600, width=800) screen.bgcolor("black") screen.tracer(0) right_paddle = Paddle((350, 0)) right_paddle.color("green") left_paddle = Paddle((-350, 0)) left_paddle.color("green") ball = Ball() scoreboard = Scoreboard() screen.listen() screen.onkey(right_paddle.go_up, "Up") screen.onkey(right_paddle.go_down, "Down") screen.onkey(left_paddle.go_up, "w") screen.onkey(left_paddle.go_down, "s") is_on = True while is_on: time.sleep(0.1) screen.update() ball.move()
screen.update() # Paddle misses ball if ball.xcor() >= 570 or ball.xcor() <= -580: collision = True sleep_time = .1 if 270 > ball.ball_heading > 90: ball.reset_ball("left") scoreboard.right_score += 1 else: ball.reset_ball("right") scoreboard.left_score += 1 scoreboard.update_score() if scoreboard.is_game_over(): ball.color("black") net.clear() left_paddle.color("black") right_paddle.color("black") screen.update() scoreboard.declare_winner() game_on = False # Ball bounces off floor or ceiling if ball.ycor() >= 430 or ball.ycor() <= -430: collision = True # Ball is returned by paddle if ball.xcor() > 520 and ball.distance(right_paddle.position()) < 40 \ or ball.xcor() < -520 and ball.distance(left_paddle.position()) < 40: ball_return = True if collision and game_on: ball.bounce_boundary() if ball_return: if sleep_time > .03:
my_screen = Screen() my_screen.title("Welcome to the Pong Game - The Famous Arcade Game") my_screen.bgcolor("black") my_screen.setup(width=900, height=750) my_screen.listen() my_screen.tracer(0) new_board = Board() # Asking user to enter a choice of the game user_choice = my_screen.textinput( title="Select Player GAME-POINT = 20", prompt="Enter your choice ( SINGLEPLAYER / MULTIPLAYER )").lower() r_paddle = Paddle((350, 0)) r_paddle.color("VioletRed") l_paddle = Paddle((-350, 0)) l_paddle.color("yellow") my_screen.onkeypress(fun=r_paddle.go_up, key="Up") my_screen.onkeypress(fun=r_paddle.go_down, key="Down") my_screen.onkeypress(fun=l_paddle.go_up, key="w") my_screen.onkeypress(fun=l_paddle.go_down, key="s") ball = Ball() scoreboard = ScoreBoard() game_is_on = True while game_is_on: time.sleep(ball.ball_speed) my_screen.update()
# paddle two bounce logic if ball.distance(paddle_two) < 50 and ball.xcor() < 280: direction = 1 angle = ball.change_angle_on_paddle_bounce( direction=direction, paddle_position=paddle_two.pos()) # reset game board on win if ball.xcor() > 290: scoreboard.update_score(winner="left") ball.reset() direction = -1 speed /= 1.5 angle = ball.change_angle_on_game_reset(direction=direction) elif ball.xcor() < -290: scoreboard.update_score(winner="right") ball.reset() direction = 1 speed /= 1.5 angle = ball.change_angle_on_game_reset(direction=direction) # end game if scoreboard.paddle_one_score >= 3: scoreboard.game_over(paddle_one.color()) game_on = False elif scoreboard.paddle_two_score >= 3: scoreboard.game_over(paddle_two.color()) game_on = False screen.exitonclick()
from turtle import Screen, colormode from paddle import Paddle from ball import Ball from score_board import ScoreBrd import time colormode(255) s = Screen() s.setup(width=800, height=600) s.bgcolor("Black") s.title("Pong game") s.tracer(0) l_pad = Paddle(a=-350, b=0) l_pad.color(116, 0, 184) r_pad = Paddle(a=350, b=0) r_pad.color(100, 223, 223) b = Ball() sb = ScoreBrd() s.listen() s.onkey(l_pad.paddle_up, "w") s.onkey(l_pad.paddle_down, "s") s.onkey(r_pad.paddle_up, "Up") s.onkey(r_pad.paddle_down, "Down") game_on = True while game_on: time.sleep(b.move_spd) s.update() b.move() if b.ycor() > 280 or b.ycor() < -280: b.bounce_y() if b.distance(r_pad) < 50 and b.xcor() > 330 or b.distance(
from ball import Ball from paddle import Paddle from pong_screen import PongScreen from scoreboard import Score import random import time scr = PongScreen() user_input = scr.screen.textinput(prompt="Enter the number corresponding to one of the following options" + "\n0 - Zero players\n1 - One players\n2 - Two players", title="Number of Players") right_paddle = Paddle((450, 0)) if user_input == "1": right_paddle.color("#f88f01") left_paddle = Paddle((-450, 0)) ball = Ball() left_score = Score((100, 250)) right_score = Score((-100, 250)) scr.screen.update() scr.screen.listen() game_over = False while not game_over: # Move ball ball.move() # if ball touches ceiling, floor or paddles, it will bounce ball.bounce(right_paddle, left_paddle) # if ball gets away from the paddles