首页 » 网站推广 » phpsprite技巧_从0开始学python第146节 sprite上

phpsprite技巧_从0开始学python第146节 sprite上

访客 2024-11-20 0

扫一扫用手机浏览

文章目录 [+]

游戏开拓最核心的便是碰撞检测了,子弹击中仇敌、足球射进门、吃加血包这些都是通过碰撞检测完成的。
我们这节课学习的sprite模块对pygame的碰撞做了初步封装,简化我们开拓这些模块时的代码。
在pygame里,sprite常日是一个二维的图片。
比如一辆汽车、一个狐狸、一条小狗等。
下面我们就来详细学习一下sprite模块。

prite根本和碰撞检测

让我们来看一个利用sprite的例子,这个例子展示了一个红豆吃黑豆的游戏,屏幕上的红豆会随着我们的鼠标移动,红豆碰到黑豆后会把黑豆吃掉。
游戏效果如下图所示:项目代码可以访问下面的链接:ProgramArcadeGames.com/python_examples/f.php?file=sprite_collect_blocks.py下面我们就来详细剖析一下这个代码。

phpsprite技巧_从0开始学python第146节 sprite上

import pygameimport random# Define some colorsBLACK = ( 0, 0, 0)WHITE = (255, 255, 255)RED = (255, 0, 0)

首先,我们引入了pygame。
并定义了黑、白、后三个颜色常量。

phpsprite技巧_从0开始学python第146节 sprite上
(图片来自网络侵删)

class Block(pygame.sprite.Sprite): \"大众\"大众\公众 This class represents the ball. It derives from the \公众Sprite\公众 class in Pygame. \"大众\公众\"大众

我们定义一个Block类,这个类继续了:pygame.sprite.Sprite
因此,这个类具有了sprite类的所有属性和方法。

def __init__(self, color, width, height): \"大众\"大众\公众 Constructor. Pass in the color of the block, and its x and y position. \"大众\"大众\公众 # Call the parent class (Sprite) constructor super().__init__()

在Block类的init方法里,我们传入了颜色、宽度、高度三个属性。
这里须要把稳的是,我们通过super().__init__()调用了父类的初始化方法来初始化sprite的属性。

# Create an image of the block, and fill it with a color.# This could also be an image loaded from the disk.self.image = pygame.Surface([width, height])self.image.fill(color)

接下来,将sprite的image属性设置为一个指定宽高的空图片,并把它添补为指定的颜色。
这个类的整体初始化代码如下:

def __init__(self, color, width, height): \"大众\"大众\"大众 Ellipse Constructor. Pass in the color of the ellipse, and its size \公众\公众\"大众 # Call the parent class (Sprite) constructor super().__init__() # Set the background color and set it to be transparent self.image = pygame.Surface([width, height]) self.image.fill(WHITE) self.image.set_colorkey(WHITE) # Draw the ellipse pygame.draw.ellipse(self.image, color, [0, 0, width, height]) # Fetch the rectangle object that has the dimensions of the image # Update the position of this object by setting the values # of rect.x and rect.y self.rect = self.image.get_rect()

在这个布局方法里,一定要把稳末了一行的self.rect = self.image.get_rect()来初始化sprite的rect属性。
rect属性是pygame里Rect类的一个实例。
这个矩形表示了sprite工具的二维边界,Rect里有x,y两个关键的可修正属性。
pygame会把sprite画的屏幕的(x,y)上。
以是,移动一个sprite便是变动sprite的rect.x,rect.y属性。

# Initialize Pygamepygame.init() # Set the height and width of the screenscreen_width = 700screen_height = 400screen = pygame.display.set_mode([screen_width, screen_height])

完成Block类的定义后,我们来初始化pygame。

# This is a list of 'sprites.' Each block in the program is# added to this list.# The list is managed by a class called 'Group.'block_list = pygame.sprite.Group() # This is a list of every sprite.# All blocks and the player block as well.all_sprites_list = pygame.sprite.Group()

