Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Mini Game ​ pong by Python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report