@dave1707 I’ve solved the touch problem,now,I find another one,my ball always shakes when it’s nearby the bats and the screen padding,I use some of script in examples,but It can’t work well in my project
I’m so sorry that I’m a starter and not very good at English,so I wonder if you can figure out the wrong point in my script.And I really don’t know which class is the wrong point in.So,please forgive me to copy all my script again.That would be very thankful!
--# Ball
Ball = class()
function Ball:init(x,y)
self.x=x
self.y=y
self.pos=vec2(3*WIDTH/4,HEIGHT/2)
self.vel=vec2(3,1)
self.body=physics.body(CIRCLE,20)
self.radius=20
end
function Ball:draw()
-- Codea does not automatically call this
spriteMode(CENTER)
sprite("Space Art:UFO",self.pos.x,self.pos.y,50)
end
function Ball:update()
self.vel=0.999*self.vel
self.pos= self.pos+ self.vel
if (self.pos.x + self.radius) >= WIDTH then
self.pos.x = WIDTH - self.radius
self.vel.x = -self.vel.x
elseif (self.pos.x - self.radius) <= 0 then
self.pos.x = self.radius
self.vel.x = -self.vel.x
elseif (self.pos.y + self.radius) >= HEIGHT then
self.pos.y = HEIGHT - self.radius
self.vel.y = -self.vel.y
elseif (self.pos.y - self.radius) <= 0 then
self.pos.y = self.radius
self.vel.y = -self.vel.y
end
print(self.vel.x)
print(self.pos.x)
end
function Ball:left()
return self.pos.x - self.radius
end
function Ball:right()
return self.pos.x + self.radius
end
function Ball:top()
return self.pos.y + self.radius
end
function Ball:bottom()
return self.pos.y - self.radius
end
function Ball:win()
if self.pos.x<60 and self.pos.y<(HEIGHT/2-HEIGHT/16)and self.pos.y>(HEIGHT/8)
then
return 1
end
if self.pos.x>0.98*WIDTH and self.pos.y<(HEIGHT/2-HEIGHT/16)and self.pos.y>(HEIGHT/8) then
return 0
end
end
function Ball:touched(touch)
-- Codea does not automatically call this method
end
--# Bat1
Bat1 = class()
function Bat1:init()
-- you can accept and set parameters here
self.pos=vec2(0,HEIGHT/2)
self.hi=vec2(0,0)
self.size=vec2(80,130)
end
function Bat1:draw()
-- Codea does not automatically call this method
self.pos.x=math.min(self.pos.x,WIDTH/2-60)
rectMode(CENTER)
rect(self.pos.x,self.pos.y,80,130)
fill(143, 0, 255, 255)
end
function Bat1:touched(touch)
-- Codea does not automatically call this method
self.pos.x=math.min(touch.x,WIDTH/2-100)
self.pos.y=touch.y
self.hi.x=touch.deltaX
self.hi.y=touch.deltaY
end
function Bat1:collide(ball)
if ball:left() < self:right() and
ball:right() > self:left() and
ball:top() > self:bottom() and
ball:bottom() < self:top() then
if ball.pos.y <= self:top() and ball.pos.y >= self:bottom() then
ball.vel.x = -ball.vel.x+0.5*self.hi.x
else
ball.vel.y = -ball.vel.y+0.5*self.hi.y
end
end
end
function Bat1:left()
return self.pos.x - self.size.x / 2
end
function Bat1:right()
return self.pos.x + self.size.x / 2
end
function Bat1:top()
return self.pos.y + self.size.y / 2
end
function Bat1:bottom()
return self.pos.y - self.size.y / 2
end
--# Bat2
Bat2 = class()
function Bat2:init()
-- you can accept and set parameters here
self.pos=vec2(WIDTH,HEIGHT/2)
self.hi=vec2(0,0)
self.size=vec2(80,130)
end
function Bat2:draw()
-- Codea does not automatically call this method
self.pos.x=math.max(self.pos.x,WIDTH/2)
rectMode(CENTER)
rect(self.pos.x,self.pos.y,80,130)
fill(140,0,255,255)
end
function Bat2:touched(touch)
-- Codea does not automatically call this method
self.pos.x=math.max(touch.x,WIDTH/2+100)
self.pos.y=touch.y
self.hi.x=touch.deltaX
self.hi.y=touch.deltaY
end
function Bat2:collide()
if ball:left() <= self:right() and
ball:right() >= self:left() and
ball:top() >= self:bottom() and
ball:bottom() <= self:top() then
sound(SOUND_BLIT)
if self:top()-ball.pos.y>10 and self:top()-ball.pos.y<20 and ball.pos.y-self:bottom()>10 and ball.pos.y-self:bottom()<20 then
ball.vel.x = -ball.vel.x+self.hi.x
else
ball.vel.y = -ball.vel.y+self.hi.y
end
end
end
function Bat2:left()
return self.pos.x - self.size.x / 2
end
function Bat2:right()
return self.pos.x + self.size.x / 2
end
function Bat2:top()
return self.pos.y + self.size.y / 2
end
function Bat2:bottom()
return self.pos.y - self.size.y / 2
end
--# Main
--main
function setup()
print("hello world")
displayMode(FULLSCREEN)
bat1=Bat1()
bat2=Bat2()
touches={}
ball=Ball()
gameover=false
end
function draw()
if gameover==false then
background(0, 0, 0, 255)
ellipse(WIDTH/2,HEIGHT/2,200)
strokeWidth(10)
stroke(255, 255, 255, 255)
fill(140, 0,255, 255)
rect(0,HEIGHT/2-HEIGHT/16,10,HEIGHT/8)
rect(0.98*WIDTH,HEIGHT/2-HEIGHT/16,10,HEIGHT/8)
line(WIDTH/2,0,WIDTH/2,HEIGHT)
bat1:draw()
bat2:draw()
ball:draw()
ball:update()
bat1:collide(ball)
bat2:collide(ball)
if ball:win()==1 then
text("win",WIDTH/2,HEIGHT/2)
gameover=true
end
end
end
function touched(touch)
if touch.x<WIDTH/2 then bat1:touched(touch)
elseif touch.x>WIDTH/2 then bat2:touched(touch)
end
end
–# Vvv