Esempio n. 1
0
class Chip :
    def __init__(self, pos, value, img) :
        self.x = pos[0]
        self.y = pos[1]
        
        self.value = value
        self.img = img

        self.w = 60

    def inside(self, x, y) :
        if (self.x - x) ** 2 + (self.y - y) ** 2 < self.w ** 2 :
            return True
        
        return False

    def set_vector(self, x1, y1) :
        self.pos = Vector(self.x, self.y)
        self.vel = Vector(0, 0)
        self.acc = Vector(0, 0)
        self.target = Vector(x1, y1)

        self.max_speed = 25
        self.max_force = 15

    def update(self) :
        self.pos.add(self.vel)
        self.vel.add(self.acc)
        self.acc.mult(0)

        self.x = int(self.pos.x)
        self.y = int(self.pos.y)

    def move(self) :
         
        diff_vec = self.pos.subtract(self.target)
        if diff_vec.mag < 2 :
            return False
        
        arrive = self.arrive()
        # arrive.mult(1)
        self.applyForce(arrive)

        return True

    def arrive(self) :

        desired = self.target.subtract(self.pos)

        d = desired.mag
        #speed = self.max_speed
        #if d < 100 :
        speed = self.max_speed * d / 100

        desired.setMag(speed)

        steer = desired.subtract(self.vel)
        if steer.mag > self.max_force :
            steer.setMag(self.max_force)

        return steer

    def applyForce(self, force) :
        self.acc.add(force)

    def show(self) :
        img = pygame.transform.scale(self.img, (self.w, self.w))
        win.blit(img, (self.x, self.y))
Esempio n. 2
0
class Card :
    def __init__(self, pos, num, suit, img) :
        self.x = pos[0]
        self.y = pos[1]
        
        self.num = num
        self.suit = suit
        self.img = img

        if self.num == 1 :
            self.val = 14
        else :
            self.val = self.num
        
        self.w = 120
        self.h = self.w * 3 // 2
        self.cut = self.w // 20

        self.front = False

    def set_vector(self, x1, y1) :
        self.pos = Vector(self.x, self.y)
        self.vel = Vector(0, 0)
        self.acc = Vector(0, 0)
        self.target = Vector(x1, y1)

        self.max_speed = 15
        self.max_force = 8

    def update(self) :
        self.pos.add(self.vel)
        self.vel.add(self.acc)
        self.acc.mult(0)

        self.x = int(self.pos.x)
        self.y = int(self.pos.y)

    def move(self) :
         
        diff_vec = self.pos.subtract(self.target)
        if diff_vec.mag < 2 :
            return False
        
        arrive = self.arrive()
        # arrive.mult(1)
        self.applyForce(arrive)

        return True

    def arrive(self) :

        desired = self.target.subtract(self.pos)

        d = desired.mag
        #speed = self.max_speed
        #if d < 100 :
        speed = self.max_speed * d / 100

        desired.setMag(speed)

        steer = desired.subtract(self.vel)
        if steer.mag > self.max_force :
            steer.setMag(self.max_force)

        return steer

    def applyForce(self, force) :
        self.acc.add(force)

    def show(self) :

        if self.front :
            pygame.draw.rect(win, WHITE, (self.x, self.y + self.cut, self.w, self.h - 2*self.cut))
            pygame.draw.rect(win, WHITE, (self.x + self.cut, self.y, self.w - 2*self.cut, self.h))
            pygame.draw.circle(win, WHITE, (self.x + self.cut, self.y + self.cut), self.cut)
            pygame.draw.circle(win, WHITE, (self.x + self.w - self.cut, self.y + self.cut), self.cut)
            pygame.draw.circle(win, WHITE, (self.x + self.cut, self.y + self.h - self.cut), self.cut)
            pygame.draw.circle(win, WHITE, (self.x + self.w - self.cut, self.y + self.h - self.cut), self.cut)
            
            img = pygame.transform.scale(self.img, (self.w, self.h))
            win.blit(img, (self.x, self.y))

        else :
            img = pygame.transform.scale(card_back, (self.w, self.h))
            win.blit(img, (self.x, self.y))
Esempio n. 3
0
class Vehicle:
    def __init__(self, x, y, x0, y0):
        self.pos = Vector(x, y)
        self.vel = Vector(0, 0)
        self.acc = Vector(0, 0)
        self.target = Vector(x0, y0)
        self.r = 4
        # self.color = (255,255,255)
        self.color = (random.randint(0, 255), random.randint(0, 255),
                      random.randint(0, 255))
        self.max_speed = 10
        self.max_force = 1

    def update(self):
        self.pos.add(self.vel)
        self.vel.add(self.acc)
        self.acc.mult(0)

    def behaviors(self):
        arrive = self.arrive()
        flee = self.flee()

        arrive.mult(1)
        flee.mult(5)

        self.applyForce(arrive)
        self.applyForce(flee)

    def flee(self):
        mouse = Vector(mouseX, mouseY)

        desired = mouse.subtract(self.pos)
        d = desired.mag

        if d < 50:
            desired.setMag(self.max_speed)
            desired.mult(-1)

            steer = desired.subtract(self.vel)
            if steer.mag > self.max_force:
                steer.setMag(self.max_force)

            self.color = (random.randint(0, 255), random.randint(0, 255),
                          random.randint(0, 255))
            return steer

        else:
            return Vector(0, 0)

    def arrive(self):
        desired = self.target.subtract(self.pos)

        d = desired.mag
        #speed = self.max_speed
        #if d < 100 :
        speed = self.max_speed * d / 100

        desired.setMag(speed)

        steer = desired.subtract(self.vel)
        if steer.mag > self.max_force:
            steer.setMag(self.max_force)

        return steer

    def applyForce(self, f):
        self.acc.add(f)

    def show(self):
        # x = floor(self.target.x)
        # y = floor(self.target.y)
        # pygame.draw.circle(win, (0, 255, 0), (x, y), self.r + 4)

        x = floor(self.pos.x)
        y = floor(self.pos.y)
        pygame.draw.circle(win, self.color, (x, y), self.r)