pygame pong

Solutions on MaxInterview for pygame pong by the best coders in the world

showing results for - "pygame pong"
Angèle
07 Apr 2017
1#The best python pong game without pygame!
2
3import turtle
4
5a_wins = False
6b_wins = False
7
8# Set up the screen
9turtle.Screen()
10wn = turtle.Screen()
11wn.title("Ping Pong game by Timothy")
12wn.bgcolor("black")
13wn.setup(width=800, height=600)
14wn.tracer(0)
15
16# Score
17score_a = 0
18score_b = 0
19score_lim = 10
20switch = True
21
22# Paddle A
23paddle_a = turtle.Turtle()
24paddle_a.speed(0)
25paddle_a.color("white")
26paddle_a.shape("square")
27paddle_a.penup()
28paddle_a.goto(-350, 0)
29paddle_a.shapesize(stretch_wid=5, stretch_len=1)
30
31# Paddle B
32paddle_b = turtle.Turtle()
33paddle_b.speed(0)
34paddle_b.color("white")
35paddle_b.shape("square")
36paddle_b.penup()
37paddle_b.goto(350, 0)
38paddle_b.shapesize(stretch_wid=5, stretch_len=1)
39
40paddle_a_speed = 20
41paddle_b_speed = 20
42
43# Ball
44ball = turtle.Turtle()
45ball.speed(0)
46ball.shape("circle")
47ball.color("white")
48ball.penup()
49ball.goto(0, 0)
50ball.dy = 0.4
51ball.dx = 0.4
52
53# Pen
54pen = turtle.Turtle()
55pen.speed(0)
56pen.penup()
57pen.color("white")
58pen.hideturtle()
59pen.goto(0, 260)
60pen.write("Player A: 0  Player B: 0", align="center", font=("Courier", 24, "normal"))
61
62# Win
63win = turtle.Turtle()
64win.speed(0)
65win.penup()
66win.color("white")
67win.hideturtle()
68win.goto(0, 0)
69
70
71# Function
72def paddle_a_up():
73    y = paddle_a.ycor()
74    y += paddle_a_speed
75    paddle_a.sety(y)
76
77
78def paddle_a_down():
79    y = paddle_a.ycor()
80    y -= paddle_a_speed
81    paddle_a.sety(y)
82
83
84def paddle_b_up():
85    y = paddle_b.ycor()
86    y += paddle_b_speed
87    paddle_b.sety(y)
88
89
90def paddle_b_down():
91    y = paddle_b.ycor()
92    y -= paddle_b_speed
93    paddle_b.sety(y)
94
95
96turtle.listen()
97turtle.onkey(paddle_a_up, "w")
98turtle.onkey(paddle_a_down, "s")
99turtle.onkey(paddle_b_up, "Up")
100turtle.onkey(paddle_b_down, "Down")
101
102# Main game loop
103while True:
104    wn.update()
105
106    # Move the ball
107    ball.setx(ball.xcor() + ball.dx)
108    ball.sety(ball.ycor() + ball.dy)
109
110    # Border
111    if ball.ycor() > 290:
112        ball.sety(290)
113        ball.dy *= -1
114    elif ball.ycor() < -290:
115        ball.sety(-290)
116        ball.dy *= -1
117    elif ball.xcor() > 390:
118        ball.goto(0, 0)
119        ball.dx *= -1
120        score_a += 1
121    elif ball.xcor() < -390:
122        ball.goto(0, 0)
123        ball.dx *= -1
124        score_b += 1
125
126    # Paddle
127    if 340 < ball.xcor() < 350 and paddle_b.ycor() + 40 > ball.ycor() > paddle_b.ycor() - 40:
128        ball.setx(340)
129        ball.dx *= -1
130    if -340 > ball.xcor() > -350 and paddle_a.ycor() + 40 > ball.ycor() > paddle_a.ycor() - 40:
131        ball.setx(-340)
132        ball.dx *= -1
133
134    pen.clear()
135    pen.write(f"Player A: {score_a}  Player B: {score_b}", align="center", font=("Courier", 24, "normal"))
136
137    if score_a == score_lim:
138        turtle.clearscreen()
139        a_wins = True
140        break
141
142    elif score_b == score_lim:
143        turtle.clearscreen()
144        b_wins = True
145        break
146
147
148while True:
149    if a_wins:
150        wn.bgcolor("black")
151        win.write("Player A wins", align="center", font=("Courier", 50, "normal"))
152    elif b_wins:
153        wn.bgcolor("black")
154        win.write("Player B wins", align="center", font=("Courier", 50, "normal"))
Alex
23 Oct 2018
1import pygame
2
3### Colors
4WHITE = (255, 255, 255)
5BLACK = (0,0,0)
6
7### Constants
8W = 600
9H = 600
10pygame.font.init()
11comic = pygame.font.SysFont('Comic Sans MS', 30)
12
13### Variables
14wt = 10
15mplay = False
16
17p1x = W/30
18p1y = H/2 - ((W/60)**2)/2
19
20p2x = W-(W/30)
21p2y = H/2 - ((W/60)**2)/2
22
23p1score = 0
24p2score = 0
25
26w_p = False
27s_p = False
28wsr = False
29u_p = False
30d_p = False
31udr = False
32
33dm = H/40
34
35paddle_width = W/60
36paddle_height = paddle_width**2
37
38bsd = 1
39
40bx = W/2
41by = H/2
42bw = W/65
43bxv = H/60
44bxv = -bxv
45byv = 0
46
47### Functions
48def drawpaddle(x, y, w, h):
49    pygame.draw.rect(screen, WHITE, (x, y, w, h))
50
51def drawball(x, y):
52    pygame.draw.circle(screen, WHITE, (int(x), int(y)), int(bw))
53
54def uploc():
55    global p1y
56    global p2y
57    if w_p:
58        if p1y-(dm) < 0:
59            py1 = 0
60        else:
61            p1y -= dm
62    elif s_p:
63        if p1y+(dm)+paddle_height > H:
64            p1y = H-paddle_height
65        else:
66            p1y += dm
67    if u_p:
68        if p2y-(dm) < 0:
69            p2y = 0
70        else:
71            p2y -= dm
72    elif d_p:
73        if p2y+(dm)+paddle_height > H:
74            p2y = H-paddle_height
75        else:
76            p2y += dm
77
78def upblnv():
79    global bx
80    global bxv
81    global by
82    global byv
83    global p2score
84    global p1score
85
86    if (bx+bxv < p1x+paddle_width) and ((p1y < by+byv+bw) and (by+byv-bw < p1y+paddle_height)):
87        bxv = -bxv
88        byv = ((p1y+(p1y+paddle_height))/2)-by
89        byv = -byv/((5*bw)/7)
90    elif bx+bxv < 0:
91        p2score += 1
92        bx = W/2
93        bxv = H/60
94        by = H/2
95        byv = 0
96    if (bx+bxv > p2x) and ((p2y < by+byv+bw) and (by+byv-bw < p2y+paddle_height)):
97        bxv = -bxv
98        byv = ((p2y+(p2y+paddle_height))/2)-by
99        byv = -byv/((5*bw)/7)
100    elif bx+bxv > W:
101        p1score += 1
102        bx = W/2
103        bxv = -H/60
104        by = H/2
105        byv = 0
106    if by+byv > H or by+byv < 0:
107        byv = -byv
108
109    bx += bxv
110    by += byv
111
112def drawscore():
113    score = comic.render(str(p1score) + " - " + str(p2score), False, WHITE)
114    screen.blit(score, (W/2,30))
115
116### Initialize
117screen = pygame.display.set_mode((W, H))
118pygame.display.set_caption('Snake ML v.1.0.0')
119screen.fill(BLACK)
120pygame.display.flip()
121
122running = True
123while running:
124    for event in pygame.event.get():
125        if event.type == pygame.QUIT:
126            running = False
127        if event.type == pygame.KEYDOWN:
128            if event.key == pygame.K_ESCAPE:
129                running = False
130            if event.key == pygame.K_w:
131                w_p = True
132                if s_p == True:
133                    s_p = False
134                    wsr = True
135            if event.key == pygame.K_s:
136                s_p = True
137                if w_p == True:
138                    w_p = False
139                    wsr = True
140            if event.key == pygame.K_UP:
141                u_p = True
142                if d_p == True:
143                    d_p = False
144                    udr = True
145            if event.key == pygame.K_DOWN:
146                d_p = True
147                if u_p == True:
148                    u_p = False
149                    udr = True
150        if event.type == pygame.KEYUP:
151            if event.key == pygame.K_w:
152                w_p = False
153                if wsr == True:
154                    s_p = True
155                    wsr = False
156            if event.key == pygame.K_s:
157                s_p = False
158                if wsr == True:
159                    w_p = True
160                    wsr = False
161            if event.key == pygame.K_UP:
162                u_p = False
163                if udr == True:
164                    d_p = True
165                    udr = False
166            if event.key == pygame.K_DOWN:
167                d_p = False
168                if udr == True:
169                    u_p = True
170                    udr = False
171
172    screen.fill(BLACK)
173    uploc()
174    upblnv()
175    drawscore()
176    drawball(bx, by)
177    drawpaddle(p1x, p1y, paddle_width, paddle_height)
178    drawpaddle(p2x, p2y, paddle_width, paddle_height)
179    pygame.display.flip()
180    pygame.time.wait(wt)
queries leading to this page
pong pygame vs computer c3 b9pong in pycharm tutorialpong pythoncreate a pong pong game in pygamepng pygamepython pong gamehow to make a pong game in pythonpong pygame vs iapython console pongpong pygame vs computerhow to make pong in pygamepython pongpython pong modulepygame pong gamepygame pongpygame pong codepong using pygamepong in pygame copy and pastepygame full pong codedoes pygame support pngpong pong in pygamepython pong game codepython pong scriptpong game python pygamehow to create pong game in pygamehow to code pong in pythonpong game using pythonpong pygame examplepygame pong source codepong game python ncodepong game pygame codepython pong pygamepong game in pygamepong with pygamemake pong in pythonmaking pingpong game pythonpong game python codepygame pong examplepong game pythonhow to make pong game in pythonpong code pygamepygame code for pongmake a pong game in pythonpong game python code pygamepong game in python using pygamepython pong game scriptpong python code with pygamepong game pygamepong game code python pygamehow to create pong in pythoncode for ping pong in pygamepong pygamepython pygame pongpong in pythonping ball game in pythonpong game with pyhton pygamehow to make pong in pythonpong game using pygamepong pygame codepong game in python pygamepython pong tutorial pygamepong game in pythonhow to make pong game on pygamepong game pyhon codehow to make pong with with pygamehow to make a python pong that shoots a pingpong code of pygamepong program python pygamebuilding the pong game in pythonpong game on pythonpong in pygamepygame pngpygame pong