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()
我有一个蓝色的矩形从屏幕上升起,一个红色的矩形位于上升的蓝色矩形的顶部。在左下角和右下角放置两个盒子,当它们被点击时,红色盒子向左或向右水平移动。我怎样才能做到这一点,所以我可以将鼠标放在其中一个盒子上,矩形将继续移动直到我放开
答案 0 :(得分:0)
该问题有许多不同的方法。 :)
非常简单快捷的方法
is_Left
事件发生时,is_Right
和pygame.MOUSEBUTTONDOWN
和各自的{{ 1}}方法返回.collidepoint()
true
时,此方法的更新代码:
pygame.MOUSEBUTTONUP
正如Marius已经提到的,另一种方法是使用PyGames鼠标模块的pygame.mouse.get_pressed()
函数来获取鼠标按钮的状态。
此函数返回三个布尔值的#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()
,表示所有鼠标按钮的状态。在您的情况下,我们只需要第一个值,它代表鼠标左键的状态。
我们在主游戏循环中调用tupel
以获取鼠标左键的状态,并同时检查相应的{{1} }}方法返回pygame.mouse.get_pressed()[0]
:
.collidepoint()
个人更喜欢第二个变体,因为我们不需要任何额外的全局变量(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()
)。
我希望这会有所帮助:)