Commit 39a665f3 authored by Aurel Wildfellner's avatar Aurel Wildfellner

pyglet port for canvas

parent 83beb032
......@@ -25,9 +25,10 @@
class PongConfig:
gametitle = "Code Pong"
screensize = width, height = 1920, 1080
#screensize = width, height = 1920, 1000
#screensize = width, height = 1600, 900
screensize = width, height = 1366, 768
#screensize = width, height = 1366, 768
barcodes = {"ball" : "123", "playerleft" : "101", "playerright" : "102"}
......
......@@ -33,8 +33,9 @@ from keyboardreader import KeyboardReader
from readereventqueue import ReaderEvent, ReaderEventQueue, EventType
# drawing
from canvas import Canvas
import pygame
#from canvas import Canvas
from pygletcanvas import PygletCanvas
import pyglet
# game core
import gamesim
......@@ -81,7 +82,7 @@ class PongGame:
self.sim = gamesim.GameSim(self.ball, self.gamefield)
# setup rendering
self.canvas = Canvas(self.ball, self.gamefield, self.leftPlayer, self.rightPlayer)
self.canvas = PygletCanvas(self.ball, self.gamefield, self.leftPlayer, self.rightPlayer)
def setupControllers(self, numControllers=2):
......@@ -182,12 +183,12 @@ class PongGame:
# End of Game?
if self.leftPlayer.lifes <= 0:
self.menuText = "Right Player Wins!"
self.gameState = GameState.GAMEEND
self.gameState = GameState.STARTGAME
print("RIGHT PLAYER WINS!")
return
if self.rightPlayer.lifes <= 0:
self.menuText = "Left Player Wins!"
self.gameState = GameState.GAMEEND
self.gameState = GameState.STARTGAME
print("LEFT PLAYER WINS!")
return
......@@ -272,20 +273,22 @@ def main():
utils.disableBarcodeKeyboards()
pg = PongGame()
DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
if not pg.setupControllers():
print("Controller Error")
#exit(1)
while True:
if pg.gameState == GameState.GAMEEND:
print("REACHED END OF GAME")
pygame.quit()
pid = os.getpid()
subprocess.call(["kill", "-9", str(pid)])
# if pg.gameState == GameState.GAMEEND:
# print("REACHED END OF GAME")
# pygame.quit()
# pid = os.getpid()
# subprocess.call(["kill", "-9", str(pid)])
def gameLoop(foo):
pg.gameLoop()
pyglet.clock.schedule_interval(gameLoop, 1/60.)
pyglet.app.run()
#TODO catch exceptions and stop barcode reader threads
......
# -*- coding: utf-8 -*-
import pyglet
from player import Player
from pongconfig import PongConfig
class Zone():
def __init__(self, gamefield, (ratio, height)):
self.gamefield = gamefield
self.ratio = ratio
self.height = height
self.color = (230, 230, 230,
230, 230, 230,
230, 230, 230,
230, 230, 230,
230, 230, 230,
230, 230, 230)
class LeftZone(Zone):
def __init__(self, gamefield, (ratio, height)):
Zone.__init__(self, gamefield, (ratio, height))
def draw(self):
pyglet.graphics.draw(6, pyglet.gl.GL_TRIANGLES,
('v2i', (0, 0,
0, int(self.height),
int(self.gamefield.leftZoneSize * self.ratio), int(self.height),
0, 0,
int(self.gamefield.leftZoneSize * self.ratio), 0,
int(self.gamefield.leftZoneSize * self.ratio), int(self.height))),
('c3B', self.color))
class RightZone(Zone):
def __init__(self, gamefield, (ratio, height)):
Zone.__init__(self, gamefield, (ratio, height))
def draw(self):
pyglet.graphics.draw(6, pyglet.gl.GL_TRIANGLES,
('v2i', (int(self.ratio), 0,
int(self.ratio), int(self.height),
int(self.ratio) - int(self.gamefield.rightZoneSize * self.ratio),
int(self.height),
int(self.ratio) - int(self.gamefield.rightZoneSize * self.ratio),
int(self.height),
int(self.ratio) - int(self.gamefield.rightZoneSize * self.ratio),
0,
int(self.ratio), 0)),
('c3B', self.color))
class BallView():
def __init__(self, model, ratio):
self.model = model
self.ratio = ratio
self.size = (
int(self.model.radius * 2 * self.ratio),
int(self.model.radius * 2 * self.ratio)
)
self.image = pyglet.image.load('../img/ball.png')
#self.ball = BallView(ball, self.ratio, self.ball_image)
self.sprite = pyglet.sprite.Sprite(img=self.image)
self.sprite.scale = (self.size[0] / float(self.sprite.width))
print(self.sprite.scale)
def update(self):
self.sprite.x = (self.model.position[0] - self.model.radius) * self.ratio
self.sprite.y = (self.model.position[1] - self.model.radius) * self.ratio
def draw(self):
self.sprite.draw()
class PlayerCode1():
def __init__(self, (ratio, height)):
self.ratio = ratio
self.height = height
self.ready = False
self.image = pyglet.image.load('../img/101.png')
self.size = (
int(self.ratio / 7),
int(self.ratio / 7)
)
self.sprite = pyglet.sprite.Sprite(img=self.image)
self.sprite.scale = (self.size[0] / self.sprite.width)
self.sprite.x = (self.ratio/4) - (self.size[0]/2)
self.sprite.y = (self.height/2) - (self.size[1]/2)
self.label = pyglet.text.Label('Player 1',
font_name='Arial',
color=(0,0,0,255),
font_size=46,
x=self.sprite.x + self.sprite.width/2, y=self.sprite.y - 50,
anchor_x='center', anchor_y='center')
def draw(self):
self.sprite.draw()
self.label.draw()
class PlayerCode2():
def __init__(self, (ratio, height)):
self.ratio = ratio
self.height = height
self.ready = False
self.image = pyglet.image.load('../img/102.png')
self.size = (
int(self.ratio / 7),
int(self.ratio / 7)
)
self.sprite = pyglet.sprite.Sprite(img=self.image)
self.sprite.scale = (self.size[0] / self.sprite.width)
self.sprite.x = (self.ratio/4*3) - (self.size[0]/2)
self.sprite.y = self.height/2 - (self.size[1]/2)
self.label = pyglet.text.Label('Player 2',
font_name='Arial',
color=(0,0,0,255),
font_size=46,
x=self.sprite.x + self.sprite.width/2, y=self.sprite.y - 50,
anchor_x='center', anchor_y='center')
def draw(self):
self.sprite.draw()
self.label.draw()
class Heart():
def __init__(self, (ratio, height), side, i):
self.ratio = ratio
self.height = height
self.image = pyglet.image.load('../img/heart.png')
self.sprite = pyglet.sprite.Sprite(img=self.image)
self.sprite.y = 10
if side == 'right':
self.sprite.x = self.ratio - 40*i
elif side == 'left':
self.sprite.x = 40*i - 30
def draw(self):
self.sprite.draw()
class PygletCanvas(pyglet.window.Window):
def __init__(self, ball, gamefield, leftPlayer, rightPlayer):
self.size = PongConfig.screensize
super(PygletCanvas, self).__init__(*(self.size))
# white background
pyglet.gl.glClearColor(1.0, 1.0, 1.0, 0.0)
self.clear()
self.leftPlayer = leftPlayer
self.rightPlayer = rightPlayer
self.hearts = []
self.ratio = self.size[0]
self.stage = 'menu'
### sprites
self.ball = BallView(ball, self.ratio)
self.code1 = PlayerCode1(self.size)
self.code2 = PlayerCode2(self.size)
self.leftZone = LeftZone(gamefield, self.size)
self.rightZone = RightZone(gamefield, self.size)
self.leftPlayerHearts = [Heart(self.size, 'left', i) for i in range(1, 4)]
self.rightPlayerHearts = [Heart(self.size, 'right', i) for i in range(1, 4)]
def showGame(self):
self.stage = 'game'
def showMenu(self, splashtext):
self.stage = 'menu'
def on_draw(self):
pyglet.gl.glClearColor(1.0, 1.0, 1.0, 0.0)
self.clear()
if self.stage == 'menu':
self.code1.draw()
self.code2.draw()
elif self.stage == 'game':
self.leftZone.draw()
self.rightZone.draw()
for i in range(self.leftPlayer.lifes):
self.leftPlayerHearts[i].draw()
for i in range(self.rightPlayer.lifes):
self.rightPlayerHearts[i].draw()
self.ball.update()
self.ball.draw()
pass
def update(self):
if self.stage == 'menu':
pass
elif self.stage == 'game':
self.ball.update()
#!/usr/bin/python2
import pyglet
import pygletcanvas
import gameelements
from pongconfig import PongConfig
def main():
ball = gameelements.Ball()
gamefield = gameelements.GameField()
gamefield.setAspectRatio(*PongConfig.screensize)
canvas = pygletcanvas.PygletCanvas(ball, None, None, None)
pyglet.app.run()
if __name__ == "__main__":
main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment