Python_pygame library study notes (3): use relative relationship to locate elements, use for loop nested if conditional statement to reuse elements

Hello everyone, I am Imanuel, and this is my original study notes. I use the Feynman learning method to ask questions by myself, research and explain to netizens. I hope that I can learn and progress together with you.

The beginning of the third lesson is the discussion of the last green grass homework.

It is not difficult to import green grass, but the difficult thing is to determine the y coordinate of the green grass element. The far right of the green grass is actually the far right of the window, which can be determined by the relative position of screen_rect.right and grass_rect.right, but y The coordinates depend on the ground which we haven't created yet.
So before creating the green grass, let's create the ground first!

4. Create the ground

After careful observation, it is not difficult to find that the ground is composed of repeated elements. We name the following repeated elements as ground and store them in the images folder, and then encode them.

import sys
import pygame

pygame.init
screen = pygame.display.set_mode((1200,600))
pygame.display.set_caption(('超级玛丽'))
cloud_img = pygame.image.load('images/cloud.jpg')

'''↓导入ground,通过get_rect()方法获得其矩形用于定位'''
ground_img = pygame.image.load('images/ground.jpg')
ground_img_rect = ground_img.get_rect()
'''↓通过get_rect()方法获得screen矩形用于定位'''
screen_rect = screen.get_rect()
'''↓获得ground的数量,
因为要用于for函数的range以复用素材,所以将其转为整数
因为地板铺满了画面边缘,所以我们在用屏幕宽/素材宽的得出其数量后+1以让画面铺满'''
ground_num = int(screen_rect.width / ground_img_rect.width) + 1

while True:	
	for event in pygame.event.get():		
		if event.type == pygame.QUIT:			
			sys.exit()	
	screen.fill((121,178,250))
	screen.blit(cloud_img,(0,0))
	'''↓刷新地板让其出现
	这里我们用for循环复用地板素材'''
	for ground in range(ground_num):
		new_ground = ground_img#新建一块地板
		new_ground_rect = ground_img_rect#获取新地板的rect以定位新地板
		new_ground_rect.bottom = screen_rect.bottom#定位新地板的底部与屏幕底部一致
		new_ground_rect.x = ground * new_ground_rect.width#定位新地板的x轴为循环数*地板宽
		screen.blit(new_ground,new_ground_rect)#让新地板出现
	pygame.display.flip()

Running the program, we get:
insert image description here

Using the for loop to nest the if condition, we can easily open the classic gap in the Super Mario game on the floor.
If we want to skip the 10th, 11th, and 12th floors to make gaps, we only need to use the if condition to set

import sys
import pygame

pygame.init
screen = pygame.display.set_mode((1200,600))
pygame.display.set_caption(('超级玛丽'))

cloud_img = pygame.image.load('images/cloud.jpg')

ground_img = pygame.image.load('images/ground.jpg')
ground_img_rect = ground_img.get_rect()
screen_rect = screen.get_rect()
ground_num = int(screen_rect.width / ground_img_rect.width) + 1

while True:	
	for event in pygame.event.get():		
		if event.type == pygame.QUIT:			
			sys.exit()	
	screen.fill((121,178,250))
	screen.blit(cloud_img,(0,0))
	
	for ground in range(ground_num):
		#用条件函数跳过第10,11,12块地板
		if ground == 9 or ground == 10 or ground == 11:
			continue
		else:
			new_ground = ground_img
			new_ground_rect = ground_img_rect
			new_ground_rect.bottom = screen_rect.bottom
			new_ground_rect.x = ground * new_ground_rect.width
			screen.blit(new_ground,new_ground_rect)

	pygame.display.flip()

The result is as follows:
insert image description here

3. Create the Green Grass

After creating the floor, we can easily create the green grass.

import sys
import pygame

pygame.init
screen = pygame.display.set_mode((1200,600))
pygame.display.set_caption(('超级玛丽'))
cloud_img = pygame.image.load('images/cloud.jpg')

ground_img = pygame.image.load('images/ground.jpg')
ground_img_rect = ground_img.get_rect()
screen_rect = screen.get_rect()
ground_num = int(screen_rect.width / ground_img_rect.width) + 1
'''↓导入grass,并利用rect定位'''
grass_img = pygame.image.load('images/grass.jpg')
grass_img_rect = grass_img.get_rect()
grass_img_rect.right = screen_rect.right
#记得上次的知识点吗?screen左上角坐标为0,0,所以要得出草的坐标,需要用屏幕的高-地板的高
grass_img_rect.bottom = screen_rect.height - ground_img_rect.height

while True:
	for event in pygame.event.get():		
		if event.type == pygame.QUIT:			
			sys.exit()	
			
	screen.fill((121,178,250))
	screen.blit(cloud_img,(0,0))
	
	for ground in range(ground_num):
		if ground == 9 or ground == 10 or ground == 11:
			continue
		else:
			new_ground = ground_img
			new_ground_rect = ground_img_rect
			new_ground_rect.bottom = screen_rect.bottom
			new_ground_rect.x = ground * new_ground_rect.width
			screen.blit(new_ground,new_ground_rect)
	'''↓blit()方法展示草'''
	screen.blit(grass_img,grass_img_rect)

	pygame.display.flip()

The result of the operation is as follows
insert image description here

5, 6 Build floating blocks and question mark blocks

Just like building the floor, we can use the for language to nest the if conditional statement after importing the image material to operate.
Since it is an introduction, it does not involve skills such as level design for the time being. We only roughly place the positions of these two elements now, and do not expand the level design.

import sys
import pygame

pygame.init
screen = pygame.display.set_mode((1200,600))
pygame.display.set_caption(('超级玛丽'))
cloud_img = pygame.image.load('images/cloud.jpg')

ground_img = pygame.image.load('images/ground.jpg')
ground_img_rect = ground_img.get_rect()
screen_rect = screen.get_rect()
ground_num = int(screen_rect.width / ground_img_rect.width) + 1
grass_img = pygame.image.load('images/grass.jpg')
grass_img_rect = grass_img.get_rect()
grass_img_rect.right = screen_rect.right
grass_img_rect.bottom = screen_rect.height - ground_img_rect.height

'''↓由于用坐标定位,暂时只导入,不做rect定位'''
brick_img = pygame.image.load('images/brick.jpg')
qm_brick_img = pygame.image.load('images/qm_brick.jpg')

while True:
	for event in pygame.event.get():		
		if event.type == pygame.QUIT:			
			sys.exit()	
	screen.fill((121,178,250))
	screen.blit(cloud_img,(0,0))
	
	for ground in range(ground_num):
		if ground == 9 or ground == 10 or ground == 11:
			continue
		else:
			new_ground = ground_img
			new_ground_rect = ground_img_rect
			new_ground_rect.bottom = screen_rect.bottom
			new_ground_rect.x = ground * new_ground_rect.width
			screen.blit(new_ground,new_ground_rect)
	screen.blit(grass_img,grass_img_rect)

	'''↓导入显示三个砖块和一个问号'''
	screen.blit(brick_img,(500,400))
	screen.blit(brick_img,(454,400))
	screen.blit(qm_brick_img,(546,400))
	screen.blit(brick_img,(900,400))

	pygame.display.flip(

operation result
insert image description here

Review of this issue

In the third lesson, we learned:
1 How to use relative relationship to locate elements
2 How to use for loop to nest if statement to realize element reuse

And use these two knowledge points to solve:
1. The positioning of the green grass . 2. The reuse of floors,
bricks, and question marks.
Move, jump, and interact with the environment, so stay tuned.

Guess you like

Origin blog.csdn.net/weixin_45980989/article/details/125812653