Browse Source

scale snake to single panel dot count

Thomas Buck 1 year ago
parent
commit
ae944557b4
1 changed files with 14 additions and 11 deletions
  1. 14
    11
      snake.py

+ 14
- 11
snake.py View File

29
                                     3, 50, sc)
29
                                     3, 50, sc)
30
 
30
 
31
         self.text_c = (0, 0, 255)
31
         self.text_c = (0, 0, 255)
32
+        self.fact = int(self.gui.width / self.gui.panelW)
32
 
33
 
33
         random.seed()
34
         random.seed()
34
         self.restart()
35
         self.restart()
39
         self.direction = "r"
40
         self.direction = "r"
40
         self.directionTmp = "r"
41
         self.directionTmp = "r"
41
         self.score = 0
42
         self.score = 0
42
-        self.data = [[0 for y in range(self.gui.height)] for x in range(self.gui.width)]
43
+        self.data = [[0 for y in range(int(self.gui.height / self.fact))] for x in range(int(self.gui.width / self.fact))]
43
 
44
 
44
-        self.player = [ (int(self.gui.width / 2), int(self.gui.height / 2)) ]
45
+        self.player = [ (int(self.gui.width / 2 / self.fact), int(self.gui.height / 2 / self.fact)) ]
45
         self.data[self.player[0][0]][self.player[0][1]] = 1
46
         self.data[self.player[0][0]][self.player[0][1]] = 1
46
 
47
 
47
         DrawText = util.getTextDrawer()
48
         DrawText = util.getTextDrawer()
77
         return False
78
         return False
78
 
79
 
79
     def placeDot(self):
80
     def placeDot(self):
80
-        d = (random.randrange(0, self.gui.width), random.randrange(0, self.gui.height))
81
+        d = (random.randrange(0, int(self.gui.width / self.fact)), random.randrange(0, int(self.gui.height / self.fact)))
81
         while (self.data[d[0]][d[1]] != 0) or (d[0] < 15) or (d[1] < 8):
82
         while (self.data[d[0]][d[1]] != 0) or (d[0] < 15) or (d[1] < 8):
82
-            d = (random.randrange(0, self.gui.width), random.randrange(0, self.gui.height))
83
+            d = (random.randrange(0, int(self.gui.width / self.fact)), random.randrange(0, int(self.gui.height / self.fact)))
83
         self.data[d[0]][d[1]] = 2
84
         self.data[d[0]][d[1]] = 2
84
 
85
 
85
     def buttons(self):
86
     def buttons(self):
109
         elif self.direction == "d":
110
         elif self.direction == "d":
110
             player = (player[0], player[1] + 1)
111
             player = (player[0], player[1] + 1)
111
 
112
 
112
-        if (player[0] < 0) or (player[1] < 0) or (player[0] >= self.gui.width) or (player[1] >= self.gui.height):
113
+        if (player[0] < 0) or (player[1] < 0) or (player[0] >= int(self.gui.width / self.fact)) or (player[1] >= int(self.gui.height / self.fact)):
113
             return False
114
             return False
114
 
115
 
115
         if self.data[player[0]][player[1]] == 0:
116
         if self.data[player[0]][player[1]] == 0:
122
         else:
123
         else:
123
             # collected a dot
124
             # collected a dot
124
             self.score += 1
125
             self.score += 1
125
-            if self.score >= self.gui.width * self.gui.height:
126
+            if self.score >= int(self.gui.width / self.fact) * int(self.gui.height / self.fact):
126
                 return False
127
                 return False
127
 
128
 
128
             self.timestep = self.timestep * self.speedup
129
             self.timestep = self.timestep * self.speedup
133
         return True
134
         return True
134
 
135
 
135
     def finishedEndScreen(self):
136
     def finishedEndScreen(self):
136
-        if self.score >= self.gui.width * self.gui.height:
137
+        if self.score >= int(self.gui.width / self.fact) * int(self.gui.height / self.fact):
137
             return self.winText.finished()
138
             return self.winText.finished()
138
         else:
139
         else:
139
             return self.loseText.finished()
140
             return self.loseText.finished()
140
 
141
 
141
     def drawEndScreen(self):
142
     def drawEndScreen(self):
142
-        if self.score >= self.gui.width * self.gui.height:
143
+        if self.score >= int(self.gui.width / self.fact) * int(self.gui.height / self.fact):
143
             self.winText.draw()
144
             self.winText.draw()
144
         else:
145
         else:
145
             self.loseText.draw()
146
             self.loseText.draw()
179
                 self.loseText.restart()
180
                 self.loseText.restart()
180
                 self.scoreText.restart()
181
                 self.scoreText.restart()
181
 
182
 
182
-        for x in range(0, self.gui.width):
183
-            for y in range(0, self.gui.height):
184
-                self.gui.set_pixel(x, y, self.colors[self.data[x][y]])
183
+        for x in range(0, int(self.gui.width / self.fact)):
184
+            for y in range(0, int(self.gui.height / self.fact)):
185
+                for x1 in range(0, self.fact):
186
+                    for y1 in range(0, self.fact):
187
+                        self.gui.set_pixel(x * self.fact + x1, y * self.fact + y1, self.colors[self.data[x][y]])
185
 
188
 
186
         # draw score
189
         # draw score
187
         self.text.setText(str(self.score), "tom-thumb")
190
         self.text.setText(str(self.score), "tom-thumb")

Loading…
Cancel
Save