Created
September 13, 2022 23:57
-
-
Save CoolGuyBraydan/2b8257eab918f315f8a0391bef68573a to your computer and use it in GitHub Desktop.
1-Player Pong against bot
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(1) | |
import random | |
import math | |
delay = 0.01 | |
score = 0 | |
highscore = 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("Score: 0 High 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 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": | |
if p1.ycor()<247.5: | |
y = p1.ycor() | |
p1.sety(y+4.5) | |
gamestate = "go" | |
if p1.direction == "down": | |
if p1.ycor()>-247.5: | |
y = p1.ycor() | |
p1.sety(y-4.5) | |
gamestate = "go" | |
def move_p2(): | |
global gamestate | |
if ball.ycor()>p2.ycor(): | |
p2.direction="up" | |
if ball.ycor()<p2.ycor(): | |
p2.direction="down" | |
if (ball.direction=="bstop" or ball.xcor()<150 or ball.direction=="upleft" or ball.direction=="downleft") and p2.ycor()<6.5 and p2.ycor()>-6.5: | |
p2.direction="stop" | |
if (ball.direction=="bstop" or ball.direction=="upleft" or ball.direction=="downleft") and p2.ycor()>6: | |
p2.direction="down" | |
if (ball.direction=="bstop" or ball.direction=="upleft" or ball.direction=="downleft") and p2.ycor()<-6: | |
p2.direction="up" | |
if p2.direction != "p2stop": | |
if p2.direction == "up": | |
if p2.ycor()<247.5: | |
y = p2.ycor() | |
p2.sety(y+4.5) | |
gamestate = "go" | |
if p2.direction == "down": | |
if p2.ycor()>-247.5: | |
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.onkeyrelease(p1_stop, "w") | |
wn.onkeyrelease(p1_stop, "s") | |
wn.onkeypress(p1u, "Up") | |
wn.onkeypress(p1d, "Down") | |
wn.onkeyrelease(p1_stop, "Up") | |
wn.onkeyrelease(p1_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() | |
score = 0 | |
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("Score: {} High Score: {}".format(score, highscore), align="center", font=("candara", 20, "bold")) | |
gamestate = "stop" | |
ball.direction = "bstop" | |
if ball.xcor() > 564 and ball.xcor() < 571: | |
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" | |
score += 1 | |
if score>highscore: | |
highscore=score | |
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("Score: {} High Score: {}".format(score, highscore), align="center", font=("candara", 20, "bold")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment