Jump to content

User:Wakebrdkid/Game

From Wikipedia, the free encyclopedia

This is a collaborative game development experiment.

Current rules: Control a yellow dot with the arrow keys and try to get as much food as you can.

Try current version

validQ[p : {x_, y_}] :=
  If[Clip[p, {1, Length@landscape}] == p &&
    MatchQ[landscape[[x, y]], 1 | 5], True, False];
move[p_, o_] := If[validQ[p + o], p + o, p];
randomMove[p_] :=
  If[RandomInteger[3] == 0,
   RandomChoice[move[p, #] & /@ {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}],
   p];
plotLandscape[] :=
 Grid[{{"Energy:" <> ToString@energy <> "  Score:" <> ToString@score,
    SpanFromLeft}, {Null,
    ArrayPlot[{ReplacePart[Table[White, {Length@landscape}],
       y -> Black]}, ImageSize -> {300, 5},
     PlotRangePadding -> 0]}, {ArrayPlot[
     ReplacePart[Table[{White}, {Length@landscape}], {x, 1} -> Black],
      ImageSize -> {5, 300}, PlotRangePadding -> 0],
    ArrayPlot[
     ReplacePart[ReplacePart[landscape, people -> 3], pos -> 4],
     ColorRules -> {1 -> Brown, 2 -> Blue, 3 -> Black, 4 -> Yellow,
       5 -> Green}, ImageSize -> {300, 300}, PlotRangePadding -> 0]}}]

SetOptions[InputNotebook[],
  NotebookEventActions -> {"RightArrowKeyDown" :> (dir = {0, 1}),
    "LeftArrowKeyDown" :> (dir = {0, -1}),
    "UpArrowKeyDown" :> (dir = {-1, 0}),
    "DownArrowKeyDown" :> (dir = {1, 0})}];

Manipulate[
 Dynamic[If[energy > 0, energy--; score++, dir = {0, 0}];
  If[landscape[[x, y]] == 5, energy += 10;
   landscape[[x, y]] = 1]; (landscape[[#, #2]] = 1) & @@@ people;
  pos = move[pos, dir]; people = randomMove /@ people;
  plotLandscape[], TrackedSymbols -> {}, UpdateInterval -> .1],
 Initialization :> (
   landscape =
    Map[If[# < .47, 2,
       If[RandomReal[] > Rescale[#, {.45, .53}], 5, 1]] &,
     GaussianFilter[RandomReal[1, {100, 100}], 5], {2}];
   people = RandomChoice[Position[landscape, 1], 200];
   pos = RandomChoice[Position[landscape, 1]];
   x := pos[[1]];
   y := pos[[2]];
   dir = {0, 0};
   score = 0;
   energy = 100;), Deployed -> True, SaveDefinitions -> True,
 Paneled -> False, AppearanceElements -> None]