如何在 PyGame 中添加移动平台
先决条件:在 Pygame 中绘图
在本文中,我们将学习如何使用Python的PyGame 将移动平台添加到我们的游戏中。
创建平台
我们可以使用 draw() 方法在 pygame 中轻松创建任何类型的平台。为此,我们将使用 draw.rect()函数创建具有特定宽度和高度的反应。
Syntax: pygame.draw.rect(surface, color, rect, width)
Parameters:
- surface : Here we can pass the surface on which we want to draw our rectangle.
- color : Here we can pass the color for our rectangle.
- rect : Here we can pass the rectangle, position, and dimensions.
- width : Here we can pass the line thickness. we can also create a solid rectangle by changing the value of this width parameter.
代码:
Python3
# Importing the pygame module
import pygame
from pygame.locals import *
# Initiate pygame and give permission
# to use pygame's functionality
pygame.init()
# Create a display surface object
# of specific dimension
window = pygame.display.set_mode((600,600))
# Creating a new clock object to
# track the amount of time
clock = pygame.time.Clock()
# Starting coordinates of the platform
x = 100
y = 150
# Creating a rect with width
# and height
rect = Rect(x, y, 200, 50)
# Creating a boolean variable that
# we will use to run the while loop
run = True
# Creating an infinite loop
# to run our game
while run:
# Setting the framerate to 30fps
clock.tick(30)
# Drawing the rect on the screen using the
# draw.rect() method
pygame.draw.rect(window, (255, 0, 0),rect)
# Updating the display surface
pygame.display.update()
# Filling the window with white color
window.fill((255,255,255))
Python3
# Importing the pygame module
import pygame
from pygame.locals import *
# Initiate pygame and give permission
# to use pygame's functionality
pygame.init()
# Create a display surface object
# of specific dimension
window = pygame.display.set_mode((600,600))
# Creating a new clock object to
# track the amount of time
clock = pygame.time.Clock()
# Variable to store the
# velocity of the platform
platform_vel = 5
# Starting coordinates of the platform
x = 100
y = 150
# Creating a rect with width
# and height
rect = Rect(x, y, 200, 50)
# Creating a boolean variable that
# we will use to run the while loop
run = True
# Creating an infinite loop
# to run our game
while run:
# Setting the framerate to 30fps
clock.tick(30)
# Multiplying platform_vel with -1
# if its x coordinate is less then 100
# or greater than or equal to 300.
if rect.left >=300 or rect.left<100:
platform_vel*= -1
# Adding platform_vel to x
# coordinate of our rect
rect.left += platform_vel
# Drawing the rect on the screen using the
# draw.rect() method
pygame.draw.rect(window, (255, 0, 0),rect)
# Updating the display surface
pygame.display.update()
# Filling the window with white color
window.fill((255,255,255))
Python3
# Importing the pygame module
import pygame
from pygame.locals import *
# Initiate pygame and give permission
# to use pygame's functionality
pygame.init()
# Create a display surface object
# of specific dimension
window = pygame.display.set_mode((600,600))
# Creating a new clock object to
# track the amount of time
clock = pygame.time.Clock()
# Variable to store the
# velocity of the platform
platform_vel = 5
# Starting coordinates of the platform
x = 100
y = 150
# Starting coordinates for
# player sprite
player_x = 180
player_y = 0
# Creating a new variable
# for gravity
gravity = 8
# Creating a new rect for player
player_rect = Rect(player_x, player_y, 50, 50)
# Creating a rect with width
# and height
rect = Rect(x, y, 200, 50)
# Creating a boolean variable that
# we will use to run the while loop
run = True
# Creating an infinite loop
# to run our game
while run:
# Setting the framerate to 30fps
clock.tick(30)
# Multiplying platform_vel with -1
# if its x coordinate is less then 100
# or greater than or equal to 300.
if rect.left >=300 or rect.left<100:
platform_vel*= -1
# Checking if player is colliding
# with platform or not using the
# colliderect() method.
# It will return a boolean value
collide = pygame.Rect.colliderect(rect, player_rect)
# If player is colliding with
# platform then setting coordinate
# of player bottom equal to top of platorm
# and adding the platform velocity
if collide:
player_rect.bottom = rect.top
player_rect.left += platform_vel
# Adding platform_vel to x
# coordinate of our rect
rect.left += platform_vel
# Adding gravity
player_rect.top += gravity
# Drawing the rect on the screen using the
# draw.rect() method
pygame.draw.rect(window, (255, 0, 0),rect)
# Drawing player rect
pygame.draw.rect(window, (0, 255, 0),player_rect)
# Updating the display surface
pygame.display.update()
# Filling the window with white color
window.fill((255,255,255))
输出:
移动平台
为了移动平台,我们可以创建一个带有一些数值的速度变量,我们可以将该速度添加到我们平台的 x 坐标上。之后,如果速度变量的 x 坐标小于 100 或大于或等于 300,我们将乘以 -1。
代码:
蟒蛇3
# Importing the pygame module
import pygame
from pygame.locals import *
# Initiate pygame and give permission
# to use pygame's functionality
pygame.init()
# Create a display surface object
# of specific dimension
window = pygame.display.set_mode((600,600))
# Creating a new clock object to
# track the amount of time
clock = pygame.time.Clock()
# Variable to store the
# velocity of the platform
platform_vel = 5
# Starting coordinates of the platform
x = 100
y = 150
# Creating a rect with width
# and height
rect = Rect(x, y, 200, 50)
# Creating a boolean variable that
# we will use to run the while loop
run = True
# Creating an infinite loop
# to run our game
while run:
# Setting the framerate to 30fps
clock.tick(30)
# Multiplying platform_vel with -1
# if its x coordinate is less then 100
# or greater than or equal to 300.
if rect.left >=300 or rect.left<100:
platform_vel*= -1
# Adding platform_vel to x
# coordinate of our rect
rect.left += platform_vel
# Drawing the rect on the screen using the
# draw.rect() method
pygame.draw.rect(window, (255, 0, 0),rect)
# Updating the display surface
pygame.display.update()
# Filling the window with white color
window.fill((255,255,255))
输出:
添加玩家精灵和碰撞
现在我们要添加我们的播放器和我们的播放器和平台之间的碰撞。为此,我们使用 colliderect() 方法。
Syntax: pygame.Rect.colliderect(rect1 , rect2)
Parameters: It will take two rects as its parameters.
Returns true if any portion of either rectangle overlap.
如果玩家与平台发生碰撞,那么我们将玩家底部的坐标设置为平台的顶部,然后我们将添加平台速度。我们还创建了一个重力变量。
代码:
蟒蛇3
# Importing the pygame module
import pygame
from pygame.locals import *
# Initiate pygame and give permission
# to use pygame's functionality
pygame.init()
# Create a display surface object
# of specific dimension
window = pygame.display.set_mode((600,600))
# Creating a new clock object to
# track the amount of time
clock = pygame.time.Clock()
# Variable to store the
# velocity of the platform
platform_vel = 5
# Starting coordinates of the platform
x = 100
y = 150
# Starting coordinates for
# player sprite
player_x = 180
player_y = 0
# Creating a new variable
# for gravity
gravity = 8
# Creating a new rect for player
player_rect = Rect(player_x, player_y, 50, 50)
# Creating a rect with width
# and height
rect = Rect(x, y, 200, 50)
# Creating a boolean variable that
# we will use to run the while loop
run = True
# Creating an infinite loop
# to run our game
while run:
# Setting the framerate to 30fps
clock.tick(30)
# Multiplying platform_vel with -1
# if its x coordinate is less then 100
# or greater than or equal to 300.
if rect.left >=300 or rect.left<100:
platform_vel*= -1
# Checking if player is colliding
# with platform or not using the
# colliderect() method.
# It will return a boolean value
collide = pygame.Rect.colliderect(rect, player_rect)
# If player is colliding with
# platform then setting coordinate
# of player bottom equal to top of platorm
# and adding the platform velocity
if collide:
player_rect.bottom = rect.top
player_rect.left += platform_vel
# Adding platform_vel to x
# coordinate of our rect
rect.left += platform_vel
# Adding gravity
player_rect.top += gravity
# Drawing the rect on the screen using the
# draw.rect() method
pygame.draw.rect(window, (255, 0, 0),rect)
# Drawing player rect
pygame.draw.rect(window, (0, 255, 0),player_rect)
# Updating the display surface
pygame.display.update()
# Filling the window with white color
window.fill((255,255,255))
输出: