Skip to content

Commit 73702f7

Browse files
committed
rudimental touchscreen support
1 parent c1ec8f6 commit 73702f7

4 files changed

Lines changed: 50 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ tiny snake in Python pygame
77
## FEATURES
88

99
* Joystick and GamePad support
10-
* fullscreen and window mode
10+
* touchscreen compatible
11+
* full screen and window mode
1112
* fully customizable

conf.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
# screen settings
2-
#SCREEN_WIDTH = 1024
3-
#SCREEN_HEIGHT = 600
4-
SCREEN_WIDTH = 1152
5-
SCREEN_HEIGHT = 864
2+
SCREEN_WIDTH = 800
3+
SCREEN_HEIGHT = 600
4+
#SCREEN_WIDTH = 1152
5+
#SCREEN_HEIGHT = 864
66
FULLSCREEN = False
77

88
# window settings
99
WINDOW_POSITION_X = 0
1010
WINDOW_POSITION_Y = 0
11-
WINDOW_BORDER = False
11+
WINDOW_BORDER = True
1212

1313
# UI settings
1414
FONT_COLOR = (255, 226, 165)
1515

16+
# controller foo
17+
TOUCH_SCREEN = True
18+
1619
# game settings
17-
BLOCKSIZE = 25
20+
BLOCKSIZE = 10
1821
SNACKS = 10
1922
SNACK_COLOR = (99, 255, 99) # 'green'
2023
SNAKE_COLOR = (245, 101, 44) # ORANGE

snake.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from ui import ui
1111
from joystick import joystick
1212
from popUp import popUp
13+
from touchScreen import touchScreen
1314
#from pygame.sprite import Sprite
1415

1516
#sys.stdout = os.devnull
@@ -62,6 +63,7 @@ def __init__(self):
6263
self.playerBox = None
6364
self.ui = None
6465
self.popUp = None
66+
self.touchScreen = None
6567

6668
def move(self, direction):
6769
#print 'direction:', direction
@@ -232,6 +234,7 @@ def run_game(self):
232234
self.iUi = ui(self.screen)
233235
self.popUp = popUp(self.screen)
234236
self.popUp.color = conf.FONT_COLOR
237+
self.touchScreen = touchScreen(self.SCREEN_WIDTH, self.SCREEN_HEIGHT)
235238

236239
pygame.joystick.init()
237240
self.joystickInteract = joystick()
@@ -291,7 +294,16 @@ def run_game(self):
291294
else:
292295
pass
293296
#print event
294-
297+
298+
if conf.TOUCH_SCREEN:
299+
mouseAction = self.touchScreen.getEventBoxes()
300+
if mouseAction > 0:
301+
if gameOver:
302+
self.resetGame()
303+
gameOver = False
304+
else:
305+
doMove = mouseAction
306+
295307
if gameOver is False and redrawCount >= (self.gameSpeed - self.gameSpeedFactors[self.gameSpeedFactor]):
296308
# ONLY move, when the timer elapses!
297309
# otherwise you could change the direction multiple times before the scenery changes and upates
@@ -329,6 +341,9 @@ def run_game(self):
329341
for elem in reversed(self.elements):
330342
elem.blit()
331343

344+
# draw touchscreen
345+
# TODO: draw touch areas
346+
332347
# draw pop ups
333348
self.popUp.drawPopUps()
334349

touchScreen.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pygame
2+
3+
4+
class touchScreen(object):
5+
def __init__(self, width, height):
6+
self.width = width
7+
self.height = height
8+
9+
one4width = self.width / 4
10+
one4height = self.height / 4
11+
12+
self.regions = []
13+
self.regions.append(pygame.Rect(one4width, 0, self.width / 2, one4height)) # top
14+
self.regions.append(pygame.Rect(one4width * 3, one4height, one4width, self.height / 2)) # right
15+
self.regions.append(pygame.Rect(one4width, one4height * 3, self.width / 2, one4height)) # down
16+
self.regions.append(pygame.Rect(0, one4height, one4width, self.height / 2)) # left
17+
18+
def getEventBoxes(self):
19+
for i in range(len(self.regions)):
20+
mPosX, mPosY = pygame.mouse.get_pos()
21+
if pygame.mouse.get_pressed()[0] and self.regions[i].collidepoint(mPosX, mPosY):
22+
#print("HIT AREA:" + str(i))
23+
return i + 1

0 commit comments

Comments
 (0)