2048 是一款益智游戏,只需要用方向键让两两相同的数字碰撞就会诞生一个翻倍的数字,初始数字由 2 或者 4 构成,直到游戏界面全部被填满,游戏结束。
编程时并未查看原作者代码,不喜勿喷。
程序结构如下:
R语言代码:
复制代码
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275#!/usr/bin/Rscript #画背景 draw_bg <- function(){ plot(0,0,xlim=c(0,0.8),ylim=c(0,0.8),type='n',xaxs="i", yaxs="i") for (i in c(1:4)){ for (j in c(1:4)){ points(0.1+0.2*(i-1),0.9-0.2*(j),col="gray",pch=15,cex=16)}} } #画数字矩阵 draw_num <- function(){ for (i in c(1:4)){ for (j in c(1:4)){ if (e$m[i,j] != 0){ text(0.1+(j-1)*0.2,0.7-(i-1)*0.2,font=2,family="Arial",label=e$m[i,j],cex=2) } } } } #初次运行游戏,生成随机矩阵 init <- function(){ e$stage=1 mt <- matrix(c(sample(c(2,4),1),rep(0,15)),nrow=4) e$m <- mt[sample(4),sample(4)] draw_bg() draw_num() } #移除矩阵数字间的0 rm_zero <- function(){ if (e$x==0){ if (e$dir=="up"){ for (c in 1:4) e$m[,c] <- c(e$m[,c][which(e$m[,c]!=0)],rep(0,4-length(e$m[,c][which(e$m[,c]!=0)]))) } if (e$dir=="down"){ for (c in 1:4) e$m[,c] <- c(rep(0,4-length(e$m[,c][which(e$m[,c]!=0)])),e$m[,c][which(e$m[,c]!=0)]) } if (e$dir=="left"){ for (r in 1:4) e$m[r,] <- c(e$m[r,][which(e$m[r,]!=0)],rep(0,4-length(e$m[r,][which(e$m[r,]!=0)]))) } if (e$dir=="right"){ for (r in 1:4) e$m[r,] <- c(rep(0,4-length(e$m[r,][which(e$m[r,]!=0)])),e$m[r,][which(e$m[r,]!=0)]) } } else{ if (e$dir=="up"){ c <- e$x e$m[,c] <- c(e$m[,c][which(e$m[,c]!=0)],rep(0,4-length(e$m[,c][which(e$m[,c]!=0)]))) } if (e$dir=="down"){ c <- e$x e$m[,c] <- c(rep(0,4-length(e$m[,c][which(e$m[,c]!=0)])),e$m[,c][which(e$m[,c]!=0)]) } if (e$dir=="left"){ r <- e$x e$m[r,] <- c(e$m[r,][which(e$m[r,]!=0)],rep(0,4-length(e$m[r,][which(e$m[r,]!=0)]))) } if (e$dir=="right"){ r <- e$x e$m[r,] <- c(rep(0,4-length(e$m[r,][which(e$m[r,]!=0)])),e$m[r,][which(e$m[r,]!=0)]) } } } #在空白处添加随机数字 new_mt <- function(){ e$m[sample(which(e$m==0),1)] <- sample(c(2,4),1) } #检查是否游戏失败 fail <- function(){ if (length(e$m[which(e$m==0)])==0){ e$x=0 for (r in 1:3){ for (c in 1:3){ if (e$m[r,c] == e$m[r,c+1] | e$m[r,c] == e$m[r+1,c]){ e$x=1 } } } if (e$x==0){ stage2() } } } #游戏中 stage1 <- function(){ e$stage <- 1 e$x <- 0 rm_zero() if (e$dir=="left"){ i=1 while (i<=4){ if (e$m[i,1] != 0 & e$m[i,1]==e$m[i,2] & e$m[i,1]==e$m[i,3] & e$m[i,1]==e$m[i,4]){ e$m[i,]=rep(c(2*e$m[i,1],0),each=2) e$x=1 } else if (e$m[i,2]!=0 & e$m[i,3] != 0 & e$m[i,2]==e$m[i,1] & e$m[i,3]==e$m[i,4]){ e$m[i,]=c(2*e$m[i,1],0,2*e$m[i,3],0) e$x=1 } else if (e$m[i,2] != 0 & e$m[i,2]==e$m[i,1]){ e$m[i,]=c(2*e$m[i,1],0,e$m[i,3],e$m[i,4]) e$x=1 } else if (e$m[i,3] != 0 & e$m[i,3]==e$m[i,4]){ e$m[i,]=c(e$m[i,1],e$m[i,2],2*e$m[i,3],0) e$x=1 } else if (e$m[i,2] != 0 & e$m[i,2]==e$m[i,3]){ e$m[i,]=c(e$m[i,1],2*e$m[i,2],0,e$m[i,4]) e$x=1 } i=i+1 } rm_zero() new_mt() draw_bg() draw_num() fail() } if (e$dir=="right"){ i=1 while (i<=4){ if (e$m[i,1] != 0 & e$m[i,1]==e$m[i,2] & e$m[i,1]==e$m[i,3] & e$m[i,1]==e$m[i,4]){ e$m[i,]=rep(c(0,2*e$m[i,1]),each=2) e$x=1 } else if (e$m[i,2] != 0 & e$m[i,3] != 0 & e$m[i,2]==e$m[i,1] & e$m[i,3]==e$m[i,4]){ e$m[i,]=c(0,2*e$m[i,1],0,2*e$m[i,3]) e$x=1 } else if (e$m[i,2] != 0 & e$m[i,2]==e$m[i,1]){ e$m[i,]=c(0,2*e$m[i,1],e$m[i,3],e$m[i,4]) e$x=1 } else if (e$m[i,3] != 0 & e$m[i,3]==e$m[i,4]){ e$m[i,]=c(e$m[i,1],e$m[i,2],0,2*e$m[i,3]) e$x=1 } else if (e$m[i,2] != 0 & e$m[i,2]==e$m[i,3]){ e$m[i,]=c(e$m[i,1],0,2*e$m[i,2],e$m[i,4]) e$x=1 } i=i+1 } rm_zero() new_mt() draw_bg() draw_num() fail() } if (e$dir=="up"){ j=1 while (j<=4){ if (e$m[1,j] != 0 & e$m[1,j]==e$m[2,j] & e$m[1,j]==e$m[3,j] & e$m[1,j]==e$m[4,j]){ e$m[,j]=rep(c(2*e$m[1,j],0),each=2) e$x=1 } else if (e$m[2,j] != 0 & e$m[3,j] != 0 & e$m[2,j]==e$m[1,j] & e$m[3,j]==e$m[4,j]){ e$m[,j]=c(2*e$m[1,j],0,2*e$m[3,j],0) e$x=1 } else if (e$m[2,j] != 0 & e$m[2,j]==e$m[1,j]){ e$m[,j]=c(2*e$m[1,j],0,e$m[3,j],e$m[4,j]) e$x=1 } else if (e$m[3,j] != 0 & e$m[3,j]==e$m[4,j]){ e$m[,j]=c(e$m[1,j],e$m[2,j],2*e$m[3,j],0) e$x=1 } else if (e$m[2,j] != 0 & e$m[2,j]==e$m[3,j]){ e$m[,j]=c(e$m[1,j],2*e$m[2,j],0,e$m[4,j]) e$x=1 } j=j+1 } rm_zero() new_mt() draw_bg() draw_num() fail() } if (e$dir=="down"){ j=1 while (j<=4){ if (e$m[1,j] != 0 & e$m[1,j]==e$m[2,j] & e$m[1,j]==e$m[3,j] & e$m[1,j]==e$m[4,j]){ e$m[,j]=rep(c(0,2*e$m[1,j]),each=2) e$x=1 } else if (e$m[2,j] != 0 & e$m[3,j] != 0 & e$m[2,j]==e$m[1,j] & e$m[3,j]==e$m[4,j]){ e$m[,j]=c(0,2*e$m[1,j],0,2*e$m[3,j]) e$x=1 } else if (e$m[2,j] != 0 & e$m[2,j]==e$m[1,j]){ e$m[,j]=c(0,2*e$m[1,j],e$m[3,j],e$m[4,j]) e$x=1 } else if (e$m[3,j] != 0 & e$m[3,j]==e$m[4,j]){ e$m[,j]=c(e$m[1,j],e$m[2,j],0,2*e$m[3,j]) e$x=1 } else if (e$m[2,j] != 0 & e$m[2,j]==e$m[3,j]){ e$m[,j]=c(e$m[1,j],0,2*e$m[2,j],e$m[4,j]) e$x=1 } j=j+1 } rm_zero() new_mt() draw_bg() draw_num() fail() } } stage2<-function(){ e$stage<-2 plot(0,0,xlim=c(0,1),ylim=c(0,1),type='n',xaxs="i", yaxs="i") text(0.5,0.7,label="Game Over",cex=2) text(0.5,0.4,label="Space to restart, q to quit.",cex=2,col=4) text(0.2,0.05,label="Author:YwLiao",cex=1) } # 开机画图 stage0<-function(){ e$stage<-0 plot(0,0,xlim=c(0,1),ylim=c(0,1),type='n',xaxs="i", yaxs="i") text(0.5,0.7,label="2048",cex=2) text(0.5,0.4,label="Any keyboard to start",cex=2,col=4) text(0.5,0.3,label="Up,Down,Left,Rigth to control direction",cex=2,col=2) text(0.2,0.05,label="Author:YwLiao",cex=1) } #键盘事件 keydown<-function(K){ print(paste("keydown:",K,",stage:",e$stage)); if(e$stage==0){ #开机画面 init() return(NULL) } if(e$stage==2){ #结束画面 if(K=="q") q() else if(K==' ') stage0() return(NULL) } if(e$stage==1){ #游戏中 if(K == "q") { stage2() }else { if(tolower(K) %in% c("up","down","left","right")){ e$dir<-tolower(K) stage1() } } } return(NULL) } #开始运行游戏 run<-function(){ e<<-new.env() #X11(type="Xlib") #linux系统需添加此行代码,不过字体受到限制,没有windows下大 stage0() getGraphicsEvent(prompt="2048",onKeybd=keydown) } run()
游戏画面
到此这篇关于R语言写2048游戏实例讲解的文章就介绍到这了,更多相关R语言写2048游戏内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!
最后
以上就是会撒娇心情最近收集整理的关于R语言写2048游戏实例讲解的全部内容,更多相关R语言写2048游戏实例讲解内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复