Ruby/SDLを使ってブロック崩しを制作しているのですが画面いっぱいにブロックを並べたら処理が落ちて困っています。なにか軽くする方法を教えていただけないでしょうか?お願いします。
ソース抜粋 重要そうなところだけ抜粋してみました
require "sdl"
require "lib/fpstimer"
SCREEN_W = 640
SCREEN_H = 480
def load_image(fname)
image = SDL::Surface.load(fname)
image.set_color_key(SDL::SRCCOLORKEY, [255, 255, 0])
image
end
SDL.init(SDL::INIT_EVERYTHING)
screen=SDL.set_video_mode(SCREEN_W, SCREEN_H, 16,SDL::SWSURFACE)
ball = load_image("image/ball.png")
bar = load_image("image/bar.png")
block = [load_image("image/block1.png"),
load_image("image/block2.png"),
(略します)
load_image("image/block9.png")]
board =[[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
(略します 25*32)
[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]
loop do
screen.fillRect 0, 0, 640, 480, [ 0, 0, 0 ]
speedX = 5 if speedX > 5
speedY = 5 if speedY > 5
speedX = -5 if speedX < -5
speedY = -5 if speedY < -5
SDL::Key.scan
if SDL::Key.press?(SDL::Key::SPACE)
barX -= 12 if SDL::Key.press?(SDL::Key::LEFT)
barX += 12 if SDL::Key.press?(SDL::Key::RIGHT)
else
barX -= 8 if SDL::Key.press?(SDL::Key::LEFT)
barX += 8 if SDL::Key.press?(SDL::Key::RIGHT)
end
if ballenterflag == 0
if SDL::Key.press?(SDL::Key::RETURN)
ballenterflag = 1
end
if SDL::Key.press?(SDL::Key::LEFT)
if SDL::Key.press?(SDL::Key::RETURN)
ballenterflag = 2
end
end
end
#バーとボールの判定
if fallball == 0
if ballX + 6< barX + 96 && ballX + 6 > barX &&
ballY+ 6 > barY - 2 && ballY + 6 < barY + 12 + 2 # 周りにヒット
if SDL::Key.press?(SDL::Key::LEFT)
if speedX >= 0 && speedY >= 0
speedX = speedX / 1.2
speedY = speedY *1.2
elsif speedX < 0 && speedY > 0
speedX = speedX * 1.2
speedY = speedY / 1.2
end
elsif SDL::Key.press?(SDL::Key::RIGHT)
if speedX >= 0 && speedY >= 0
speedX = speedX * 1.2
speedY = speedY / 1.2
elsif speedX < 0 && speedY > 0
speedX = speedX / 1.2
speedY = speedY * 1.2
end
end
speedY = -speedY
if ballX + 6< barX + 52 && ballX + 6 > barX + 44 &&
ballY+ 6 > barY - 2 && ballY + 6 < barY + 12 + 2 #真ん中ヒット
kantuu = 1
elsif
kantuu = 0
scoreplus = 200
end
fallball = 1 #1ならボールを跳ね返さない
end
end
for i in 0..25
for j in 0..32 #高さ
if board[j][i] == 1
screen.put(block[0], blockX+i*24, blockY+j*12)
elsif board[j][i] == 2
screen.put(block[1], blockX+i*24, blockY+j*12)
略します 1~10です
elsif board[j][i] == 10
screen.put(block[9], blockX+i*24, blockY+j*12)
end
if board[j][i] != 0
if (ballX + 6 > blockX + i*24 && ballX + 6 < blockX + i*24 +24) &&
((ballY + 6 > blockY + j*12 -3 && ballY + 6 < blockY + j*12 + 3) ||
(ballY + 6 > blockY + j*12 + 12 -3 && ballY + 6 < blockY + j*12 +12 + 3))
if kantuu == 0
speedY = -speedY
end
board[j][i] = 0
score += scoreplus
scoreplus += 50
elsif (ballY + 6 > blockY + j*12 && ballY + 6 < blockY + j*12 + 12) &&
((ballX + 6 > blockX + i*24 -2 && ballX + 6 < blockY + i*24 +3) ||
(ballX + 6 > blockX + i*24 + 24 -3 && ballX + 6 < blockY + i*24 +24 +2))
if kantuu == 0
speedX = -speedX
end
board[j][i] = 0
score += scoreplus
scoreplus += 50
end
end
end
end
screen.put(ball, ballX, ballY)
timer.wait_frame do
screen.update_rect(0, 0, 0, 0)
end
end
お礼
回答ありがとうございます。 screen.fillRect 0, 0, 640, 480, [ 0, 0, 0 ] この処理を無くし、画面の更新だけにしたところとても軽くなりました。ありがとうございました