大家好,我是Imanuel,这是我原创的学习笔记,用费曼学习法自己提出问题,自己研究讲给网友们听,希望可以和大家一起学习进步。
第三课的开头首先是上次绿草作业的探讨。
导入绿草并不困难,但困难的是绿草元素y坐标的确定,绿草的最右边实际上是在窗口的最右边,能沟通过screen_rect.right与grass_rect.right的相对位置确定,但y坐标的却依赖我们还没创建的地面。 所以在创建绿草之前,我们先创建地面吧!
4. 创建地面
仔细观察不难发现地面是由重复元素构成的,我们将以下重复元素命名为ground存入images文件夹中,然后进行编码。
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
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()
运行程序,我们得到:
用for循坏嵌套if条件,我们就能在地板上很轻松的开出超级玛丽游戏里经典的缺口。 如我们想跳过第10,11,12这三块地板制造缺口,只需要运用if条件进行设置
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):
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()
结果如下:
3.创建绿草
创建完地板,我们就能很轻松创建出绿草了。
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
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()
运行结果如下
5,6构建漂浮方块和问号方块
和构建地板一样,我们导入图片素材后可用for语言嵌套if条件语句进行运作。 由于是入门,暂且不涉及关卡设计等技巧。我们现在只大致放置好这两个元素的位置,不做关卡设计的扩展。
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()
运行结果
本期回顾
第三课我们学习了: 1 如何用相对关系定位元素 2 如何用for循环嵌套if语句实现元素复用
并使用这两个知识点解决了: 1 绿草的定位 2 地板,砖块,问号的复用 万事俱备,只欠超级玛丽本人,第四课,我们将导入超级玛丽本人,并控制他进行移动,跳跃,与环境进行互动,敬请期待哦。
|