【Dison夏令营 Day 26】PyGame 中的赛车游戏

news2024/11/14 15:04:50

在本文中,我们将了解如何使用 Pygame 在 Python 中创建一个赛车游戏。在这个游戏中,我们将拥有驾驶、障碍物碰撞、通过关卡时速度增加、暂停、倒计时、记分牌和说明书屏幕等功能。

在这里插入图片描述

所需模块:

在继续之前,请在命令提示符下运行以下命令行,安装必要的软件包,并将它们导入到您的 game.py 文件中:

pip install pygame
pip install time 
pip install random

项目结构:

在这里插入图片描述

第 1 步:导入所有模块

# code for developing car racing game in python 
import pygame 
import time 
import random 

第 2 步:设置屏幕

使用 pygame.init() 初始化所有 Pygame 模块。现在设置颜色值。现在设置不同文字的标题

pygame.image.load() 用于加载图像资源
# Initialize pygame and set the colors with captions 
pygame.init() 

# Define color codes 
gray = (119, 118, 110) 
black = (0, 0, 0) 
red = (255, 0, 0) 
green = (0, 200, 0) 
blue = (0, 0, 200) 
bright_red = (255, 0, 0) 
bright_green = (0, 255, 0) 
bright_blue = (0, 0, 255) 

# Define display dimensions 
display_width = 800
display_height = 600

# Set up game display 
gamedisplays = pygame.display.set_mode((display_width, 
										display_height)) 
pygame.display.set_caption("car game") 
clock = pygame.time.Clock() 

# Load car image and background images 
carimg = pygame.image.load('car1.jpg') 
backgroundpic = pygame.image.load("download12.jpg") 
yellow_strip = pygame.image.load("yellow strip.jpg") 
strip = pygame.image.load("strip.jpg") 
intro_background = pygame.image.load("background.jpg") 
instruction_background = pygame.image.load("background2.jpg") 

# Set car width and initialize pause state 
car_width = 56
pause = False

第 3 步:使用 “开始 ”屏幕

在 “开始 ”屏幕中,有三个按钮,分别名为 “开始”、“退出 ”和 “指导”。START 按钮将初始化游戏,QUIT 按钮将退出窗口,INSTRUCTION 按钮将显示玩家控制按钮。

如果当前屏幕是介绍屏幕,那么名为 intro 的布尔值将被设置为 True。

