collidepoint functions for pygame rects

collidepoint functions for pygame rects

Postby jedi_mind_ » Fri Jun 27, 2014 6:52 pm

Code: Select all
import pygame, sys, random
from pygame.locals import *

WINDOWWIDTH = 800
WINDOWHEIGHT = 600
BOXSIZE = 50
GAPSIZE = 10
FPS = 30
SPEED = 8
XMARGIN = 70
YMARGIN = 65

#        R    G     B
GRAY   =(100, 100, 100)
NAVYBLUE =(60, 60, 100)
WHITE   =(255, 255, 255)
RED    =(255,   0,   0)
GREEN   =(  0, 255,   0)
BLUE   =(   0,     0, 255)
YELLOW    =(255, 255,     0)
ORANGE   =(255, 128,   0)
PURPLE   =(255,    0, 255)
CYAN   =(  0, 255, 255)

#Direction
UP = (1, 0, 0, 0)
DOWN = (0, 1, 0, 0)
LEFT = (0, 0, 1, 0)
RIGHT = (0, 0, 0, 1)
UPLEFT = (1, 0, 1, 0)
UPRIGHT = (1, 0, 0, 1)
DOWNLEFT = (0, 1, 1, 0)
DOWNRIGHT = (0, 1, 0, 1)

def Main():
   pygame.init()
   global FPSCLOCK, screen
   screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
   FPSCLOCK = pygame.time.Clock()

   #Not needed yet
   mousey = 0
   mousex = 0

   rects = make_rects()
   while True:
      mouseClicked = False
      screen.fill(WHITE)
      move_rects(rects, mouse = (mousex, mousey))
      draw_rects(rects)
      for event in pygame.event.get():
         if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
            pygame.quit()
            sys.exit()
         elif event.type == MOUSEMOTION:
            mousey, mousex = event.pos
         elif event.type == MOUSEBUTTONUP:
            mouseClicked = True
            mousey, mousex = event.pos

      pygame.display.update()
      FPSCLOCK.tick(FPS)

def make_rects():
   Rects = []
   Rects.append(pygame.Rect(0, 0, BOXSIZE, BOXSIZE))
   Rects.append(pygame.Rect(WINDOWWIDTH - 50, 0, BOXSIZE, BOXSIZE))
   return Rects

def move_rects(rects, mouse = None):
   counter = 0
   for rect in rects:
      if counter == 1:
         if not rect.collidepoint(mouse) and rect.colliderect(screen.get_rect()) and rect.x > 0:
            rect.left += -SPEED
      else:
         if rect.colliderect(screen.get_rect()) and not rect.collidepoint(mouse) and (rect.x + 50) <= WINDOWWIDTH:
            rect.left += SPEED
      counter += 1

def draw_rects(rects):
   pygame.draw.rect(screen, BLUE, rects[0])
   pygame.draw.rect(screen, GREEN, rects[1])

if __name__ == '__main__':
   Main()



in move_rects they're suppose to stop if the mouse is over them. somehow that doesn't work....
jedi_mind_
 
Posts: 11
Joined: Sun Jun 08, 2014 5:31 pm

Re: collidepoint functions for pygame rects

Postby jedi_mind_ » Fri Jun 27, 2014 9:24 pm

Can anyone help?
jedi_mind_
 
Posts: 11
Joined: Sun Jun 08, 2014 5:31 pm

Re: collidepoint functions for pygame rects

Postby Mekire » Sat Jun 28, 2014 9:38 am

Don't bump your question just because it wasn't answered in a couple hours.

You reversed your mouse coordinates.

This:
Code: Select all
mousey, mousex = event.pos
Should be this:
Code: Select all
mousex, mousey = event.pos

Here is some other stuff to think about:
Code: Select all
import sys
import random
import pygame as pg


SCREEN_SIZE = (SCREEN_WIDTH, SCREEN_HEIGHT) = (800, 600)
BOX_SIZE = (50, 50)
FPS = 30
SPEED = 8



class BoxSprite(pg.sprite.Sprite):
    def __init__(self, pos, color, direction, *groups):
        pg.sprite.Sprite.__init__(self, *groups)
        self.direction = direction
        self.rect = pg.Rect(pos, BOX_SIZE)
        self.image = pg.Surface(self.rect.size).convert()
        self.image.fill(color)

    def update(self, mouse, screen_rect):
        if not self.rect.collidepoint(mouse):
            self.rect.x += SPEED*self.direction
        if not screen_rect.contains(self.rect):
            self.direction *= -1
            self.rect.clamp_ip(screen_rect)


def main():
    pg.init()
    screen = pg.display.set_mode(SCREEN_SIZE)
    screen_rect = screen.get_rect()
    clock = pg.time.Clock()
    mouse = pg.mouse.get_pos()
    boxes = make_boxes()
    done = False
    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
                done = True
            elif event.type == pg.MOUSEMOTION:
                mouse = event.pos
        boxes.update(mouse, screen_rect)
        screen.fill(pg.Color("white"))
        boxes.draw(screen)
        pg.display.update()
        clock.tick(FPS)
    pg.quit()
    sys.exit()


def make_boxes():
    boxes = pg.sprite.Group()
    BoxSprite((0,0), pg.Color("blue"), 1, boxes)
    BoxSprite((SCREEN_WIDTH-BOX_SIZE[0],0), pg.Color("green"), -1, boxes)
    return boxes


if __name__ == '__main__':
   main()

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: collidepoint functions for pygame rects

Postby jedi_mind_ » Sat Jun 28, 2014 2:22 pm

Haa, thanks a lot mekire.
jedi_mind_
 
Posts: 11
Joined: Sun Jun 08, 2014 5:31 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest