Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement Mini Game ​ pong by Python

Shulou Source: shulou.com Published: 2022-06-01 11:43:46 10月04日 Update

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

Tags: Function movement coordinates ball player bezel up and down code next Mini Game two run content file scope character location variable suggestion source code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS Shulou Information Huawei OPPO Reno