1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
begin
require 'rubygems'
rescue LoadError
end
require 'gosu'
include Gosu
#on a appelé gosu et tout ce qu'il faut pour pouvoir faire des graphismes.
#################################################################################
class Game < Window#la classe de la fenetre.
#=====================================================================================================================================
def initialize(height,width)#début initialize
super(height*10, width*10, false)#taille de la fenetre(10 fois le nombre de case plus une marge de 20 px)
self.caption = "Life..."#le nom de la fenetre
@height=height#longueur de la fenetre
@width =width#largeur de la fenetre
@compteur=0
@tableau1=Array.new#on créé un tableau
for i in 0 ... @height#de 0 jusqu'a la longueur de la fenetre...
@tableau1[i]=Array.new#on créé un tableau dans chaque case du tableau
for j in 0 ... @width#ce qui fait qu'on a un tableau a 2 dimensions.
@tableau1[i][j]=rand(2)#pour chaque case du tableau on met une valeur entre 0 et 1.
end#fin for i
end#fin for j
@tableau2=Array.new#on créé un tableau
for i in 0 ... @height#de 0 jusqu'a la longueur de la fenetre...
@tableau2[i]=Array.new#on créé un tableau dans chaque case du tableau
for j in 0 ... @width#ce qui fait qu'on a un tableau a 2 dimensions.
@tableau2[i][j]=0#on met tout a 0 cette fois ci
end#fin for i
end#fin for j
for i in 0 ... @height
for j in 0 ... @width
avenir(i,j)
end
end
end#fin initialize
#===================================================================================================================================(
def update
if button_down? Button::KbEscape
self.close
end
end
#===================================================================================================================================
def avenir(x,y)#début avenir
for i in x-1 ... x+1
for j in y-1 ... y+1
if i>=0 and i <=@height and j>=0 and j <=@width
if @tableau1[i][j]==1
@compteur += 1
end
end
end
end
#====================================
if @tableau1[x][y]==0
if @compteur==3 or @compteur==2
@tableau2[x][y]=1
else#compteur different de 2 ou 3(isolé ou surpop)
@tableau2[x][y]=0
end
else#if @tableau1[x][y]==1
@compteur -= 1
if @compteur==2 or @compteur==3
@tableau2[x][y]=1
else
@tableau2[x][y]=0
end
end
end#fin avenir
#===================================================================================================================================
def draw
@img1=Image.new(self,"cell01.png",true)#dessiner image1
@img0=Image.new(self,"cell00.png",true)
if !button_down? Button::KbLeft
for i in 0 ... @height
for j in 0 ... @width
if @tableau2[i][j]==1
@img1.draw(i*10,j*10,2)
else
# @img=Image.new(self,"cell00.png",true)
@img0.draw(i*10,j*10,2)
#dessiner image2
end
end
end
else
for i in 0 ... @height
for j in 0 ... @width
if @tableau1[i][j]==1
@img1.draw(i*10,j*10,2)
else
# @img=Image.new(self,"cell00.png",true)
@img0.draw(i*10,j*10,2)
#dessiner image2
end
end
end
end
end#fin draw
#===================================================================================================================================
end#fin de la classe Game
Game.new(80,25).show#on affiche la fenetre... |
Partager