0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DXRuby 講義資料 ピンポンゲーム

Last updated at Posted at 2024-04-30

学べる知識

  • キーボード制御
  • 画像の表示および移動
  • フォント利用
  • シーン制御

ゲーム内容

卓球
動いてる図形と静止してる図形が合わさるようにタイミングよくキーを押して
ラリーする(ように見える)

ラリーが続く動く図形の速度がどんどん早くなる

主な学習目的

ゲーム内でマウスを使用する方法を学ぶ。

ping_pong.rb
# coding: utf-8
require 'dxruby'
Window.width =  1000
Window.height = 800
Window.bgcolor=[255,200,200,200]
x = 300
y = 100

image1 = Image.load("./image/pingpong1.png")
table_tennis = Sprite.new(0, 0, image1)
image2 = Image.load("./image/pingpong2.png")
image3 = Image.load("./image/out_range.png")
image4 = Image.load("./image/out_range.png")
background = Sprite.new(500, 200, image3)
out_range = Sprite.new(401, 200, image4)

speed = 1
level = 0

font = Font.new(52)

score = 1
miss = 0 

Window.loop do
  Sprite.draw(table_tennis) 
  Sprite.draw(background)
  Sprite.draw(out_range)

  if score >= 10
    Window.draw_font(400, 100, "乗ってきたZE ドリームタイム突入ーーー", font)
    Window.bgcolor=[255,out_range.x-400, out_range.x-300, out_range.x-320]
elsif score >= 4     
    Window.draw_font(400, 100, "スピードげていくぜEEEE", font)
    Window.bgcolor=[255,(out_range.x-400)/2, (out_range.x-300)/2, (out_range.x-320)/2]
end

    if miss >= 1
        Window.draw_font(100, 100, "ラリー頑張って続けようぜ", font)
        Window.draw_font(100, 150, "連続ラリー数:【#{score}回】", font)
         if Input.keyDown?(K_RETURN)
           miss = 0
         end
    else
            out_range.x += speed
            if out_range === background
                if Input.keyPush?(K_SPACE)
                p "hit"
                    if score % 2 == 0
                        table_tennis = Sprite.new(0, 0, image1)
                        score += 1
                    elsif score % 2 == 1
                        table_tennis = Sprite.new(0, 0, image2)
                        score += 1
                        if score >= 4
                            speed *= 1.2
                        elsif score >= 2
                            speed *= 2
                        end
                    end
                end
            else
                if Input.keyPush?(K_SPACE)
                    p "dame!!!!!"
                    p miss
                    # ミスした場合に 加算
                    miss += 1
                    table_tennis = Sprite.new(0, 0, image1)
                end
            end

        if out_range.x > 600
            speed *= -1
            # -1を掛けることで 向きを反転することができる
        elsif out_range.x < 400
            speed *= -1
        end
        Window.draw_font(400, 50, "#{out_range.x}", font)
    end

end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?