This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

This article code, or can go to the public number: AI Yue Chuang, to copy oh!

Hello, I’m Yue Chuang.

Last piece we talked about the button and mouse of PyGame, and I also put the lack of attention today 🈶️ posted out, yesterday the title of the public account also forgot to write. Harm…

  1. 03- PyGame Keyboard and mouse
  2. 03- PyGame Keyboard and Mouse (code comments added)

directory

  1. Object-oriented concepts
  2. Music playback
  3. Player production

1. Object-oriented concepts

Case 1: Create a new file and write the following code to learn about object-oriented concepts.

I’m going to say a little bit about Python, which is an object-oriented language that can be used to make large-scale programming more efficient. We can start with the concept of classes, which is that things that have the same characteristics are called a class.

Such as:

  • The French, the Japs are all human (personal prejudice, other random, blind judging is even worse). They have eyes, noses, and mouths.
  • Dog: Wangcai, rhubarb what are all dogs, they are all dogs this kind.

Classes are allowed to have different attributes, such as human, there are good, bad, and blind judges. Of course, tall, short, fat and thin.

When a Python class is used, it needs to be instantiated, that is, input property information to create a concrete example, such as Tom, which is called an object.

Object Oriented


Create a hello class
class hello(object) :
	To create a class for greeting, first set the properties that the class has. Since it is a greeting, the properties are name, greeting, and age.
	
	def __init__(self, name, hello_words, age) :  # Initial properties
		self.name = name  # Save the value passed in to the property
		self.hello_words = hello_words
		self.age = age
	
	This class has a function that outputs a greeting statement
	The value of the property set in this class
	def say_hello(self) :  # class function
		print("%s,my name is %s,I'm %d years old." \
		      % (self.hello_words, self.name, self.age))
# ⬆️ %s represents a string, %d represents an integer, and the \ in the middle indicates that a line cannot be written


aiyc = hello('aiyc'.'nice to meet you'.10)  # Create an object of type Hello, named aiyc, with the attribute assigned
aiyc.say_hello()  Call a function in an object
Copy the code

2. Music play

Example 2: Create a new file and write the following code to use the functions provided by PyGame to play music.

As usual, let’s write the code first and then write the code parsing.

Needed material remember to pay attention to the public number: AI Yue Chuang, background reply PyGame to get.

Processing of Music
import pygame
import sys

pygame.init()
size = width, height = 640.480
screen = pygame.display.set_mode(size)
pygame.mixer.init()  Initialize the mixer
# 👇 load() loads the background music, play() plays it.
pygame.mixer.music.load('hop hop-aiyc.mp3')
pygame.mixer.music.play(-1)
# Number of times to play. -1 indicates the loop
pygame.mixer.music.set_volume(0.1)
# Volume maximum 1.0
t = pygame.mixer.Sound('beep1.ogg')
Create sound, not too long
t.play()  # Play this sound

while True:
	for e in pygame.event.get():
		if e.type == pygame.QUIT:
			sys.exit()
Copy the code

Code parsing:

Next, let’s parse our code.

  • Sound file name:hop hop-aiyc.mp3Original sound, rigorous commercial. You can change it to your favorite music.
  • play()You can have two parameters. The first parameter indicates the number of loops, and -1 keeps the loop. The second parameter indicates the start time of the play.
  • Play the sound directly, you can useSound()To instantiate a sound, like t here, and then you can use this T object,play()It’s playing,Stop()Stop playing (the file should not be too large, too long, or the load will fail).

3. Player production

Case 3: create a new file, write the following code, make a simulation player, you can pause and continue to play through the mouse click.

import pygame, sys, time

pygame.init()
size = width, height = 360.120
screen = pygame.display.set_mode(size)
pygame.display.set_caption("AI Joychuang ·Pygame Player")
pygame.mixer.init()  Initialize the mixer
pygame.mixer.music.load('hop hop-aiyc.mp3')  # Volume maximum 1.0
pygame.mixer.music.set_volume(0.1)
pygame.mixer.music.play(-1)
status = 1  # Store variables for playback state
while True:
	for e in pygame.event.get():
		if e.type == pygame.QUIT:
			sys.exit()
		if e.type == pygame.MOUSEBUTTONDOWN:
			if e.button == 1 and status == 0:
				status = 1  Switch state with every click
			elif e.button == 1 and status == 1:
				status = 0
	if status == 1:  # State 1 is playing continuously
		pygame.mixer.music.unpause()  # Continue playing
		screen.fill((0.255.0))  The background color is # and the solid circle is below.
		pygame.draw.circle(screen, (255.0.0), (60.60), 50)
	if status == 0:
		pygame.mixer.music.pause()
		screen.fill((255.0.0))
		pygame.draw.circle(screen, (0.255.0), (60.60), 50)
	# False progress bar
	pygame.draw.line(screen, (0.0.255), (120.80), (350.80), 10)
	pygame.display.update()
Copy the code