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()
我有一個藍色的矩形,上升屏幕,一個紅色的矩形,坐在上升的藍色矩形的頂部。在左下角和右下角放置兩個盒子,當它們被點擊時,紅色盒子左右水平移動。我怎樣才能做到這一點,我可以按住鼠標放在其中一個盒子上,矩形將繼續移動,直到我放開去。如何在pygame中獲得連續運動?
'MOUSEBUTTONDOWN'只有當鼠標如果第一次點擊情況,可以檢查是否有'pygame.mouse.get_pressed()'按鈕仍然按住 - 參見[文檔](http://www.pygame.org/docs/ref/mouse.html) – Marius 2015-04-01 01:03:51