如何在 pygame 中获得连续运动?
How can I get continuous motion in pygame?
def function():
import pygame
import time
from pygame.locals import *
pygame.init()
Width = 272
Height = 552
white = 255,255,255
blue = 0,255,255
red = 255,0,0
Left_Rect = pygame.Rect(0,452,135,100)
Right_Rect = pygame.Rect(137,452,135,100)
Location = 136
WaterLevel = 452
Depth = 100
CLOCK = pygame.time.Clock()
FPS = 30
gameDisplay = pygame.display.set_mode((Width,Height))
pygame.display.set_caption('boat game')
stop = False
gameDisplay.fill(white)
while not stop:
####### CONTROLLES ####################################################
pygame.draw.rect(gameDisplay, red, Left_Rect)
pygame.draw.rect(gameDisplay, red, Right_Rect)
####### BOAT #########################################################
pygame.draw.rect(gameDisplay, red, (Location,WaterLevel-20,40,20))
####### WATER ########################################################
pygame.draw.rect(gameDisplay,blue,(0,WaterLevel,272,Depth))
WaterLevel -= 1
Depth += 1
######################################################################
for event in pygame.event.get():
print event
if event.type == pygame.MOUSEBUTTONDOWN:
is_Left = Left_Rect.collidepoint(pygame.mouse.get_pos())
if is_Left == 1:
Location -= 5
if event.type == pygame.MOUSEBUTTONDOWN:
is_Right = Right_Rect.collidepoint(pygame.mouse.get_pos())
if is_Right == 1:
Location += 5
if event.type == pygame.QUIT:
stop = True
pygame.quit()
quit()
CLOCK.tick(FPS)
pygame.display.update()
function()
我有一个蓝色矩形从屏幕上升起,一个红色矩形位于上升的蓝色矩形之上。在左下角和右下角放置两个框,单击它们时,红色框向左或向右水平移动。我怎样才能做到这一点,这样我就可以将鼠标悬停在其中一个框上,矩形将一直移动,直到我放开 Go
有许多不同的方法来解决这个问题。 :)
一个非常简单快速但肮脏的方法是
- 每次
pygame.MOUSEBUTTONDOWN
事件发生时,设置两个名为 is_Left
和 is_Right
的全局变量之一 and各自的.collidepoint()
方法returnstrue
- 当检测到
pygame.MOUSEBUTTONUP
时重置两个变量。
您为此方法更新的代码:
#your original code
#declare global variables is_Left and is_Right
is_Left = False
is_Right = False
while not stop:
#draw controls, boat and water
#increment WaterLevel and Depth
#event loop
for event in pygame.event.get():
#detect MOUSEBUTTONDOWN event and collision
if event.type == pygame.MOUSEBUTTONDOWN:
is_Left = Left_Rect.collidepoint(pygame.mouse.get_pos())
is_Right = Right_Rect.collidepoint(pygame.mouse.get_pos())
if event.type == pygame.MOUSEBUTTONUP:
#reset is_Left and is_Right
is_Left = False
is_Right = False
if event.type == pygame.QUIT:
pygame.quit()
quit()
#change Location
if is_Left:
Location -= 5
elif is_Right:
Location += 5
CLOCK.tick(FPS)
pygame.display.update()
另一种方法是,正如 Marius 已经提到的,使用 PyGames 鼠标模块 的 pygame.mouse.get_pressed()
函数 来获取鼠标按钮的状态.
此函数 returns 一个 tupel
三个布尔值,表示 所有 鼠标按钮的状态。在您的情况下,我们只需要 第一个值,它表示 鼠标左键.
的状态
我们在主游戏循环中调用pygame.mouse.get_pressed()[0]
来获取鼠标左键的状态并在同时如果相应的 .collidepoint()
方法 returns true
:
#your original code
while not stop:
#draw controls, boat and water
#increment WaterLevel and Depth
#event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#get state of left mouse button
isPressed = pygame.mouse.get_pressed()[0]
#change location
if ispressed and Left_Rect.collidepoint(pygame.mouse.get_pos()):
Location -= 5
elif ispressed and Right_Rect.collidepoint(pygame.mouse.get_pos()):
Location += 5
CLOCK.tick(FPS)
pygame.display.update()
CLOCK.tick(FPS)
pygame.display.update()
个人更喜欢第二种变体,因为我们不需要任何额外的全局变量(is_Left
、is_Right
)。
希望对您有所帮助:)
def function():
import pygame
import time
from pygame.locals import *
pygame.init()
Width = 272
Height = 552
white = 255,255,255
blue = 0,255,255
red = 255,0,0
Left_Rect = pygame.Rect(0,452,135,100)
Right_Rect = pygame.Rect(137,452,135,100)
Location = 136
WaterLevel = 452
Depth = 100
CLOCK = pygame.time.Clock()
FPS = 30
gameDisplay = pygame.display.set_mode((Width,Height))
pygame.display.set_caption('boat game')
stop = False
gameDisplay.fill(white)
while not stop:
####### CONTROLLES ####################################################
pygame.draw.rect(gameDisplay, red, Left_Rect)
pygame.draw.rect(gameDisplay, red, Right_Rect)
####### BOAT #########################################################
pygame.draw.rect(gameDisplay, red, (Location,WaterLevel-20,40,20))
####### WATER ########################################################
pygame.draw.rect(gameDisplay,blue,(0,WaterLevel,272,Depth))
WaterLevel -= 1
Depth += 1
######################################################################
for event in pygame.event.get():
print event
if event.type == pygame.MOUSEBUTTONDOWN:
is_Left = Left_Rect.collidepoint(pygame.mouse.get_pos())
if is_Left == 1:
Location -= 5
if event.type == pygame.MOUSEBUTTONDOWN:
is_Right = Right_Rect.collidepoint(pygame.mouse.get_pos())
if is_Right == 1:
Location += 5
if event.type == pygame.QUIT:
stop = True
pygame.quit()
quit()
CLOCK.tick(FPS)
pygame.display.update()
function()
我有一个蓝色矩形从屏幕上升起,一个红色矩形位于上升的蓝色矩形之上。在左下角和右下角放置两个框,单击它们时,红色框向左或向右水平移动。我怎样才能做到这一点,这样我就可以将鼠标悬停在其中一个框上,矩形将一直移动,直到我放开 Go
有许多不同的方法来解决这个问题。 :)
一个非常简单快速但肮脏的方法是
- 每次
pygame.MOUSEBUTTONDOWN
事件发生时,设置两个名为is_Left
和is_Right
的全局变量之一 and各自的.collidepoint()
方法returnstrue
- 当检测到
pygame.MOUSEBUTTONUP
时重置两个变量。
您为此方法更新的代码:
#your original code
#declare global variables is_Left and is_Right
is_Left = False
is_Right = False
while not stop:
#draw controls, boat and water
#increment WaterLevel and Depth
#event loop
for event in pygame.event.get():
#detect MOUSEBUTTONDOWN event and collision
if event.type == pygame.MOUSEBUTTONDOWN:
is_Left = Left_Rect.collidepoint(pygame.mouse.get_pos())
is_Right = Right_Rect.collidepoint(pygame.mouse.get_pos())
if event.type == pygame.MOUSEBUTTONUP:
#reset is_Left and is_Right
is_Left = False
is_Right = False
if event.type == pygame.QUIT:
pygame.quit()
quit()
#change Location
if is_Left:
Location -= 5
elif is_Right:
Location += 5
CLOCK.tick(FPS)
pygame.display.update()
另一种方法是,正如 Marius 已经提到的,使用 PyGames 鼠标模块 的 pygame.mouse.get_pressed()
函数 来获取鼠标按钮的状态.
此函数 returns 一个 tupel
三个布尔值,表示 所有 鼠标按钮的状态。在您的情况下,我们只需要 第一个值,它表示 鼠标左键.
我们在主游戏循环中调用pygame.mouse.get_pressed()[0]
来获取鼠标左键的状态并在同时如果相应的 .collidepoint()
方法 returns true
:
#your original code
while not stop:
#draw controls, boat and water
#increment WaterLevel and Depth
#event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
#get state of left mouse button
isPressed = pygame.mouse.get_pressed()[0]
#change location
if ispressed and Left_Rect.collidepoint(pygame.mouse.get_pos()):
Location -= 5
elif ispressed and Right_Rect.collidepoint(pygame.mouse.get_pos()):
Location += 5
CLOCK.tick(FPS)
pygame.display.update()
CLOCK.tick(FPS)
pygame.display.update()
个人更喜欢第二种变体,因为我们不需要任何额外的全局变量(is_Left
、is_Right
)。
希望对您有所帮助:)