How to implement Mini Game pong by Python
This article mainly explains "how Python implements Mini Game pong". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how Python implements Mini Game pong".
Game action picture:
Source code
It is recommended to read it carefully before looking back at DE8UG's analysis of the source code.
From random import choice, random
From turtle import *
From freegames import vector
Def value ():
Randomly generate value between (- 5,-3) or (3,5).
Return (3 + random () 2) * choice ([1,-1])
Ball = vector (0,0)
Aim = vector (value (), value ())
State = {1: 0,2: 0}
Def move (player, change):
"Move player position by change."
State [player] + = change
Def rectangle (x, y, width, height):
"Draw rectangle at (x, y) with given width and height."
Up ()
Goto (x, y)
Down ()
Begin_fill ()
For count in range (2):
Forward (width)
Left (90)
Forward (height)
Left (90)
End_fill ()
Def draw ():
"Draw game and move pong ball."
Clear ()
Rectangle (- 200, state [1], 10,50)
Rectangle (190, state [2], 10,50)
Ball.move (aim)
X = ball.x
Y = ball.y
Up ()
Goto (x, y)
Dot (10)
Update ()
If y
< -200 or y >200:
Aim.y =-aim.y
If x <-185:
Low = state [1]
High = state [1] + 50
If low