python - How can I get continuous motion in pygame? -
def function(): import pygame import time 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 ###################################################################### 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() i have blue rectangle rises screen, red rectangle sits on top of rising blue rectangle. put 2 boxes on bottom left , right corners , when clicked red box move horizontally left or right. how can make can hold down mouse on 1 of boxes , rectangle keep moving until let go
there many different approaches problem. :)
a simple , quick , dirty method to
- set 1 of 2 global variables called
is_left,is_righteach timepygame.mousebuttondownevent occurs and respective.collidepoint()method returnstrue - reset both variables when
pygame.mousebuttonupdetected.
your updated code approach:
#your original code #declare global variables is_left , is_right is_left = false is_right = false while not stop: #draw controls, boat , water #increment waterlevel , depth #event loop event in pygame.event.get(): #detect mousebuttondown event , 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 , 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() another method is, marius mentioned, use pygame.mouse.get_pressed() function of pygames mouse module to state of mouse buttons.
this function returns tupel of 3 boolean values representing state of all mouse buttons. in case need first value represents state of left mouse button.
we call pygame.mouse.get_pressed()[0] inside main game loop state of left mouse button , check @ same time if respective .collidepoint() method returns true:
#your original code while not stop: #draw controls, boat , water #increment waterlevel , depth #event loop 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 , left_rect.collidepoint(pygame.mouse.get_pos()): location -= 5 elif ispressed , right_rect.collidepoint(pygame.mouse.get_pos()): location += 5 clock.tick(fps) pygame.display.update() clock.tick(fps) pygame.display.update() in prefer second variant, because don´t need global variables (is_left, is_right).
i hope helps :)
Comments
Post a Comment