Last active
September 14, 2022 02:13
-
-
Save CoolGuyBraydan/0058c4b636031578696d94ca38306b15 to your computer and use it in GitHub Desktop.
a version of pong i made with python
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
print("Press 'g' to make the ball move.") | |
import turtle | |
import time | |
time.sleep(3) | |
import random | |
import math | |
delay = 0.01 | |
p1_score = 0 | |
p2_score = 0 | |
gamestate = "stop" | |
wn = turtle.Screen() | |
wn.title("Pong") | |
wn.bgcolor("black") | |
wn.setup(width=1200, height=600) | |
wn.tracer(0) | |
pen = turtle.Turtle() | |
pen.speed(0) | |
pen.color("white") | |
pen.penup() | |
pen.setposition(-600, -300) | |
pen.pendown() | |
pen.pensize(3) | |
for side in range(2): | |
pen.fd(1200) | |
pen.lt(90) | |
pen.fd(600) | |
pen.lt(90) | |
pen.penup() | |
pen.ht() | |
pen.goto(0,250) | |
pen.pendown() | |
pen.write("Player 1 Score: 0 Player 2 Score: 0", align="center", | |
font=("candara", 20, "bold")) | |
pen.penup() | |
p1 = turtle.Turtle() | |
p1.shape("square") | |
p1.color("white") | |
p1.shapesize(4.5,0.4) | |
p1.penup() | |
p1.goto(-570,0) | |
p1.direction = "p1stop" | |
p2 = turtle.Turtle() | |
p2.shape("square") | |
p2.color("white") | |
p2.shapesize(4.5,0.4) | |
p2.penup() | |
p2.goto(570,0) | |
p2.direction = "p2stop" | |
ball = turtle.Turtle() | |
ball.shape("circle") | |
ball.color("white") | |
ball.shapesize(0.7,0.7) | |
ball.penup() | |
ball.goto(0,0) | |
ball.direction = "bstop" | |
def p1_stop(): | |
p1.direction = "p1stop" | |
def p1u(): | |
p1.direction = "up" | |
def p1d(): | |
p1.direction = "down" | |
def p2_stop(): | |
p2.direction = "p2stop" | |
def p2u(): | |
p2.direction = "up" | |
def p2d(): | |
p2.direction = "down" | |
def bur(): | |
ball.direction = "upright" | |
def bul(): | |
ball.direction = "upleft" | |
def bdr(): | |
ball.direction = "downright" | |
def bdl(): | |
ball.direction = "downleft" | |
def move_p1(): | |
if p1.direction != "p1stop": | |
global gamestate | |
if p1.direction == "up": | |
y = p1.ycor() | |
p1.sety(y+4.5) | |
gamestate = "go" | |
if p1.direction == "down": | |
y = p1.ycor() | |
p1.sety(y-4.5) | |
gamestate = "go" | |
def move_p2(): | |
if p2.direction != "p2stop": | |
global gamestate | |
if p2.direction == "up": | |
y = p2.ycor() | |
p2.sety(y+4.5) | |
gamestate = "go" | |
if p2.direction == "down": | |
y = p2.ycor() | |
p2.sety(y-4.5) | |
gamestate = "go" | |
def start_game(): | |
global gamestate | |
if ball.direction == "bstop": | |
if gamestate == "stop": | |
gamestate = "go" | |
if gamestate == "go": | |
ball.direction = random.choice(['upright', 'upleft', 'downright', 'downleft']) | |
def move_ball(): | |
if ball.direction == "upright": | |
y = ball.ycor()+5 | |
x = ball.xcor()+7 | |
ball.sety(y) | |
ball.setx(x) | |
if ball.direction == "upleft": | |
y = ball.ycor()+5 | |
x = ball.xcor()-7 | |
ball.sety(y) | |
ball.setx(x) | |
if ball.direction == "downright": | |
y = ball.ycor()-5 | |
x = ball.xcor()+7 | |
ball.sety(y) | |
ball.setx(x) | |
if ball.direction == "downleft": | |
y = ball.ycor()-5 | |
x = ball.xcor()-7 | |
ball.sety(y) | |
ball.setx(x) | |
wn.listen() | |
wn.onkeypress(p1u, "w") | |
wn.onkeypress(p1d, "s") | |
wn.onkeypress(p2u, "Up") | |
wn.onkeypress(p2d, "Down") | |
wn.onkeyrelease(p1_stop, "w") | |
wn.onkeyrelease(p1_stop, "s") | |
wn.onkeyrelease(p2_stop, "Up") | |
wn.onkeyrelease(p2_stop, "Down") | |
wn.onkeypress(start_game, "g") | |
while True: | |
wn.update() | |
move_p1() | |
move_p2() | |
move_ball() | |
time.sleep(delay) | |
if gamestate == "stop": | |
ball.direction = "bstop" | |
if ball.ycor() > 294: | |
if ball.direction == "upright": | |
ball.direction = "downright" | |
if ball.direction == "upleft": | |
ball.direction = "downleft" | |
if ball.ycor() < -294: | |
if ball.direction == "downright": | |
ball.direction = "upright" | |
if ball.direction == "downleft": | |
ball.direction = "upleft" | |
if ball.xcor() < -600: | |
ball.ht() | |
ball.setposition(0,0) | |
ball.st() | |
p2_score += 1 | |
pen.clear() | |
pen.penup() | |
pen.setposition(-600, -300) | |
pen.pendown() | |
pen.pensize(3) | |
for side in range(2): | |
pen.fd(1200) | |
pen.lt(90) | |
pen.fd(600) | |
pen.lt(90) | |
pen.penup() | |
pen.ht() | |
pen.goto(0,250) | |
pen.pendown() | |
pen.write("Player 1 Score: {} Player 2 Score: {}".format(p1_score, p2_score), align="center", font=("candara", 20, "bold")) | |
gamestate = "stop" | |
ball.direction = "bstop" | |
if ball.xcor() > 600: | |
ball.ht() | |
ball.setposition(0,0) | |
ball.st() | |
p1_score += 1 | |
pen.clear() | |
pen.penup() | |
pen.setposition(-600, -300) | |
pen.pendown() | |
pen.pensize(3) | |
for side in range(2): | |
pen.fd(1200) | |
pen.lt(90) | |
pen.fd(600) | |
pen.lt(90) | |
pen.penup() | |
pen.ht() | |
pen.goto(0,250) | |
pen.pendown() | |
pen.write("Player 1 Score: {} Player 2 Score: {}".format(p1_score, p2_score), align="center", font=("candara", 20, "bold")) | |
gamestate = "stop" | |
ball.direction = "bstop" | |
if ball.xcor() > 564 and ball.xcor() < 571: | |
a = p2.ycor() + 53 | |
b = p2.ycor() - 53 | |
if ball.ycor() < a and ball.ycor() > b: | |
if ball.direction == "upright": | |
ball.direction = "upleft" | |
if ball.direction == "downright": | |
ball.direction = "downleft" | |
if ball.xcor() < -564 and ball.xcor() > -571: | |
a = p1.ycor() + 53 | |
b = p1.ycor() - 53 | |
if ball.ycor() < a and ball.ycor() > b: | |
if ball.direction == "upleft": | |
ball.direction = "upright" | |
if ball.direction == "downleft": | |
ball.direction = "downright" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment