May 31, 2021 Article blog
This article comes from the public number: Python Technology Author: Pieson Sauce
A few days ago, to my son bought a flying chess, even like, every day to come with me two sets, yesterday ready to fight a war, found that the dice lost, no dice can not play, just want to use the eraser to do one, suddenly thought of a better way, after a toss, finally finished, the result ...
The dice are cubes, with six faces, each with different locations, from 1 to 6, representing 1 to 6 six numbers, and when you play, roll the dice, wait until it stops, and the face up is a few points, which means a few.
In different games, there are different ways to play the number of points you hit, such as flying chess, shaking to 5 or 6, and taking off a plane.
Now I need to use a program to simulate this process, in fact, is to produce 1 to 6 direct random numbers, directly with
random.randint(1, 6)
can be done, but I do not want to do so simple, one is for children, directly give the number is not intuitive enough, and the other is, can have the opportunity to give a son a stunt, why not?
So the idea is as follows:
After the idea is well conceived, hurry to achieve!
First from the Internet to find some dice material, and finally selected WeChat roll dice emoticon as an element of a series of gif pictures, through the picture resolution tool, extracted from the gif picture each frame, including the point-up picture, and turn the middle of the picture, so that the picture material is done.
In practice, if it is not convenient to obtain picture footage, you can get it from the sample code in this article
Next, it's the programming section, using pygame, a python game engine library.
First, write a dice class that defines the various resources of the dice and manages the status of the dice, as follows:
import random
import pygame
class Dice:
def __init__(self):
self.diceRect = pygame.Rect(200, 225, 100, 100)
self.diceSpin = [
pygame.image.load("asset/rolling/4.png"),
pygame.image.load("asset/rolling/3.png"),
pygame.image.load("asset/rolling/2.png"),
pygame.image.load("asset/rolling/1.png")
]
self.diceStop = [
pygame.image.load("asset/dice/1.png"),
pygame.image.load("asset/dice/2.png"),
pygame.image.load("asset/dice/3.png"),
pygame.image.load("asset/dice/4.png"),
pygame.image.load("asset/dice/5.png"),
pygame.image.load("asset/dice/6.png")
]
self.StopStatus = random.randint(0, 5)
self.SpinStatus = 0
def move(self):
self.SpinStatus += 1
if self.SpinStatus == len(self.diceSpin):
self.SpinStatus = 0
pygame.Rect
method sets a rectangular area, i.e. the game window coordinates (200, 225) and the height and width are 100, which is used to draw the dice in the game window
pygame.image.load
Next, write a game engine class that drives the game, as follows:
import random
import sys
import pygame
class Game:
def __init__(self, width=500, height=600):
pygame.init()
size = width, height
self.screen = pygame.display.set_mode(size)
self.clock = pygame.time.Clock()
self.screen.fill((255, 255, 255))
self.rollTimes = 0 # 掷骰子过程的帧数记录
self.Dice = Dice()
self.start = False # 状态标识
self.rollCount = random.randint(3, 10) # 初始投掷帧数
def roll(self):
self.screen.blit(self.Dice.diceSpin[self.Dice.SpinStatus], self.Dice.diceRect)
self.Dice.move()
self.rollTimes += 1
if self.rollTimes > self.rollCount:
self.start = False
self.rollCount = random.randint(3, 10)
self.Dice.StopStatus = random.randint(0, 5)
self.rollTimes = 0
def stop(self):
self.screen.blit(self.Dice.diceStop[self.Dice.StopStatus], self.Dice.diceRect)
def run(self):
while True:
self.clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if ((event.type == pygame.KEYDOWN and event.key==pygame.K_SPACE) \
or event.type == pygame.MOUSEBUTTONDOWN) \
and self.start == False:
self.start = True
if self.start:
self.roll()
else:
self.stop()
pygame.display.update()
move
method of the dice to get a new rotation state
StopStatus
range is 0 to 5, which corresponds exactly to the index of
diceStop
list, which is why the random number range is 0 to 5
roll
method, otherwise call
stop
method
pygame.display.update()
to refresh the window once
Here's a description of
clock.tick
which is used to make the loop execute how many times per second, abstractly, as animated frames, that is, how many frames per second.
We are more familiar with
clock.tick
which indicates how long to wait before executing, and then the effect of
clock.tick(10)
is equivalent to
time.sleep(0.1)
that is, 10 executions per second, which is equivalent to one tenth of a second at a time.
time.sleep
if __name__ == '__main__':
Game().run()
Note: Switch the directory to run under the code catalog, or you may be prompted that the picture file cannot be found.
Here's how it works, like this:
After tossing, I can't wait to go to my son to show off, as a result, he has fallen asleep, surrounded by some flying pieces ...
Whether in life or work, programming skills are becoming more and more important, programming has become a tool for thinking and creating, learn a programming skills, not only can help themselves, perhaps can save a child programming costs, in improving children's logical thinking ability at the same time, but also to enhance the feelings with children, I have to say, when my son used the dice I wrote to play flying chess, more happy.
To be a hardcore parent, I use Python!
That's
W3Cschool编程狮
has to say about
using Python to explain what a hard-core dad is
called, and I hope it will help you.