In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use Python to make a simple Mini Game". In daily operation, I believe many people have doubts about how to use Python to make a simple Mini Game. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to use Python to make a simple Mini Game". Next, please follow the editor to study!
After the Thonny is installed, install the Pygame Zero module. First, open Thonny, click the menu "tools"-> "Management Module", type "pgzero" and click search, and finally click "install". The installation is successful!
We make an elf that requires that after clicking on the elf, it will make a sound and the elf will fall. The first step is to create a game character:
Create two new directories under the source code files directory: one is images (with a customizable name) to store picture files, and the other is sounds to store sound files.
Create a sprite object with the Actor class: specify the picture, location
Draw sprites in draw: the sprite picture will appear in the specified location alien.pos
The second step is to update update: change the location of the elf, 60 times per second!
Import pgzrun
Import time
# create a wizard
Alien = Actor ('alien')
Alien.pos = 100,0556
# draw window size
WIDTH = 500,
HEIGHT = alien.height + 100
# the draw function will be called automatically every time the window needs to be refreshed
Def draw ():
Screen.clear ()
Screen.fill ((128,0,0))
Alien.draw ()
The update function is called at every frame
Def update ():
Alien.left + = 2
If alien.left > WIDTH:
Alien.left = 0
The third step is the interaction with the genie: pos is the location of the mouse. When you click on the genie, the genie will fall down and issue a "er" sound, and return to the original state after 1 second; if you do not click on the genie, the word "flash" will be printed; if there is no click behavior, the genie will always move in a cycle.
Def on_mouse_down (pos):
If alien.collidepoint (pos):
Set_alien_hurt ()
Else:
Print ("flash")
Def set_alien_hurt ():
Sounds.eep.play ()
Alien.image = 'alien_hurt'
Print ("er")
Clock.schedule_unique (set_alien_normal, 1.0)
Def set_alien_normal ():
Alien.image = 'alien'
Pgzrun.go ()
We're done here! The above is a small example for you to get to know Python's Pygame Zero module. You can use Python to do Mini Game in your spare time. On the one hand, you can consolidate relevant knowledge, on the other hand, you can broaden your horizons and release pressure.
Reference answer of the previous issue
Program:
From turtle import *
From random import *
From math import *
Def tree (n, l):
Pd ()
Pencolor ('brown')
Pensize (n / 3)
Forward (l)
If n > 0:
B = random () * 15 + 10
C = random () * 15 + 10
D = l * (random () * 0.25 + 0.7)
Right (b)
Tree (n-1, d)
Left (b + c)
Tree (n-1, d)
Right (c)
Else:
Right (90)
Pencolor ('green')
Circle (3)
Left (90)
Pu ()
Backward (l)
Ht ()
Speed (0)
Tracer (0)
Pu ()
Backward (100)
Left (90)
Pu ()
Backward (300)
Tree (12,100)
Done ()
Results:
At this point, the study on "how to make a simple Mini Game with Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.