Skip to content

Commit a4b7c6d

Browse files
author
Ricardo Band
committed
updated to the newest version of pymlgame
1 parent 1b3f5a1 commit a4b7c6d

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

game.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python3.3
1+
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

44
"""
@@ -7,17 +7,17 @@
77
88
This is a clone of the well known snake game for Mate Light using pymlgame.
99
"""
10-
import time
1110

1211
__author__ = 'Ricardo Band'
1312
__copyright__ = 'Copyright 2014, Ricardo Band'
1413
__credits__ = ['Ricardo Band']
1514
__license__ = 'MIT'
16-
__version__ = '1.0.0'
15+
__version__ = '1.0.1'
1716
__maintainer__ = 'Ricardo Band'
1817
__email__ = '[email protected]'
1918
__status__ = 'Development'
2019

20+
import time
2121
import random
2222

2323
import pymlgame
@@ -29,22 +29,17 @@ class Game(object):
2929
"""
3030
The main game class that holds the gameloop.
3131
"""
32-
def __init__(self, host, port, width, height):
32+
def __init__(self, mlhost, mlport):
3333
"""
3434
Create a screen and define some game specific things.
3535
"""
36-
self.host = host
37-
self.port = port
38-
self.width = width
39-
self.height = height
40-
self.screen = pymlgame.Screen(self.host, self.port,
41-
self.width, self.height)
42-
self.clock = pymlgame.Clock()
43-
self.ctlr = pymlgame.Controller(self.host, self.port + 1)
44-
45-
part = (int(self.width / 2), int(self.height / 2))
36+
pymlgame.init()
37+
self.screen = pymlgame.Screen(mlhost, mlport, 40, 16)
38+
self.clock = pymlgame.Clock(15)
39+
40+
part = (int(self.screen.width / 2), int(self.screen.height / 2))
4641
self.snake = Snake([(part[0] - 2, part[1]), (part[0] - 1, part[1]),
47-
part], RIGHT, (self.width, self.height))
42+
part], RIGHT, (self.screen.width, self.screen.height))
4843
self.gameover = False
4944
self.apple = self.generate_apple()
5045
self.apple_surface = pymlgame.Surface(1, 1)
@@ -72,7 +67,7 @@ def render(self):
7267
self.screen.reset()
7368

7469
# draw snake
75-
surface = pymlgame.Surface(self.width, self.height)
70+
surface = pymlgame.Surface(self.screen.width, self.screen.height)
7671
for part in self.snake.parts:
7772
surface.draw_dot(part, pymlgame.RED)
7873
self.screen.blit(surface)
@@ -86,17 +81,17 @@ def render(self):
8681

8782
self.screen.update()
8883

89-
# accelerate every 5 points by 1 fps
90-
self.clock.tick(5 + int(self.score / 5))
84+
#TODO: accelerate every 5 points by 1 fps
85+
self.clock.tick()
9186

9287
def handle_events(self):
9388
"""
9489
Loop through all events.
9590
"""
96-
for event in self.ctlr.get_events():
97-
if event.type == pymlgame.NEWCTLR:
91+
for event in pymlgame.get_events():
92+
if event.type == pymlgame.E_NEWCTLR:
9893
print('new ctlr with uid:', event.uid)
99-
elif event.type == pymlgame.KEYDOWN:
94+
elif event.type == pymlgame.E_KEYDOWN:
10095
if event.button == pymlgame.CTLR_UP:
10196
if self.snake.direction != DOWN:
10297
self.snake.direction = UP
@@ -109,7 +104,7 @@ def handle_events(self):
109104
elif event.button == pymlgame.CTLR_RIGHT:
110105
if self.snake.direction != LEFT:
111106
self.snake.direction = RIGHT
112-
elif event.type == pymlgame.PING:
107+
elif event.type == pymlgame.E_PING:
113108
print('ping from', event.uid)
114109

115110
def gameloop(self):
@@ -129,7 +124,7 @@ def gameloop(self):
129124
end = time.time() + 5
130125
while time.time() < end:
131126
self.screen.reset()
132-
surface = pymlgame.Surface(self.width, self.height)
127+
surface = pymlgame.Surface(self.screen.width, self.screen.height)
133128
#TODO: write score and highscore
134129
#font = pymlgame.font('score: {}'.format(self.score),
135130
# pymlgame.WHITE, pymlgame.BLACK)
@@ -147,15 +142,14 @@ def gameloop(self):
147142

148143
self.screen.blit(surface)
149144
self.screen.update()
150-
self.clock.tick(5)
145+
self.clock.tick()
151146

152147
except KeyboardInterrupt:
153148
pass
154-
self.ctlr.quit()
155149

156150
def generate_apple(self):
157-
return (random.randrange(self.width),
158-
random.randrange(self.height))
151+
return (random.randrange(self.screen.width),
152+
random.randrange(self.screen.height))
159153

160154
def write_highscore(self):
161155
"""
@@ -184,5 +178,6 @@ def get_highscore():
184178

185179

186180
if __name__ == '__main__':
187-
GAME = Game('127.0.0.1', 1337, 50, 28)
188-
GAME.gameloop()
181+
#GAME = Game('127.0.0.1', 1337)
182+
GAME = Game('ml.jaseg.net', 1337)
183+
GAME.gameloop()

0 commit comments

Comments
 (0)