Skip to content

Commit

Permalink
snake enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
tfeldmann committed Dec 25, 2013
1 parent 036d4e5 commit 3024974
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Software/qlocktoo/app_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file './qlocktoo/app.ui'
#
# Created: Wed Dec 4 14:09:58 2013
# Created: Wed Dec 25 18:46:36 2013
# by: pyside-uic 0.2.15 running on PySide 1.2.1
#
# WARNING! All changes made in this file will be lost!
Expand Down
4 changes: 2 additions & 2 deletions Software/qlocktoo/assets/assets_rc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

# Resource object code
#
# Created: Mi. Dez. 4 14:09:58 2013
# by: The Resource Compiler for PySide (Qt v4.8.6)
# Created: Mi. Dez. 25 18:46:35 2013
# by: The Resource Compiler for PySide (Qt v4.7.4)
#
# WARNING! All changes made in this file will be lost!

Expand Down
2 changes: 1 addition & 1 deletion Software/qlocktoo/connect/connect_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file './qlocktoo/connect/connect.ui'
#
# Created: Wed Dec 4 14:09:58 2013
# Created: Wed Dec 25 18:46:36 2013
# by: pyside-uic 0.2.15 running on PySide 1.2.1
#
# WARNING! All changes made in this file will be lost!
Expand Down
2 changes: 1 addition & 1 deletion Software/qlocktoo/console/console_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file './qlocktoo/console/console.ui'
#
# Created: Wed Dec 4 14:09:58 2013
# Created: Wed Dec 25 18:46:36 2013
# by: pyside-uic 0.2.15 running on PySide 1.2.1
#
# WARNING! All changes made in this file will be lost!
Expand Down
4 changes: 2 additions & 2 deletions Software/qlocktoo/demo/demo.ui
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@
<enum>Qt::ClickFocus</enum>
</property>
<property name="text">
<string>Pong</string>
<string>Pixeltest</string>
</property>
<property name="icon">
<iconset resource="../assets/assets.qrc">
<normaloff>:/icons/black32/fa-gamepad.png</normaloff>:/icons/black32/fa-gamepad.png</iconset>
<normaloff>:/icons/black32/fa-qrcode.png</normaloff>:/icons/black32/fa-qrcode.png</iconset>
</property>
<property name="iconSize">
<size>
Expand Down
6 changes: 3 additions & 3 deletions Software/qlocktoo/demo/demo_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file './qlocktoo/demo/demo.ui'
#
# Created: Wed Dec 4 14:09:58 2013
# Created: Wed Dec 25 18:46:36 2013
# by: pyside-uic 0.2.15 running on PySide 1.2.1
#
# WARNING! All changes made in this file will be lost!
Expand Down Expand Up @@ -70,7 +70,7 @@ def setupUi(self, demoapp):
self.pong.setSizePolicy(sizePolicy)
self.pong.setFocusPolicy(QtCore.Qt.ClickFocus)
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap(":/icons/black32/fa-gamepad.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
icon3.addPixmap(QtGui.QPixmap(":/icons/black32/fa-qrcode.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.pong.setIcon(icon3)
self.pong.setIconSize(QtCore.QSize(32, 32))
self.pong.setAutoDefault(False)
Expand Down Expand Up @@ -161,7 +161,7 @@ def retranslateUi(self, demoapp):
self.fade.setText(QtGui.QApplication.translate("demoapp", "Verlauf", None, QtGui.QApplication.UnicodeUTF8))
self.helix.setText(QtGui.QApplication.translate("demoapp", "Helix", None, QtGui.QApplication.UnicodeUTF8))
self.wave.setText(QtGui.QApplication.translate("demoapp", "Welle", None, QtGui.QApplication.UnicodeUTF8))
self.pong.setText(QtGui.QApplication.translate("demoapp", "Pong", None, QtGui.QApplication.UnicodeUTF8))
self.pong.setText(QtGui.QApplication.translate("demoapp", "Pixeltest", None, QtGui.QApplication.UnicodeUTF8))
self.pulse.setText(QtGui.QApplication.translate("demoapp", "Puls", None, QtGui.QApplication.UnicodeUTF8))
self.white.setText(QtGui.QApplication.translate("demoapp", "Weiß", None, QtGui.QApplication.UnicodeUTF8))
self.black.setText(QtGui.QApplication.translate("demoapp", "Schwarz", None, QtGui.QApplication.UnicodeUTF8))
Expand Down
2 changes: 1 addition & 1 deletion Software/qlocktoo/marquee/marquee_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file './qlocktoo/marquee/marquee.ui'
#
# Created: Wed Dec 4 14:09:58 2013
# Created: Wed Dec 25 18:46:36 2013
# by: pyside-uic 0.2.15 running on PySide 1.2.1
#
# WARNING! All changes made in this file will be lost!
Expand Down
2 changes: 1 addition & 1 deletion Software/qlocktoo/settings/settings_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file './qlocktoo/settings/settings.ui'
#
# Created: Wed Dec 4 14:09:58 2013
# Created: Wed Dec 25 18:46:36 2013
# by: pyside-uic 0.2.15 running on PySide 1.2.1
#
# WARNING! All changes made in this file will be lost!
Expand Down
12 changes: 6 additions & 6 deletions Software/qlocktoo/snake/snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def __init__(self, device):
self.snake = SnakeModel(
width=self.device.columns,
height=self.device.rows,
gameOverCallback=self.gameOver,
ateFoodCallback=self.ateFood)
gameover_callback=self.gameOver,
atefood_callback=self.ateFood)
self.highscore = 0
self.pause = False

Expand All @@ -49,13 +49,13 @@ def keyPressEvent(self, event):
self.snake.reset()
self.stepTimer.start(1000 / self.stepFrequency)
elif key == Qt.Key_Left:
self.snake.setSnakeDirection(-1, 0)
self.snake.set_snake_direction(-1, 0)
elif key == Qt.Key_Right:
self.snake.setSnakeDirection(1, 0)
self.snake.set_snake_direction(1, 0)
elif key == Qt.Key_Down:
self.snake.setSnakeDirection(0, 1)
self.snake.set_snake_direction(0, 1)
elif key == Qt.Key_Up:
self.snake.setSnakeDirection(0, -1)
self.snake.set_snake_direction(0, -1)
else:
QDialog.keyPressEvent(self, event)

Expand Down
66 changes: 43 additions & 23 deletions Software/qlocktoo/snake/snake_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,78 @@ class SnakeModel(object):
"""

def __init__(self, width, height,
gameOverCallback, ateFoodCallback):
self._width = width
self._height = height
self._gameover = gameOverCallback
self._ateFood = ateFoodCallback
gameover_callback, atefood_callback):
"""
Initializes the game
gameover_callback and atefood_callback are callback functions. They
are passed the current player score (length of the snake's tail) as an
integer.
"""
self.width = width
self.height = height
self.gameover_callback = gameover_callback
self.atefood_callback = atefood_callback
self.reset()

def reset(self):
"""
Starts a new game
Starts a new game.
Sets the starting position, length and direction of the snake as well
as the position of the first food item.
"""
self._dir = (0, -1)

# You can read head, tail and food directly for drawing
self.head = (self._width / 2, self._height - 2)
self.tail = [(self._width / 2, self._height - 1)]
self.food = (self._width / 2, self._height / 3)
self.head = (self.width / 2, self.height - 2)
self.tail = [(self.width / 2, self.height - 1)]
self.food = (self.width / 2, self.height / 3)

def step(self):
"""
Generates the next game frame
Advances the game one step
"""
# check for collisions
if self.head in self.tail:
self._gameover(len(self.tail))
self.gameover_callback(len(self.tail))

# eat food
if self.head == self.food:
self.tail.insert(0, self.head)
self.food = self._newFoodLocation()
self._ateFood(len(self.tail))
self.food = self._new_food_location()
self.atefood_callback(len(self.tail))

# move snake
self._moveSnake(*self._dir)
self._move_snake()

def setSnakeDirection(self, dx, dy):
def set_snake_direction(self, dx, dy):
"""
Sets the direction the snake will move on the next step
"""
self._dir = (dx, dy)

def _moveSnake(self, dx, dy):
def _move_snake(self):
"""
Moves the snake one step in the given direction.
The snake can move through the game area's borders and comes out on the
other side.
"""
x, y = self.head
newX = (x + dx) % self._width
newY = (y + dy) % self._height
dx, dy = self._dir
newX = (x + dx) % self.width
newY = (y + dy) % self.height
self.head = (newX, newY)
self.tail.insert(0, (x, y))
self.tail.pop()

def _newFoodLocation(self):
game_field = set([(x, y) for x in range(self._width)
for y in range(self._height)])
def _new_food_location(self):
"""
Returns a new possible food position which is not in the snake.
"""
game_field = set([(x, y) for x in range(self.width)
for y in range(self.height)])
possible = game_field - set(self.tail) - set([self.head])
newPosition = random.choice(list(possible))
return newPosition
new_position = random.choice(list(possible))
return new_position
2 changes: 1 addition & 1 deletion Software/qlocktoo/snake/snake_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Form implementation generated from reading ui file './qlocktoo/snake/snake.ui'
#
# Created: Wed Dec 4 14:09:58 2013
# Created: Wed Dec 25 18:46:36 2013
# by: pyside-uic 0.2.15 running on PySide 1.2.1
#
# WARNING! All changes made in this file will be lost!
Expand Down
5 changes: 2 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Firmware:
☐ Datum
☐ Matrix
☐ Datum Laufschrift
☐ Checksumme bei streaming
☐ Blockierender Stream

Expand All @@ -16,11 +15,11 @@ Wünsche von Marlene:

Carsti und Manu´s Wünsche:
☐ GoL direkt auf der Uhr
☐ Matrix direkt auf der Uhr
☐ Malprogramm - Simulator

___________________
Archive:
✔ Matrix direkt auf der Uhr @done (13-12-22 22:43) @project(Carsti und Manu´s Wünsche)
✔ Pixeltest @done (13-12-16 15:38) @project(Software)
✔ Temperatur @done (13-12-13 14:32) @project(Firmware)
✔ Matrix Demo @done (13-12-13 14:32) @project(Software)
Expand Down

0 comments on commit 3024974

Please sign in to comment.