利用sprites的紧张好处是我们可以把游戏里的所有角色在一个组里做统一处理。
巴塔木放大哦一个统一的组里后,我们可以同时渲染、移动他们。
我们还可以检测一个角色是否和组里的任何一个角色发生碰撞。
上面的代码里我们定义了两个group,all_sprites_list用来存储所有的角色,这个组用来渲染游戏里的所有角色。
block_list用来存储游戏里的碰撞目标。

for i in range(50): # This represents a block block = Block(BLACK, 20, 15) # Set a random location for the block block.rect.x = random.randrange(screen_width) block.rect.y = random.randrange(screen_height) # Add the block to the list of objects block_list.add(block) all_sprites_list.add(block)

接下来,我们利用for循环来初始化玄色的块,这里把稳,我们我们利用random来讲每个block随机到窗口的不同位置上。
然后,我们把所有玄色的块放到block_listall_sprites_list里。

# Create a RED player blockplayer = Block(RED, 20, 15)all_sprites_list.add(player)

接下来,我们初始化赤色的块。

# Loop until the user clicks the close button.done = False # Used to manage how fast the screen updatesclock = pygame.time.Clock() score = 0 # -------- Main Program Loop -----------while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True # Clear the screen screen.fill(WHITE)

角色初始化完成后,我们进入游戏的主循环。
我们定义了一个score变量来存储我们的游戏得分,同时,我们把屏幕设置为白色。

# Get the current mouse position. This returns the position# as a list of two numbers.pos = pygame.mouse.get_pos() # Fetch the x and y out of the list,# just like we'd fetch letters out of a string.# Set the player object to the mouse locationplayer.rect.x = pos[0]player.rect.y = pos[1]

我们利用mouse.get_pos()获取鼠标位置工具,然后把player.rect的x y属性设置为鼠标的横纵坐标。

# See if the player block has collided with anything.blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)

移动完赤色块后,我们利用pygame.sprite.spritecollide方法来检讨赤色块是否和玄色块发生碰撞。
第一个参数传入检测工具,第二个参数传入检测目标。
第三个参数代表了如果group里的元素和player碰撞后,是否移除这个元素。

# Check the list of collisions.for block in blocks_hit_list: score +=1 print(score)

获取碰撞的块后,我们利用for循环来增加得分,并打印得分。

# Draw all the spitesall_sprites_list.draw(screen)

碰撞检测完成后,我们重新将所有角色渲染到screen上。
group工具里有个draw方法,这个方法会循环group里的每个sprite,并调用sprite的draw方法。
这样,我们只须要利用一行代码就可以将所有sprite渲染到screen上了。

# Limit to 60 frames per second clock.tick(60) # Go ahead and update the screen with what we've drawn. pygame.display.flip()pygame.quit()

末了,我们设置游戏帧率为60,调用display的flip方法来重新渲染全体屏幕。
在主循环结束后,我们调用pygame的quit方法来结束游戏。

阿达老师-孩子身边的编程专家

完全课程请关注阿达老师,主页里有完全的课程目录和不雅观看地址

标签:

相关文章

介绍直播新纪元,轻松进入直播的五大步骤

随着互联网技术的飞速发展,直播行业在我国逐渐崛起,越来越多的人选择通过直播这一新兴媒介展示自己、分享生活、传递价值。对于许多新手来...

网站推广 2025-01-03 阅读1 评论0

介绍相机美颜原理,科技与美学的完美结合

随着科技的发展,智能手机的摄像头功能日益强大,美颜相机成为了许多人拍照的首选。美颜相机不仅满足了人们对于美的追求,更在视觉上给人带...

网站推广 2025-01-03 阅读1 评论0

介绍磁铁的制造,科学与艺术的完美结合

磁铁,一种神秘的物质,自古以来就吸引了无数人的目光。它不仅具有独特的磁性,还能在工业、医疗、科研等领域发挥重要作用。磁铁是如何制造...

网站推广 2025-01-03 阅读1 评论0

介绍电瓶激活方法,让电池焕发新生

随着科技的不断发展,电动汽车逐渐成为人们出行的首选。而电瓶作为电动汽车的核心部件,其性能直接影响着车辆的续航里程和行驶体验。新购买...

网站推广 2025-01-03 阅读1 评论0