使用 .rect() 绘制矩形。输入长、宽、高来制作矩形。
blit() 将把矩形表面放到屏幕顶部。
# Intro screen 
def intro_loop(): 
	intro = True
	while intro: 
		for event in pygame.event.get(): 
			if event.type == pygame.QUIT: 
				pygame.quit() 
				quit() 
				sys.exit() 

		# Display background image 
		gamedisplays.blit(intro_background, 
						(0, 0)) 

		# Render and display "CAR GAME" text 
		largetext = pygame.font.Font 
					('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects 
						("CAR GAME", largetext) 
		TextRect.center = (400, 100) 
		gamedisplays.blit(TextSurf, TextRect) 

		# Render and display "START" button 
		button("START", 150, 520, 100, 50, green, 
								bright_green, "play") 

		# Render and display "QUIT" button 
		button("QUIT", 550, 520, 100, 50, red, 
								bright_red, "quit") 

		# Render and display "INSTRUCTION" button 
		button("INSTRUCTION", 300, 520, 200, 
					50, blue, bright_blue, "intro") 

		pygame.display.update() 
		clock.tick(50) 

第 4 步:为按钮添加功能

现在,让我们把这些看起来花哨的按钮变成反应按钮。例如,“播放”(PLAY)按钮应触发主游戏循环,并带有一个计数器,用于计算玩家玩游戏的秒数。“退出”(QUIT)按钮应退出窗口。“介绍”(INTRO)按钮应提供游戏说明手册(详情将在下一步讨论)。

# Function to create a button with specified parameters 
# msg: The text to be displayed on the button 
# x, y: The coordinates of the top-left corner of the button 
# w, h: The width and height of the button 
# ic: The color of the button when inactive 
# ac: The color of the button when active (hovered over) 
# action: The action to be performed when the button is clicked 

def button(msg, x, y, w, h, ic, ac, action=None): 
	# Get the current mouse position 
	mouse = pygame.mouse.get_pos() 
	# Get the current state of mouse buttons 
	click = pygame.mouse.get_pressed() 

	# Check if mouse is within the button's boundaries 
	if x+w > mouse[0] > x and y+h > mouse[1] > y: 
		# Draw button with active color 
		pygame.draw.rect(gamedisplays, ac, (x, y, w, h)) 
		# Check if left mouse button is clicked 
		# and action is specified 
		if click[0] == 1 and action != None: 
			# If action is "play", call the countdown() 
			if action == "play": 
				countdown() 
			# If action is "quit", quit the game 
			elif action == "quit": 
				pygame.quit() 
				quit() 
				sys.exit() 

			elif action == "intro": 
				introduction() 
			# If action is "menu", call the intro_loop() function 
			elif action == "menu": 
				intro_loop() 
			# If action is "pause", call the paused() function 
			elif action == "pause": 
				paused() 
			# If action is "unpause", call the unpaused() function 
				unpaused() 
			elif action == "unpause": 

	else: 
		# Draw button with inactive color 
		pygame.draw.rect(gamedisplays, ic, (x, y, w, h)) 
	smalltext = pygame.font.Font("freesansbold.ttf", 20) 
	textsurf, textrect = text_objects(msg, smalltext) 
	textrect.center = ((x+(w/2)), (y+(h/2))) 
	gamedisplays.blit(textsurf, textrect) 

第 5 步:实现介绍界面

由于当前屏幕是说明屏幕,我们将把说明布尔值设为 true。在说明手册中,最重要的是玩家的控制和游戏的内容。因此,我们将在说明界面中组织这些细节。

pygame.font.Font('fontname.ttf', fontsize) => 这将显示指定字体大小的字体
# Function to display the introduction screen 
def introduction(): 
	introduction = True
	while introduction: 
		# Get events from the event queue 
		for event in pygame.event.get(): 
			# If the 'QUIT' event is triggered 
			# (e.g., window closed) 
			if event.type == pygame.QUIT: 
				pygame.quit() # Quit pygame 
				quit() # Quit the game 
				sys.exit() # Exit the system 
		# Draw the instruction background 
		gamedisplays.blit(instruction_background, (0, 0)) 
		# Set font for large text 
		largetext = pygame.font.Font('freesansbold.ttf', 80) 
		# Set font for small text 
		smalltext = pygame.font.Font('freesansbold.ttf', 20) 
		# Set font for medium text 
		mediumtext = pygame.font.Font('freesansbold.ttf', 40) 

		# Render and draw the instruction text 
		textSurf, textRect = text_objects("This is an car game" +
			"in which you need dodge the coming cars", smalltext) 
		textRect.center = ((350), (200)) 
		TextSurf, TextRect = text_objects("INSTRUCTION", largetext) 
		TextRect.center = ((400), (100)) 
		gamedisplays.blit(TextSurf, TextRect) 
		gamedisplays.blit(textSurf, textRect) 

		# Render and draw the control instructions 
		stextSurf, stextRect = text_objects( 
			"ARROW LEFT : LEFT TURN", smalltext) 
		stextRect.center = ((150), (400)) 
		hTextSurf, hTextRect = text_objects( 
			"ARROW RIGHT : RIGHT TURN", smalltext) 
		hTextRect.center = ((150), (450)) 
		atextSurf, atextRect = text_objects 
					("A : ACCELERATOR", smalltext) 
		atextRect.center = ((150), (500)) 
		rtextSurf, rtextRect = text_objects 
						("B : BRAKE ", smalltext) 
		rtextRect.center = ((150), (550)) 
		ptextSurf, ptextRect = text_objects 
						("P : PAUSE ", smalltext) 
		ptextRect.center = ((150), (350)) 
		sTextSurf, sTextRect = text_objects 
							("CONTROLS", mediumtext) 
		sTextRect.center = ((350), (300)) 
		gamedisplays.blit(sTextSurf, sTextRect) 
		gamedisplays.blit(stextSurf, stextRect) 
		gamedisplays.blit(hTextSurf, hTextRect) 
		gamedisplays.blit(atextSurf, atextRect) 
		gamedisplays.blit(rtextSurf, rtextRect) 
		gamedisplays.blit(ptextSurf, ptextRect) 

		# Render and draw the 'BACK' button 
		button("BACK", 600, 450, 100, 50, blue, 
							bright_blue, "menu") 

		pygame.display.update() # Update the display 
		clock.tick(30) # Limit frame rate to 30 FPS 

第 6 步:实现暂停功能

由于暂停函数会在您玩游戏时出现,因此它应该能够冻结除自身函数外的所有全局操作。因此,为了确保这一点,我们在 “暂停 ”按钮中使用了全局声明,并将其值设置为 True。按下 “暂停 ”按钮后,新界面将显示 “继续”(CONTINUE)、“重启”(RESTART)和 “主菜单”(MAIN MENU)按钮。取消暂停时,我们只需将全局布尔暂停重置为 “False”。

def paused(): 
	global pause 

	# Loop for handling events during pause state 
	while pause: 
		for event in pygame.event.get(): 
			if event.type == pygame.QUIT: 
				pygame.quit() 
				quit() 
				sys.exit() 
		gamedisplays.blit(instruction_background, 
						(0, 0)) 
		largetext = pygame.font.Font('freesansbold.ttf', 
							115) 
		TextSurf, TextRect = text_objects("PAUSED", 
							largetext) 
		TextRect.center = ( 
		(display_width/2), 
		(display_height/2)) 
		gamedisplays.blit(TextSurf, TextRect) 
		# Create buttons for continue, restart, and main menu 
		button("CONTINUE", 150, 
			450, 150, 50, 
			green, bright_green, "unpause") 
		button("RESTART", 350, 
			450, 150, 50, 
			blue, bright_blue, "play") 
		button("MAIN MENU", 550, 
			450, 200, 50, 
			red, bright_red, "menu") 
		pygame.display.update() 
		clock.tick(30) 


def unpaused(): 
	global pause 
	pause = False

第 7 步:游戏倒计时系统

让我们制作一个记分牌,记录得分和躲避汽车的情况。除此之外,我们还可以在 countdown_background() 中设置暂停按钮。

def countdown_background(): 
	# Import the necessary modules and set up the game display 
	# Initialize the font for displaying text 
	font = pygame.font.SysFont(None, 25) 
	# Set the initial positions for the game objects 
	# (background, strips, car, and text) 
	x = (display_width*0.45) 
	y = (display_height*0.8) 
	# Draw the background images on the game display 
	gamedisplays.blit(backgroundpic, (0, 0)) 
	gamedisplays.blit(backgroundpic, (0, 200)) 
	gamedisplays.blit(backgroundpic, (0, 400)) 
	gamedisplays.blit(backgroundpic, (700, 0)) 
	gamedisplays.blit(backgroundpic, (700, 200)) 
	gamedisplays.blit(backgroundpic, (700, 400)) 
	# Draw the yellow strips on the game display 
	gamedisplays.blit(yellow_strip, (400, 100)) 
	gamedisplays.blit(yellow_strip, (400, 200)) 
	gamedisplays.blit(yellow_strip, (400, 300)) 
	gamedisplays.blit(yellow_strip, (400, 400)) 
	gamedisplays.blit(yellow_strip, (400, 100)) 
	gamedisplays.blit(yellow_strip, (400, 500)) 
	gamedisplays.blit(yellow_strip, (400, 0)) 
	gamedisplays.blit(yellow_strip, (400, 600)) 
	# Draw the side strips on the game display 
	gamedisplays.blit(strip, (120, 200)) 
	gamedisplays.blit(strip, (120, 0)) 
	gamedisplays.blit(strip, (120, 100)) 
	gamedisplays.blit(strip, (680, 100)) 
	gamedisplays.blit(strip, (680, 0)) 
	gamedisplays.blit(strip, (680, 200)) 
	# Draw the car on the game display 
	gamedisplays.blit(carimg, (x, y)) 
	# Draw the text for the score and number of dodged cars 
	text = font.render("DODGED: 0", True, black) 
	score = font.render("SCORE: 0", True, red) 
	gamedisplays.blit(text, (0, 50)) 
	gamedisplays.blit(score, (0, 30)) 
	# Draw the "PAUSE" button on the game display 
	button("PAUSE", 650, 0, 150, 50, blue, bright_blue, "pause") 

第 8 步:执行倒计时函数

只要游戏开始,我们的倒计时就会开启,因此我们要将布尔值 countdown 设为 true。现在循环直到倒计时为 true,并在循环中调用 countdown_background。此外,在倒计时期间,时钟应该每 1 秒滴答作响一次,因此设置 clock.tick(1)。每秒钟后,我们将更新显示内容。这个循环一直持续到倒计时布尔值变为 false。

	gamedisplay.fill(color) => 这将用给定的颜色显示游戏画面
	pygame.display.update() => 每次调用它都会更新整个游戏图形界面。
def countdown(): 
	# Initialize a boolean variable to indicate if countdown is i 
		# n progress 
	countdown = True
	# Continue looping until countdown is complete 
	while countdown: 
		# Check for events in the pygame event queue 
		for event in pygame.event.get(): 
			# If user closes the game window 
			if event.type == pygame.QUIT: 
				pygame.quit() # Quit pygame 
				quit() # Quit the game 
				sys.exit() # Exit the program 
		# Fill the game display with a gray color 
		gamedisplays.fill(gray) 
		# Call a function to display the countdown background 
		countdown_background() 

		# Display "3" in large font at the center of the screen 
		largetext = pygame.font.Font('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects("3", largetext) 
		TextRect.center = ((display_width/2), (display_height/2)) 
		gamedisplays.blit(TextSurf, TextRect) 
		pygame.display.update() 
		clock.tick(1) # Delay for 1 second 

		gamedisplays.fill(gray) 
		countdown_background() 

		# Display "2" in large font at the center of the screen 
		largetext = pygame.font.Font('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects("2", largetext) 
		TextRect.center = ((display_width/2), (display_height/2)) 
		gamedisplays.blit(TextSurf, TextRect) 
		pygame.display.update() 
		clock.tick(1) # Delay for 1 second 

		gamedisplays.fill(gray) 
		countdown_background() 

		# Display "1" in large font at the center of the screen 
		largetext = pygame.font.Font('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects("1", largetext) 
		TextRect.center = ((display_width/2), (display_height/2)) 
		gamedisplays.blit(TextSurf, TextRect) 
		pygame.display.update() 
		clock.tick(1) # Delay for 1 second 

		gamedisplays.fill(gray) 
		countdown_background() 

		# Display "GO!!!" in large font at the center of the screen 
		largetext = pygame.font.Font('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects("GO!!!", largetext) 
		TextRect.center = ((display_width/2), (display_height/2)) 
		gamedisplays.blit(TextSurf, TextRect) 
		pygame.display.update() 
		clock.tick(1) # Delay for 1 second 
		# Call the game loop function after the countdown is complete 
		game_loop() 

第 9 步:定义游戏中的障碍物

在赛车比赛中会有一些障碍物,比如对手的赛车,因此让我们来定义这些障碍物。要制作障碍物,我们需要它们的 x、y 和 obs id。获得这些信息后,我们就可以在游戏窗口中轻松加载这些障碍物了。

# Loading the obstacles 
def obstacle(obs_startx, obs_starty, obs): 
	if obs == 0: 
		obs_pic = pygame.image.load("car.jpg") 
	elif obs == 1: 
		obs_pic = pygame.image.load("car1.jpg") 
	elif obs == 2: 
		obs_pic = pygame.image.load("car2.jpg") 
	elif obs == 3: 
		obs_pic = pygame.image.load("car4.jpg") 
	elif obs == 4: 
		obs_pic = pygame.image.load("car5.jpg") 
	elif obs == 5: 
		obs_pic = pygame.image.load("car6.jpg") 
	elif obs == 6: 
		obs_pic = pygame.image.load("car7.jpg") 
	gamedisplays.blit(obs_pic, (obs_startx, obs_starty)) 

第 10 步: 实现得分系统

score_system()函数用于显示分数,在下面将要讨论的 game_loop 函数中使用。text_objects()函数用于在屏幕上渲染文本。

def score_system(passed, score): 
	# Create a font object with size 25 
	font = pygame.font.SysFont(None, 25) 
	# Render the "Passed" text with passed parameter 
	# and color black 
	text = font.render("Passed"+str(passed), True, black) 
	# Render the "Score" text with score parameter and color red 
	score = font.render("Score"+str(score), True, red) 
	# Draw the "Passed" text on the game display at (0, 50) 
	# coordinates 
	gamedisplays.blit(text, (0, 50)) 
	# Draw the "Score" text on the game display at (0, 30) 
	# coordinates 
	gamedisplays.blit(score, (0, 30)) 


def text_objects(text, font): 
	# Render the given text with the given font and color black 
	textsurface = font.render(text, True, black) 
	return textsurface, textsurface.get_rect() 


def message_display(text): 
	# Create a font object with size 80 
	largetext = pygame.font.Font("freesansbold.ttf", 80) 
	# Render the given text with the created font 
	textsurf, textrect = text_objects(text, largetext) 
	textrect.center = ((display_width/2), (display_height/2)) 
	# Draw the rendered text on the game display at the center of the 
	# screen 
	gamedisplays.blit(textsurf, textrect) 
	pygame.display.update() 
	time.sleep(3) 
	game_loop() 

第 11 步:后备逻辑

无论开发人员创建什么程序,都应该考虑到一些回退机制,即如果程序出错无法运行,用户应该知道发生了什么。在本例中,我们将在主屏幕上显示 “YOU CRASHED”(你崩溃了)的信息。

def crash(): 
	message_display("YOU CRASHED") 

第 12 步:屏幕上的游戏用户界面

对于赛车游戏来说,我们需要一个道路图像和一些背景场景地形。我们将在背景功能中制作该用户界面。黄色条纹是道路上的竖条纹,通常位于道路中间。

# on Screen UI 
def background(): 
	gamedisplays.blit(backgroundpic, (0, 0)) 
	gamedisplays.blit(backgroundpic, (0, 200)) 
	gamedisplays.blit(backgroundpic, (0, 400)) 
	gamedisplays.blit(backgroundpic, (700, 0)) 
	gamedisplays.blit(backgroundpic, (700, 200)) 
	gamedisplays.blit(backgroundpic, (700, 400)) 
	gamedisplays.blit(yellow_strip, (400, 0)) 
	gamedisplays.blit(yellow_strip, (400, 100)) 
	gamedisplays.blit(yellow_strip, (400, 200)) 
	gamedisplays.blit(yellow_strip, (400, 300)) 
	gamedisplays.blit(yellow_strip, (400, 400)) 
	gamedisplays.blit(yellow_strip, (400, 500)) 
	gamedisplays.blit(strip, (120, 0)) 
	gamedisplays.blit(strip, (120, 100)) 
	gamedisplays.blit(strip, (120, 200)) 
	gamedisplays.blit(strip, (680, 0)) 
	gamedisplays.blit(strip, (680, 100)) 
	gamedisplays.blit(strip, (680, 200)) 


def car(x, y): 
	gamedisplays.blit(carimg, (x, y)) 

第 13 步:运行游戏

初始化 game_loop()后,我们将检查布尔暂停是否为假,如果为假,则继续游戏。我们会将 obstacle_speed 设置为 9,你可以根据自己的需要更改速度。为了检查玩家是否撞到了汽车,我们将把布尔值 bumped 初始化为 false。除此以外,在 game_loop 函数中,我们还将配置玩家的控制、每一关后障碍物速度的增加以及记分牌的递增。

#!/usr/bin/python 
# -*- coding: utf-8 -*- 

def game_loop(): 
	global pause 
	x = display_width * 0.45
	y = display_height * 0.8
	x_change = 0
	obstacle_speed = 9
	obs = 0
	y_change = 0
	obs_startx = random.randrange(200, display_width - 200) 
	obs_starty = -750
	obs_width = 56
	obs_height = 125
	passed = 0
	level = 0
	score = 0
	y2 = 7
	fps = 120

	# flag to indicate that the player has been crashed 

	bumped = False

	while not bumped: 
		for event in pygame.event.get(): 
			if event.type == pygame.QUIT: 
				pygame.quit() 
				quit() 

			if event.type == pygame.KEYDOWN: 
				if event.key == pygame.K_LEFT: 
					x_change = -5
				if event.key == pygame.K_RIGHT: 
					x_change = 5
				if event.key == pygame.K_a: 
					obstacle_speed += 2
				if event.key == pygame.K_b: 
					obstacle_speed -= 2
			if event.type == pygame.KEYUP: 
				if event.key == pygame.K_LEFT: 
					x_change = 0
				if event.key == pygame.K_RIGHT: 
					x_change = 0

		# Update player's car position 

	x += x_change 

	# Set pause flag to True 

	pause = True

	# Fill the game display with gray color 

	gamedisplays.fill(gray) 

	# Update background position 

	rel_y = y2 % backgroundpic.get_rect().width 
	gamedisplays.blit(backgroundpic, (0, rel_y 
									- backgroundpic.get_rect().width)) 
	gamedisplays.blit(backgroundpic, (700, rel_y 
									- backgroundpic.get_rect().width)) 

	# Draw background strips 

	if rel_y < 800: 

		# Draw background strips 

		gamedisplays.blit(backgroundpic, (0, rel_y)) 
		gamedisplays.blit(backgroundpic, (700, rel_y)) 
		gamedisplays.blit(yellow_strip, (400, rel_y)) 
		gamedisplays.blit(yellow_strip, (400, rel_y + 100)) 

	# Update obstacle positions and display them 

	y2 += obstacle_speed 
	obs_starty -= obstacle_speed / 4
	obstacle(obs_startx, obs_starty, obs) 
	obs_starty += obstacle_speed 

	# Update player's car position and display it 

	car(x, y) 

	# Update score system and display score 

	score_system(passed, score) 

	# Check for collision with screen edges and call crash() 
	# function if collision occurs 

	if x > 690 - car_width or x < 110: 
		crash() 
	if x > display_width - (car_width + 110): 
		crash() 
	if x < 110: 
		crash() 

	# Update obstacle positions and display them 

	if obs_starty > display_height: 
		obs_starty = 0 - obs_height 
		obs_startx = random.randrange(170, display_width - 170) 
		obs = random.randrange(0, 7) 
		passed = passed + 1
		score = passed * 10

		# Check for level up and update obstacle speed, display 
		# level text, and pause for 3 seconds 

		if int(passed) % 10 == 0: 
			level = level + 1
			obstacle_speed += 2
			largetext = pygame.font.Font('freesansbold.ttf', 80) 
			(textsurf, textrect) = text_objects('LEVEL' + str(level), 
												largetext) 
			textrect.center = (display_width / 2, display_height / 2) 
			gamedisplays.blit(textsurf, textrect) 
			pygame.display.update() 
			time.sleep(3) 

	# Check for collision with obstacles and call crash() 
	# function if collision occurs 

	if y < obs_starty + obs_height: 
		if x > obs_startx and x < obs_startx + obs_width or x \ 
			+ car_width > obs_startx and x + car_width < obs_startx \ 
				+ obs_width: 
			crash() 

	# Draw pause button 

	button( 
		'Pause', 
		650, 
		0, 
		150, 
		50, 
		blue, 
		bright_blue, 
		'pause', 
	) 

	# Update game display and set frames per second to 60 

	pygame.display.update() 
	clock.tick(60) 

第 14 步:调用函数

现在让我们调用重要函数来启动游戏,即

  • intro_loop(): PLAY(播放)、QUIT(退出)和 INSTRUCTION(指导)按钮屏幕。
  • game_loop() : 游戏的逻辑: 用于退出游戏窗口
intro_loop() 
game_loop() 
pygame.quit() 
quit() 

第 15 步:Window.py 文件

现在,所有这些程序都需要窗口选项卡才能运行。因此,让我们在当前目录下创建一个新文件 window.py(快照附在下面),并复制以下代码,现在运行代码,你就可以玩游戏了。

# Importing the Pygame library for game development 
import pygame 

# Importing the Time module for handling 
# time-related operations 
import time 

# Initializing Pygame 
pygame.init() 

# Width of the game window 
display_width = 800

# Height of the game window 
display_height = 600

# Setting the display mode with specified width and height 
display = pygame.display.set_mode((display_width, 
								display_height)) 

# Updating the display 
pygame.display.update() 

# Setting the caption/title of the game window 
pygame.display.set_caption("Car Game") 

# Creating a Clock object to control game frame rate 
clock = pygame.time.Clock() 

# Flag to indicate if the car is bumped or not 
bumped = False

# Looping until the car is bumped 
while not bumped: 
	# Checking for events (e.g. key presses, mouse clicks) 
	for event in pygame.event.get(): 
		# If the QUIT event is triggered (user closes the game window) 
		if event.type == pygame.QUIT: 
			# Set the bumped flag to True to exit the game loop 
			bumped = True
			# Quitting the game and closing the game window 
			quit() 

完整代码片段

# code for developing car racing game in python 
import pygame 
import time 
import random 

# initialize pygame and set the colors 
pygame.init() 
gray = (119, 118, 110) 
black = (0, 0, 0) 
red = (255, 0, 0) 
green = (0, 200, 0) 
blue = (0, 0, 200) 
bright_red = (255, 0, 0) 
bright_green = (0, 255, 0) 
bright_blue = (0, 0, 255) 
display_width = 800
display_height = 600

gamedisplays = pygame.display.set_mode( 
	(display_width, display_height)) 
pygame.display.set_caption("car game") 
clock = pygame.time.Clock() 
carimg = pygame.image.load('car1.jpg') 
backgroundpic = pygame.image.load("download12.jpg") 
yellow_strip = pygame.image.load("yellow strip.jpg") 
strip = pygame.image.load("strip.jpg") 
intro_background = pygame.image.load("background.jpg") 
instruction_background = pygame.image.load("background2.jpg") 
car_width = 56
pause = False

# Intro screen 


def intro_loop(): 
	intro = True
	while intro: 
		for event in pygame.event.get(): 
			if event.type == pygame.QUIT: 
				pygame.quit() 
				quit() 
				sys.exit() 
		gamedisplays.blit(intro_background, (0, 0)) 
		largetext = pygame.font.Font('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects("CAR GAME", largetext) 
		TextRect.center = (400, 100) 
		gamedisplays.blit(TextSurf, TextRect) 
		button("START", 150, 520, 100, 50, green, 
			bright_green, "play") 
		button("QUIT", 550, 520, 100, 
			50, 
			red, 
			bright_red, 
			"quit") 
		button("INSTRUCTION", 300, 520, 200, 
			50, blue, bright_blue, 
			"intro") 
		pygame.display.update() 
		clock.tick(50) 


def button(msg, x, y, w, h, ic, ac, action=None): 
	mouse = pygame.mouse.get_pos() 
	click = pygame.mouse.get_pressed() 
	if x+w > mouse[0] > x and y+h > mouse[1] > y: 
		pygame.draw.rect(gamedisplays, 
						ac, (x, y, w, h)) 
		if click[0] == 1 and action != None: 
			if action == "play": 
				countdown() 
			elif action == "quit": 
				pygame.quit() 
				quit() 
				sys.exit() 
			elif action == "intro": 
				introduction() 
			elif action == "menu": 
				intro_loop() 
			elif action == "pause": 
				paused() 
			elif action == "unpause": 
				unpaused() 

	else: 
		pygame.draw.rect(gamedisplays, 
						ic, 
						(x, y, w, h)) 
	smalltext = pygame.font.Font("freesansbold.ttf", 20) 
	textsurf, textrect = text_objects(msg, smalltext) 
	textrect.center = ((x+(w/2)), (y+(h/2))) 
	gamedisplays.blit(textsurf, textrect) 


def introduction(): 
	introduction = True
	while introduction: 
		for event in pygame.event.get(): 
			if event.type == pygame.QUIT: 
				pygame.quit() 
				quit() 
				sys.exit() 
		gamedisplays.blit(instruction_background, (0, 0)) 
		largetext = pygame.font.Font('freesansbold.ttf', 80) 
		smalltext = pygame.font.Font('freesansbold.ttf', 20) 
		mediumtext = pygame.font.Font('freesansbold.ttf', 40) 
		textSurf, textRect = text_objects( 
			"This is an car game in which you" +
			"need dodge the coming cars", smalltext) 
		textRect.center = ((350), (200)) 
		TextSurf, TextRect = text_objects("INSTRUCTION", largetext) 
		TextRect.center = ((400), (100)) 
		gamedisplays.blit(TextSurf, TextRect) 
		gamedisplays.blit(textSurf, textRect) 
		stextSurf, stextRect = text_objects( 
			"ARROW LEFT : LEFT TURN", smalltext) 
		stextRect.center = ((150), (400)) 
		hTextSurf, hTextRect = text_objects( 
			"ARROW RIGHT : RIGHT TURN", smalltext) 
		hTextRect.center = ((150), (450)) 
		atextSurf, atextRect = text_objects("A : ACCELERATOR", smalltext) 
		atextRect.center = ((150), (500)) 
		rtextSurf, rtextRect = text_objects("B : BRAKE ", smalltext) 
		rtextRect.center = ((150), (550)) 
		ptextSurf, ptextRect = text_objects("P : PAUSE ", smalltext) 
		ptextRect.center = ((150), (350)) 
		sTextSurf, sTextRect = text_objects("CONTROLS", mediumtext) 
		sTextRect.center = ((350), (300)) 
		gamedisplays.blit(sTextSurf, sTextRect) 
		gamedisplays.blit(stextSurf, stextRect) 
		gamedisplays.blit(hTextSurf, hTextRect) 
		gamedisplays.blit(atextSurf, atextRect) 
		gamedisplays.blit(rtextSurf, rtextRect) 
		gamedisplays.blit(ptextSurf, ptextRect) 
		button("BACK", 600, 450, 100, 50, blue, 
			bright_blue, "menu") 
		pygame.display.update() 
		clock.tick(30) 


def paused(): 
	global pause 

	while pause: 
		for event in pygame.event.get(): 
			if event.type == pygame.QUIT: 
				pygame.quit() 
				quit() 
				sys.exit() 
		gamedisplays.blit(instruction_background, (0, 0)) 
		largetext = pygame.font.Font('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects("PAUSED", largetext) 
		TextRect.center = ( 
			(display_width/2), 
			(display_height/2) 
		) 
		gamedisplays.blit(TextSurf, TextRect) 
		button("CONTINUE", 150, 450, 
			150, 50, green, 
			bright_green, "unpause") 
		button("RESTART", 350, 450, 150, 
			50, blue, bright_blue, 
			"play") 
		button("MAIN MENU", 550, 450, 
			200, 50, red, bright_red, 
			"menu") 
		pygame.display.update() 
		clock.tick(30) 


def unpaused(): 
	global pause 
	pause = False


def countdown_background(): 
	font = pygame.font.SysFont(None, 25) 
	x = (display_width*0.45) 
	y = (display_height*0.8) 
	gamedisplays.blit(backgroundpic, (0, 0)) 
	gamedisplays.blit(backgroundpic, (0, 200)) 
	gamedisplays.blit(backgroundpic, (0, 400)) 
	gamedisplays.blit(backgroundpic, (700, 0)) 
	gamedisplays.blit(backgroundpic, (700, 200)) 
	gamedisplays.blit(backgroundpic, (700, 400)) 
	gamedisplays.blit(yellow_strip, (400, 100)) 
	gamedisplays.blit(yellow_strip, (400, 200)) 
	gamedisplays.blit(yellow_strip, (400, 300)) 
	gamedisplays.blit(yellow_strip, (400, 400)) 
	gamedisplays.blit(yellow_strip, (400, 100)) 
	gamedisplays.blit(yellow_strip, (400, 500)) 
	gamedisplays.blit(yellow_strip, (400, 0)) 
	gamedisplays.blit(yellow_strip, (400, 600)) 
	gamedisplays.blit(strip, (120, 200)) 
	gamedisplays.blit(strip, (120, 0)) 
	gamedisplays.blit(strip, (120, 100)) 
	gamedisplays.blit(strip, (680, 100)) 
	gamedisplays.blit(strip, (680, 0)) 
	gamedisplays.blit(strip, (680, 200)) 
	gamedisplays.blit(carimg, (x, y)) 
	text = font.render("DODGED: 0", True, black) 
	score = font.render("SCORE: 0", True, red) 
	gamedisplays.blit(text, (0, 50)) 
	gamedisplays.blit(score, (0, 30)) 
	button("PAUSE", 650, 0, 150, 50, blue, bright_blue, "pause") 


def countdown(): 
	countdown = True

	while countdown: 
		for event in pygame.event.get(): 
			if event.type == pygame.QUIT: 
				pygame.quit() 
				quit() 
				sys.exit() 
		gamedisplays.fill(gray) 
		countdown_background() 
		largetext = pygame.font.Font('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects("3", largetext) 
		TextRect.center = ( 
			(display_width/2), 
			(display_height/2)) 
		gamedisplays.blit(TextSurf, TextRect) 
		pygame.display.update() 
		clock.tick(1) 
		gamedisplays.fill(gray) 
		countdown_background() 
		largetext = pygame.font.Font('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects("2", largetext) 
		TextRect.center = ( 
			(display_width/2), 
			(display_height/2)) 
		gamedisplays.blit(TextSurf, TextRect) 
		pygame.display.update() 
		clock.tick(1) 
		gamedisplays.fill(gray) 
		countdown_background() 
		largetext = pygame.font.Font('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects("1", largetext) 
		TextRect.center = ( 
			(display_width/2), 
			(display_height/2)) 
		gamedisplays.blit(TextSurf, TextRect) 
		pygame.display.update() 
		clock.tick(1) 
		gamedisplays.fill(gray) 
		countdown_background() 
		largetext = pygame.font.Font('freesansbold.ttf', 115) 
		TextSurf, TextRect = text_objects("GO!!!", largetext) 
		TextRect.center = ( 
			(display_width/2), 
			(display_height/2)) 
		gamedisplays.blit(TextSurf, TextRect) 
		pygame.display.update() 
		clock.tick(1) 
		game_loop() 


def obstacle(obs_startx, obs_starty, obs): 
	if obs == 0: 
		obs_pic = pygame.image.load("car.jpg") 
	elif obs == 1: 
		obs_pic = pygame.image.load("car1.jpg") 
	elif obs == 2: 
		obs_pic = pygame.image.load("car2.jpg") 
	elif obs == 3: 
		obs_pic = pygame.image.load("car4.jpg") 
	elif obs == 4: 
		obs_pic = pygame.image.load("car5.jpg") 
	elif obs == 5: 
		obs_pic = pygame.image.load("car6.jpg") 
	elif obs == 6: 
		obs_pic = pygame.image.load("car7.jpg") 
	gamedisplays.blit(obs_pic, 
					(obs_startx, 
					obs_starty)) 


def score_system(passed, score): 
	font = pygame.font.SysFont(None, 25) 
	text = font.render("Passed"+str(passed), True, black) 
	score = font.render("Score"+str(score), True, red) 
	gamedisplays.blit(text, (0, 50)) 
	gamedisplays.blit(score, (0, 30)) 


def text_objects(text, font): 
	textsurface = font.render(text, True, black) 
	return textsurface, textsurface.get_rect() 


def message_display(text): 
	largetext = pygame.font.Font("freesansbold.ttf", 80) 
	textsurf, textrect = text_objects(text, largetext) 
	textrect.center = ( 
		(display_width/2), 
		(display_height/2)) 
	gamedisplays.blit(textsurf, textrect) 
	pygame.display.update() 
	time.sleep(3) 
	game_loop() 


def crash(): 
	message_display("YOU CRASHED") 


def background(): 
	gamedisplays.blit(backgroundpic, (0, 0)) 
	gamedisplays.blit(backgroundpic, (0, 200)) 
	gamedisplays.blit(backgroundpic, (0, 400)) 
	gamedisplays.blit(backgroundpic, (700, 0)) 
	gamedisplays.blit(backgroundpic, (700, 200)) 
	gamedisplays.blit(backgroundpic, (700, 400)) 
	gamedisplays.blit(yellow_strip, (400, 0)) 
	gamedisplays.blit(yellow_strip, (400, 100)) 
	gamedisplays.blit(yellow_strip, (400, 200)) 
	gamedisplays.blit(yellow_strip, (400, 300)) 
	gamedisplays.blit(yellow_strip, (400, 400)) 
	gamedisplays.blit(yellow_strip, (400, 500)) 
	gamedisplays.blit(strip, (120, 0)) 
	gamedisplays.blit(strip, (120, 100)) 
	gamedisplays.blit(strip, (120, 200)) 
	gamedisplays.blit(strip, (680, 0)) 
	gamedisplays.blit(strip, (680, 100)) 
	gamedisplays.blit(strip, (680, 200)) 


def car(x, y): 
	gamedisplays.blit(carimg, (x, y)) 


def game_loop(): 
	global pause 
	x = (display_width*0.45) 
	y = (display_height*0.8) 
	x_change = 0
	obstacle_speed = 9
	obs = 0
	y_change = 0
	obs_startx = random.randrange(200, 
								(display_width-200)) 
	obs_starty = -750
	obs_width = 56
	obs_height = 125
	passed = 0
	level = 0
	score = 0
	y2 = 7
	fps = 120

	bumped = False
	while not bumped: 
		for event in pygame.event.get(): 
			if event.type == pygame.QUIT: 
				pygame.quit() 
				quit() 

			if event.type == pygame.KEYDOWN: 
				if event.key == pygame.K_LEFT: 
					x_change = -5
				if event.key == pygame.K_RIGHT: 
					x_change = 5
				if event.key == pygame.K_a: 
					obstacle_speed += 2
				if event.key == pygame.K_b: 
					obstacle_speed -= 2
			if event.type == pygame.KEYUP: 
				if event.key == pygame.K_LEFT: 
					x_change = 0
				if event.key == pygame.K_RIGHT: 
					x_change = 0

		x += x_change 
		pause = True
		gamedisplays.fill(gray) 

		rel_y = y2 % backgroundpic.get_rect().width 
		gamedisplays.blit( 
			backgroundpic, (0, 
							rel_y-backgroundpic.get_rect().width)) 
		gamedisplays.blit(backgroundpic, 
						(700, rel_y -
						backgroundpic.get_rect().width)) 
		if rel_y < 800: 
			gamedisplays.blit(backgroundpic, (0, rel_y)) 
			gamedisplays.blit(backgroundpic, (700, rel_y)) 
			gamedisplays.blit(yellow_strip, (400, rel_y)) 
			gamedisplays.blit(yellow_strip, (400, rel_y+100)) 
			gamedisplays.blit(yellow_strip, (400, rel_y+200)) 
			gamedisplays.blit(yellow_strip, (400, rel_y+300)) 
			gamedisplays.blit(yellow_strip, (400, rel_y+400)) 
			gamedisplays.blit(yellow_strip, (400, rel_y+500)) 
			gamedisplays.blit(yellow_strip, (400, rel_y-100)) 
			gamedisplays.blit(strip, (120, rel_y-200)) 
			gamedisplays.blit(strip, (120, rel_y+20)) 
			gamedisplays.blit(strip, (120, rel_y+30)) 
			gamedisplays.blit(strip, (680, rel_y-100)) 
			gamedisplays.blit(strip, (680, rel_y+20)) 
			gamedisplays.blit(strip, (680, rel_y+30)) 

		y2 += obstacle_speed 

		obs_starty -= (obstacle_speed/4) 
		obstacle(obs_startx, obs_starty, obs) 
		obs_starty += obstacle_speed 
		car(x, y) 
		score_system(passed, score) 
		if x > 690-car_width or x < 110: 
			crash() 
		if x > display_width-(car_width+110) or x < 110: 
			crash() 
		if obs_starty > display_height: 
			obs_starty = 0-obs_height 
			obs_startx = random.randrange(170, 
										(display_width-170)) 
			obs = random.randrange(0, 7) 
			passed = passed+1
			score = passed*10
			if int(passed) % 10 == 0: 
				level = level+1
				obstacle_speed+2
				largetext = pygame.font.Font("freesansbold.ttf", 80) 
				textsurf, textrect = text_objects( 
					"LEVEL"+str(level), largetext) 
				textrect.center = ( 
					(display_width/2), (display_height/2)) 
				gamedisplays.blit(textsurf, textrect) 
				pygame.display.update() 
				time.sleep(3) 

		if y < obs_starty+obs_height: 
			if x > obs_startx and x < \ 
					obs_startx + obs_width or x+car_width > \ 
					(obs_startx and x+car_width < obs_startx+obs_width): 
				crash() 
		button("Pause", 650, 0, 150, 50, blue, bright_blue, "pause") 
		pygame.display.update() 
		clock.tick(60) 


intro_loop() 
game_loop() 
pygame.quit() 
quit() 

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1940244.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

百科词条可以删除吗?删除百科词条的方法

大多时候大家都是想创建百度词条&#xff0c;然而有时候也会需要删除某些词条&#xff0c;因为其内容有错误、不实或者涉及某些敏感信息。但是百科词条删除需要非常明确的理由&#xff0c;不然也是很难通过的&#xff0c;这里小马识途百科顾问先初步分享下删除百科词条的流程。…

一套C#语言开发的医学影像归档与通讯系统PACS源码,三甲以下医院都能满足

医学影像归档与通讯系统&#xff08;PACS&#xff09;系统&#xff0c;是一套适用于从单一影像设备到放射科室、到全院级别等各种应用规模的医学影像归档与通讯系统。PACS集患者登记、图像采集、存档与调阅、报告与打印、查询、统计、刻录等功能为一体&#xff0c;有效地实现了…

Logstash docker发布

一 下载Logstash 不废话了&#xff0c;我下载的7.17.6 二 新增配置文件 在logstash/pipeline中&#xff0c;添加logstash.conf input {jdbc { # 连接jdbc_connection_string > "jdbc:mysql://192.168.1.1:3306/kintech-cloud-bo&#xff1f;characterEncodingUTF-8&…

【Linux网络】套接字编程

本篇博客整理了 socket 套接字编程的相关内容&#xff0c;包括 socket 网络通信原理、socket 相关的系统调用接口等&#xff0c;分别演示了基于UDP协议、TCP协议的 socket 网络编程&#xff0c;旨在让读者更加深入理解网络通信原理和设计&#xff0c;对网络编程有初步的认识和掌…

ECCV2024中有哪些值得关注的扩散模型相关的工作?

Diffusion Models专栏文章汇总:入门与实战 The Fabrication of Reality and Fantasy: Scene Generation with LLM-Assisted Prompt Interpretation 本文探讨了如何利用扩散模型生成需要艺术创造力或专业知识的复杂和富有想象力的图像提示。提出了一个新颖的评估框架RealisticF…

VulnHub:insomnia

靶机下载地址 信息收集 主机发现和端口扫描 攻击机网段192.168.31.0/24。 # 主机发现 nmap 192.168.31.0/24 -Pn -T4 # 靶机ip:192.168.31.207 端口扫描 nmap 192.168.31.207 -A -p- -T4 经过nmap扫描发现目标主机有http服务&#xff0c;端口是8080。 目录扫描 访问http…

Android RSA 加解密

文章目录 一、RSA简介二、RSA 原理介绍三、RSA 秘钥对生成1. 密钥对生成2. 获取公钥3. 获取私钥 四、PublicKey 和PrivateKey 的保存1. 获取公钥十六进制字符串1. 获取私钥十六进制字符串 五、PublicKey 和 PrivateKey 加载1. 加载公钥2. 加载私钥 六、 RSA加解密1. RSA 支持三…

YOLOv2小白精讲

YOLOv2是一个集成了分类和检测任务的神经网络&#xff0c;它将目标检测和分类任务统一在一个单一的网络中进行处理。 本文在yolov1的基础上&#xff0c;对yolov2的网络结构和改进部分进行讲解。yolov1的知识点可以看我另外一篇博客&#xff08;yolov1基础精讲-CSDN博客&#xf…

MySQL的索引、事务

MySQL的索引 索引的概念 索引是一个排序的列表&#xff0c;在列表当中存储索引的值以及索引值对应数据所在的物理行。 索引值和数据是一一映射的关系。 索引的作用 使用索引之后就不需要扫描全表来定位某行的数据 加快数据库查询的速度。 索引可以是表中的一列也可以是多…

Dify中接入GPT-4o mini模型

GPT-4o mini模型自己承认是基于GPT-3.5架构的模型&#xff0c;有图有真相&#xff1a; 一.GPT-4o mini官网简介 GPT-4o mini&#xff08;“o"代表"omni”&#xff09;是小型型号类别中最先进的型号&#xff0c;也是OpenAI迄今为止最便宜的型号。它是多模态的&#x…

C++11: auto 关键字

目录 **前言****1. 推导规则****2. 不能使用 auto 的场景****3. 常见的使用场景** 前言 在 C11 以前&#xff0c;auto 关键字的语义继承自 C 语言&#xff0c;表示 进入块后&#xff0c;自动分配内存&#xff0c;即分配堆栈内存。也就是说 auto 只能用于函数内&#xff0c;然而…

昇思25天学习打卡营第14天|基于MindNLP+MusicGen生成自己的个性化音乐

MusicGen是由Meta AI的团队开发出的一种音乐生成模型&#xff0c;它用一个语言模型来根据文本描述或音频提示制作音乐。这个模型分三步工作&#xff1a;首先&#xff0c;把用户给的文本转换成一系列状态&#xff1b;然后&#xff0c;用这些状态来预测音乐的音频token&#xff1…

[HTML]一文掌握

背景知识 主流浏览器 浏览器是展示和运行网页的平台&#xff0c; 常见的五大浏览器有 IE浏览器、火狐浏览器&#xff08;Firefox&#xff09;、谷歌浏览器&#xff08;Chrome&#xff09;、Safari浏览器、欧朋浏览器&#xff08;Opera&#xff09; 渲染引擎 浏览器解析代码渲…

【Linux 15】进程间通信的方式 - 管道

文章目录 &#x1f308; 一、管道介绍&#x1f308; 二、匿名管道⭐ 1. 匿名管道的概念⭐ 2. 匿名管道的创建⭐ 3. 匿名管道的本质⭐ 4. 匿名管道的使用⭐ 5. 匿名管道的特点⭐ 6. 匿名管道的大小 &#x1f308; 三、命名管道⭐ 1. 命名管道的概念⭐ 2. 命名管道的创建⭐ 3. 命…

USB转多路UART - USB 基础

一、 前言 断断续续做了不少USB相关开发&#xff0c;但是没有系统去了解过&#xff0c;遇到问题就很被动了。做这个USB转UART的项目就是&#xff0c;于是专门花了一天的时间学习USB及CDC相关&#xff0c;到写这文章时估计也忘得差不多了&#xff0c;趁项目收尾阶段记录一下&am…

OLAP引擎之Kylin

Apache Kylin 是一个开源的分布式分析引擎&#xff0c;设计用于在大数据环境中实现极快的在线分析处理 (OLAP) 查询。它主要用于解决大数据分析中的性能问题&#xff0c;并为大规模数据提供交互式的查询体验。Kylin 是由 eBay 于 2014 年开源的&#xff0c;现由 Apache 基金会管…

某直聘每日算法变更分析

带大家分析一下每日算法的变更情况如何破解&#xff1a; 1. 找到算法起始点 前面都是字符串的拼接&#xff0c;可以不用管&#xff0c;重点我们看数组的操作 af0d473b.js:11950 S: 191 e: 3 : af0d473b.js:11326 gl: 1 G: 1 : af0d473b.js:11950 S: 60 e: 3 : af0d473b.js:113…

Java 写一个可以持续发送消息的socket服务端

前言 最近在学习flink, 为了模仿一个持续的无界的数据源, 所以需要一个可以持续发送消息的socket服务端. 先上效果图 效果图 socket服务端可以持续的发送消息, flink端是一个统计单词出现总数的消费端,效果图如下 源代码 flink的消费端就不展示了, 需要引入一些依赖和版本…

多态性概念 OOPS

大家好&#xff01;今天&#xff0c;我们将探讨面向对象编程 (OOP) 中的一个基本概念 - 多态性。具体来说&#xff0c;我们将重点介绍其三种主要形式&#xff1a;方法重载、方法覆盖和方法隐藏。对于任何使用 OOP 语言&#xff08;例如 C#&#xff09;的程序员来说&#xff0c;…

嵌入式人工智能(17-基于树莓派4B的电机控制-伺服电机SG90)

伺服电机主要适用于角度需要不断变化且可以保持的控制系统&#xff0c;常见的机械臂、多足机器人、遥 控船、摄像头云台等都可以使用伺服电机来实现。 1、简介 伺服电动机又被称为执行电动机、舵机&#xff0c;如图9.4所示&#xff0c;是由直流电机、减速齿轮组、电位器和控制…