starter game 14

The object of this game is to get as many points as you can while trying to box the other players into as small an area as you can. It’s for 2, 3, or 4 players. When the game starts, the “playerSize” parameter lets you select a size from 1 to 8. When you have the size you want, slide the parameter window closed. To move your player, slide your finger in your color scroll box in the direction you want it to go, right, left, up, or down. You don’t have to move it much to move your player. You can only move to a black area. If you run into your trail or scroll box or the other players trail or scroll box, you lose all your points and the remaining player with the highest score wins. The more you move, the more points you get. If no one moves, the countdown timer will start it’s count down. When it reaches 0, the game is over. As long as someone is moving, the countdown timer will remain at 500. When the game ends, double tap the screen to play again. I haven’t played this with anyone, so I’m not sure how well it works.


--# Main

supportedOrientations(LANDSCAPE_ANY)
displayMode(OVERLAY)

function setup()
    col={ Red=color(255,0,0),Green=color(0,255,0),
          Blue=color(0,0,255),Yellow=color(255,255,0)}
    angle={Red=0,Green=180,Blue=270,Yellow=90}
    rectMode(CENTER)
    fontSize(40)
    parameter.integer("playerSize",1,8,4,setup1)
end

function setup1()
    countDown=1000
    winColor=color(255)
    winScore=0
    winPlayer="no one"
    img=image(WIDTH,HEIGHT)
    str="GAME OVER\
press restart"
    p1=player(WIDTH/2,HEIGHT/2-100,WIDTH/2,100,"Red")
    p2=player(WIDTH/2,HEIGHT/2+100,WIDTH/2,HEIGHT-100,"Green")
    p3=player(WIDTH/2-100,HEIGHT/2,100,HEIGHT/2,"Blue")
    p4=player(WIDTH/2+100,HEIGHT/2,WIDTH-100,HEIGHT/2,"Yellow")
    gameOver=false
end

function draw()
    background(0,0,0,255)
    sprite(img,WIDTH/2,HEIGHT/2)    
    p1:draw()
    p2:draw()
    p3:draw()
    p4:draw()  
    checkcountDown()
end

function touched(t)
    if gameOver then
        if t.state==ENDED and t.tapCount==2 then
            setup1()
        end
    else  
        p1:touched(t)  
        p2:touched(t) 
        p3:touched(t) 
        p4:touched(t) 
    end
end

function checkcountDown()
    if countDown<1000 then
        if countDown<1 then
            gameOver=true
        else
            countDown=countDown-1
        end
    end
end

player=class()

function player:init(x,y,sx,sy,id)
    self.dx=0
    self.dy=0
    self.playerX=x
    self.playerY=y
    self.move=true
    self.size=playerSize
    self.color=col[id]
    self.squareX=sx
    self.squareY=sy
    self.player=id
    self.black=0
    self.dist=self.size*2+1
    self.score=-self.dist^2
    self.angle=angle[id]
    
    setContext(img)
    fill(50,50,50,255)
    stroke(self.color)
    strokeWidth(20)
    rect(self.squareX,self.squareY,200,200)
    setContext()
end

function player:draw()
    pushStyle()
    if self.move and not self.out then
        self.move=false
        self.playerX=self.playerX+self.dx
        if self.playerX<self.size*2 then
            self.playerX=self.size*2
        end
        if self.playerX>WIDTH-self.size*2 then
            self.playerX=WIDTH-self.size*2
        end        
        self.playerY=self.playerY+self.dy
        if self.playerY<self.size*2 then
            self.playerY=self.size*2
        end
        if self.playerY>HEIGHT-self.size*2 then
            self.playerY=HEIGHT-self.size*2
        end       
        self.black=0
        setContext(img)        
        for x1=-self.size,self.size do
            for y1=-self.size,self.size do
                self:hit(self.playerX+x1,self.playerY+y1)
                img:set(self.playerX+x1,self.playerY+y1,self.color)
            end   
        end
        setContext()
        if self.black<self.dist^2 then
            gameOver=true
            self.score=0
        end  
    end  

    pushMatrix()
    translate(self.squareX,self.squareY)
    fill(255)
    rotate(self.angle)
    text(self.score,0,0)
    if gameOver then
        self:win()
        stroke(winColor)
        strokeWidth(5)
        fill(155, 168, 168, 255)
        rect(0,160,350,120)
        fill(winColor)
        text(" Winner is "..winPlayer.."\
Double tap to play",0,160)
    elseif countDown<1000 then
        fontSize(15)
        text(countDown,0,70)
    end        
    popMatrix()
    popStyle()
end

function player:win()
    if self.score>winScore then
        winScore=self.score
        winColor=self.color
        winPlayer=self.player
    end
end

function player:hit(x,y)
    r,g,b,a=img:get(x,y)
    if r+g+b==0 then
        self.black=self.black+1
        self.score=self.score+1
    end
end

function player:touched(t)
    if t.state==MOVING then
        self.move=false
        if t.x>self.squareX-100 and t.x<self.squareX+100 and
                t.y>self.squareY-100 and t.y<self.squareY+100 then
            countDown=500
            self.dx=0
            self.dy=0
            if math.abs(t.deltaX)>math.abs(t.deltaY) then
                self.dx=self.dist
                if t.deltaX<0 then
                    self.dx=-self.dist
                end
            else
                self.dy=self.dist
                if t.deltaY<0 then
                    self.dy=-self.dist
                end
            end
            self.move=true
        end
    end
end

Nice concept you got there :slight_smile: