Red Aprendiz
Número de Mensagens : 19 Reputação : Alerta :
| Assunto: crie um jogo Tetris Ter Fev 10, 2009 12:55 am | |
| script que cria um jogo de Tetris Créditos phacog - Código:
-
#Escrito por phacog #================================================================= #======================================================================= #TETRIS #Escrito por phacog em 20/06/07 #Tradução Br por Red
#=======================================================================
module Tetris FOND_ECRAN = nil #Alterar primeiro coordenados para deslocar os diferentes elementos do jogo ECRAN_JEU = Viewport.new(52, 0, 210, 462) ECRAN_PIECE = Rect.new(400, 80, 116, 116) ECRAN_SCORE = Rect.new(360, 220, 200, 122) #====================================================================== #Definições das diferentes peças com as suas cores #====================================================================== PIECES = [[[[false, false, false, false], [false, true, true, false], [false, true, true, false], [false, false, false, false]], Color.new(0, 0, 255)], [[[false, true, false, false], [false, true,false, false], [false, true, false, false], [false, true, false, false]], Color.new(255, 0, 0)], [[[false, false, false, false], [false, true, false, false], [true, true, true, false], [false, false, false, false]], Color.new(255, 85, 0)], [[[false, false, false, false], [false, true,true, false], [true, true, false, false], [false, false, false, false]], Color.new(0, 255, 0)], [[[false, false, false, false], [false, true,true, false], [false, false, true, true], [false, false, false, false]], Color.new(255, 0, 255)], [[[false, false, false, false], [false, false,true, false], [true, true, true, false], [false, false, false, false]], Color.new(255, 255, 0)], [[[false, false, false, false], [true, false,false, false], [true, true, true, false], [false, false, false, false]], Color.new(255, 255, 255)]] #======================================================================= #Começa o jogo #======================================================================= def Tetris.start(n = 1) @score = 0 @ligne = 0 @niveau = 0 $scene = Scene_Jeu.new(n) end #======================================================================= #Contabilizar, número de linhas destruídas e nível #======================================================================= def Tetris.score(s = 0) @score += s return @score end def Tetris.score_to_var(n) $game_variables[n] = @score end def Tetris.ligne(l = 0) @ligne += l return @ligne end def Tetris.ligne_to_var(n) $game_variables[n] = @ligne end def Tetris.niveau(n = 0) @niveau += n return @niveau def Tetris.niveau_to_var(n) $game_variables[n] = @niveau end end #======================================================================= #PEÇA #======================================================================= class Piece < Sprite attr_reader :matrice def initialize super(ECRAN_JEU) @piece = PIECES[rand(7)] @matrice = @piece[0] self.bitmap = Bitmap.new(84, 84) @couleur = @piece[1] self.y = 0 self.x = 42 @undo = proc {self.visible = false} refresh end def refresh self.bitmap.clear 4.times do |i| 4.times do |j| if @matrice[j][i] rect1 = Rect.new(21 * i, 21 * j, 21, 21) rect2 = Rect.new(21 * i + 1, 21 * j + 1, 19, 19) self.bitmap.fill_rect(rect1, Color.new(0, 255, 255)) self.bitmap.fill_rect(rect2, @couleur) end end end end def move_left @undo = proc {move_right} self.x -= 21 end def move_right @undo = proc {move_left} self.x += 21 end def rotate_right @undo = proc {rotate_left} piece = [[],[],[],[]] 4.times do |i| 4.times do |j| piece[i][j] = @matrice[3 - j][i] end end @matrice = piece refresh end def rotate_left @undo = proc {rotate_right} piece = [[],[],[],[]] 4.times do |i| 4.times do |j| piece[i][j] = @matrice[j][3 - i] end end @matrice = piece refresh end def move @undo = proc {self.y -= 21} self.y += 21 end def undo @undo.call end end
#======================================================================= # contabilizar Record #======================================================================= class Score < Window_Base def initialize super(ECRAN_SCORE.x, ECRAN_SCORE.y, ECRAN_SCORE.width, ECRAN_SCORE.height) self.contents = Bitmap.new(self.width - 32, self.height - 32) self.contents.font = Font.new($fontface , 24) self.contents.font.color = Color.new(255, 255, 0) self.opacity = 180 self.z = 2 refresh end def refresh self.contents.clear self.contents.draw_text(0, 0, 200, 30, "Niveau : #{Tetris.niveau}") self.contents.draw_text(0, 30, 200, 30, "Lignes : #{Tetris.ligne}") self.contents.draw_text(0, 60, 200, 30, "Score : #{Tetris.score}") end def niveau(l) Tetris.niveau(1) if Tetris.ligne / 10 != Tetris.ligne(l) / 10 Tetris.score(10 * l * (l + 1) / 2) refresh end end #====================================================================== #janela para afixar a peça seguinte #====================================================================== class Window_Piece < Window_Base def initialize super(ECRAN_PIECE.x, ECRAN_PIECE.y, ECRAN_PIECE.width, ECRAN_PIECE.height) self.opacity = 180 self.z = 2 end end #======================================================================= #Janela de fim do jogo #======================================================================= class Window_Fin < Window_Base def initialize super(150, 100, 300, 100) self.contents = Bitmap.new(self.width - 32, self.height - 32) self.contents.font = Font.new($fontface, 40) self.contents.font.color = Color.new(255, 255, 0) self.z = 10 refresh end def refresh self.contents.clear self.contents.draw_text(0, 14, self.width - 32, 40, "Score : #{Tetris.score}", 1) end end #======================================================================= #Cena do jogo #======================================================================= class Scene_Jeu def initialize(niveau = 1) @ecran_jeu = Sprite.new(ECRAN_JEU) @ecran_jeu.bitmap = Bitmap.new(210, 462) @window_piece = Window_Piece.new unless FOND_ECRAN == nil @fond = Plane.new @fond.bitmap = Bitmap.new("Graphics/Pictures/" + FOND_ECRAN) @fond.z = -1 end @piece = Piece.new @suivante = Piece.new @suivante.visible = false @window_piece.contents = @suivante.bitmap @puit = Array.new(24) 24.times do |y| @puit[y] = Array.new(12, false) @puit[y][0] = @puit [y][11] = true end 12.times do |x| @puit[22][x] = @puit[23][x] = true end afficher_puit @vitesse = 0.75**niveau @temps = Time.new Tetris.niveau(niveau) @ligne = 0 @score = Score.new end def main Graphics.transition loop do Graphics.update Input.update update break if $scene != self end @piece.dispose @score.dispose @fond.dispose unless FOND_ECRAN == nil @suivante.dispose @ecran_jeu.dispose @murs.dispose @window_piece.dispose end def update @piece.move_left if Input.repeat?(Input::LEFT) @piece.move_right if Input.repeat?(Input::RIGHT) @piece.rotate_left if Input.trigger?(Input::C) @piece.undo if test_piece game_over if Input.trigger?(Input::B) if (Time.new - @temps) > @vitesse or Input.press?(Input::DOWN) @temps = Time.new @piece.move pose_piece if test_piece end end #======================================================================= #Affiche le puit #======================================================================= def afficher_puit @murs = Sprite.new @murs.bitmap = Bitmap.new(252,483) @murs.x = ECRAN_JEU.rect.x - 21 @murs.y = ECRAN_JEU.rect.y @murs.z = 1 23.times do |y| @murs.bitmap.fill_rect(0, 21 * y, 21, 21, Color.new(136, 92, 189)) @murs.bitmap.fill_rect(1, 21 * y + 1, 19, 19, Color.new(67, 210, 154)) @murs.bitmap.fill_rect(231, 21 * y, 21, 21, Color.new(136, 92, 189)) @murs.bitmap.fill_rect(232, 21 * y + 1, 19, 19, Color.new(67, 210, 154)) end for x in 1..10 @murs.bitmap.fill_rect(21 * x, 462, 21, 21, Color.new(136, 92, 189)) @murs.bitmap.fill_rect(21 * x + 1, 463, 19, 19, Color.new(67, 210, 154)) end end #======================================================================= #Teste se a peça for colocada bem #======================================================================= def test_piece x = @piece. x / 21 + 1 y = @piece.y / 21 4.times do |j| 4.times do |i| return true if @piece.matrice[ j][i] and @puit[y + j][x + i] end end return false end
#======================================================================= #pose peça #======================================================================= def pose_piece @piece.undo @ecran_jeu.bitmap.blt(@piece.x, @piece.y, @piece.bitmap, Rect.new(0, 0, 84, 84)) x = @piece. x / 21 + 1 y = @piece.y / 21 4.times do |j| 4.times do |i| @puit[y + j][x + i] = true if @piece.matrice[j][i] end end count = 0 for i in y..[y + 4, 21].min do if test_ligne(i) supprime_ligne(i) count += 1 end end @score.niveau(count) @vitesse = 0.75**Tetris.niveau @piece.dispose @piece = @suivante @piece.visible = true if test_piece @piece.undo game_over end @suivante = Piece.new @suivante.visible = false @window_piece.contents = @suivante.bitmap end #======================================================================= #teste se uma linha for completada #======================================================================= def test_ligne(y) r = true for x in 1..10 do unless @puit[y][x] r = false break end end return r end #======================================================================= #Suprime uma linha #======================================================================== def supprime_ligne(ligne) y = ligne * 21 image = Bitmap.new(210, 462) image.blt(0, 21, @ecran_jeu.bitmap, Rect.new(0, 0, 210, y)) image.blt(0, y + 21, @ecran_jeu.bitmap, Rect.new(0, y + 21, 210, 451 - y)) @ecran_jeu.bitmap.clear @ecran_jeu.bitmap.blt(0, 0, image, Rect.new(0, 0, 210, 462)) tableau = Array.new(24) 24.times {|l| tableau[l] = Array.new(12, false)} 24.times do |j| 12.times do |i| if j < ligne tableau[j + 1][i] = @puit[j][i] elsif j > ligne tableau[j][i] = @puit[j][i] end end end tableau[0][0] = tableau [0][11] = true @puit = tableau end def game_over @window = Window_Fin.new loop do Graphics.update Input.update break if Input.trigger?(Input::C) end $scene = Scene_Map.new @window.dispose end end end #======================================================================= #TETRIS #Escrito por phacog em 20/06/07 #=======================================================================
1-faça um evento no mapa 2-Para chamar o script use Tetris.start(1) | |
|