Browse Source

Changed namespace structure. Added shell scripts for easier
execution.

LightCycle colours added as well.

Bryan Allred 14 years ago
parent
commit
f73025d6da

+ 3 - 0
.settings/org.eclipse.ltk.core.refactoring.prefs

1
#Fri Jun 10 15:49:49 EDT 2011
2
eclipse.preferences.version=1
3
org.eclipse.ltk.core.refactoring.enable.project.refactoring.history=false

+ 17 - 1
README.txt

1
Program:	PyTron
1
Program:	PyTron
2
Author:		Bryan M. Allred
2
Author:		Bryan M. Allred
3
Created:	06/03/2011
3
Created:	06/03/2011
4
Modified:   06/10/2011
4
5
5
Dependencies:
6
Dependencies:
6
	Python 2.7
7
	Python 2.7
16
	Pygame documentation.
17
	Pygame documentation.
17
	Alec Thomas on the sweet Enum() function found on StackOverflow (I felt I needed enumerations!).
18
	Alec Thomas on the sweet Enum() function found on StackOverflow (I felt I needed enumerations!).
18
19
20
Running the game:
21
22
Windows
23
24
	Assuming you are running Python 2.7 and it is installed at C:\Python27\ just run the "run.bat"
25
	batch file. If Python is at a different version or located somewhere else please edit the batch
26
	file accordingly.
27
28
Linux (only tested on Ubuntu)
29
30
	Execute the shell script "run.sh". This **should** take care of everything.
31
19
Directions:
32
Directions:
20
33
21
Avoid hitting the game grids boundaries as well as each programs light ribbon. Currently each
34
Avoid hitting the game grids boundaries as well as each programs light ribbon. Currently each
46
the amount of items in the lists which may have been causing the performance
59
the amount of items in the lists which may have been causing the performance
47
degradation.
60
degradation.
48
61
49
[Unresolved]
62
[Fixed]
50
Problem: Never got around to implementing different light cycle colors.
63
Problem: Never got around to implementing different light cycle colors.
51
64
65
Change: Changed the enumeration of colors to RBG values. The color is determined by the program
66
number (or ID) and is fixed once the program uses his or her baton.
67
52
[Unresolved]
68
[Unresolved]
53
Problem: Sound.
69
Problem: Sound.

+ 5 - 0
run.bat

1
@echo off
2
3
set PATH=C:\Python27
4
5
%PATH%\python.exe src/pytron/PyTron.py

+ 3 - 0
run.sh

1
#!/bin/sh
2
3
python src/pytron/PyTron.py

BIN
src/pytron/Enum.pyc


BIN
src/pytron/GameGrid.pyc


BIN
src/pytron/LightCycle.pyc


BIN
src/pytron/Program.pyc


+ 42 - 34
src/pytron/PyTron.py

1
#!/usr/bin/python
1
#!/usr/bin/python
2
2
3
from pytron.GameGrid import GameGrid
4
from pytron.Program import Program
5
from pytron.locals.Heading import Heading
6
7
#
8
# Build the game grid.
9
#
10
11
gameGrid = GameGrid(640, 480)
12
gameGrid.SetFrameRate(30)
13
14
#
15
# Create the programs (players).
16
#
17
18
clu = Program(0)
19
clu.UseBaton(Heading.SOUTH, 10, 10)
20
21
ram = Program(1)
22
ram.UseBaton(Heading.NORTH, gameGrid.Width - 10, gameGrid.Height - 10)
23
24
gameGrid.LoadProgram(clu)
25
gameGrid.LoadProgram(ram)
26
27
#
28
# Engine.
29
#
30
31
while gameGrid.IsRunning():
32
    gameGrid.DoWork()
33
34
#
35
# End of the world.
36
#
3
from tronverse.Heading import Heading
4
from tronverse.Program import Program
5
from tronverse.GameGrid import GameGrid
6
7
def startGame():
8
    '''
9
    Start game play.
10
    '''
11
    
12
    #
13
    # Build the game grid.
14
    #
15
    
16
    gameGrid = GameGrid(640, 480)
17
    gameGrid.SetFrameRate(30)
18
    
19
    #
20
    # Create the programs (players).
21
    #
22
    
23
    clu = Program(0)
24
    clu.UseBaton(Heading.SOUTH, 10, 10)
25
    
26
    ram = Program(1)
27
    ram.UseBaton(Heading.NORTH, gameGrid.Width - 10, gameGrid.Height - 10)
28
    
29
    #
30
    # Load programs in to the game grid.
31
    #
32
    
33
    gameGrid.LoadProgram(clu)
34
    gameGrid.LoadProgram(ram)
35
    
36
    #
37
    # Engine.
38
    #
39
    
40
    while gameGrid.IsRunning():
41
        gameGrid.DoWork()
42
43
if __name__ == "__main__":
44
    startGame()

BIN
src/pytron/__init__.pyc


+ 0 - 3
src/pytron/locals/Color.py

1
from pytron.Enum import Enum
2
3
Color = Enum(RED=0, BLUE=1, GREEN=2, YELLOW=3, WHITE=4)

BIN
src/pytron/locals/Color.pyc


+ 0 - 3
src/pytron/locals/Heading.py

1
from pytron.Enum import Enum
2
3
Heading = Enum(NORTH=0, EAST=1, SOUTH=2, WEST=3)

BIN
src/pytron/locals/Heading.pyc


BIN
src/pytron/locals/__init__.pyc


+ 3 - 0
src/pytron/tronverse/Color.py

1
from tronverse.Enum import Enum
2
3
Color = Enum(RED = (255, 0, 0), BLUE = (0, 0, 255), GREEN = (0, 192, 0), YELLOW = (255, 255, 0), WHITE = (255, 255, 255), BLACK = (0, 0, 0))

+ 1 - 2
src/pytron/Enum.py

6
6
7
def Enum(*sequential, **named):
7
def Enum(*sequential, **named):
8
    enums = dict(zip(sequential, range(len(sequential))), **named)
8
    enums = dict(zip(sequential, range(len(sequential))), **named)
9
    return type('Enum', (), enums)
10
        
9
    return type('Enum', (), enums)

+ 7 - 6
src/pytron/GameGrid.py

1
import pygame
1
import pygame
2
from pygame.locals import *
2
from pygame.locals import *
3
from pytron.locals.Heading import Heading
3
from tronverse.Color import Color
4
from tronverse.Heading import Heading
4
5
5
class GameGrid:
6
class GameGrid(object):
6
    '''
7
    '''
7
    The Game Grid is where programs compete for their survival.
8
    The Game Grid is where programs compete for their survival.
8
    '''
9
    '''
24
        self._Clock = pygame.time.Clock()
25
        self._Clock = pygame.time.Clock()
25
        self._Screen = pygame.display.set_mode((self.Width, self.Height))
26
        self._Screen = pygame.display.set_mode((self.Width, self.Height))
26
        self._Background = pygame.Surface(self._Screen.get_size())
27
        self._Background = pygame.Surface(self._Screen.get_size())
27
        self._Background.fill((0, 0, 0))
28
        self._Background.fill(Color.BLACK)
28
    
29
    
29
    def DoWork(self):
30
    def DoWork(self):
30
        # Tick-Tock.
31
        # Tick-Tock.
38
        
39
        
39
        for prog in self._Programs:
40
        for prog in self._Programs:
40
            # Move and draw.
41
            # Move and draw.
41
            self._DrawLightRibbon(prog.LightCycle.Move())
42
            self._DrawLightRibbon(prog.LightCycle.Move(), prog.LightCycle.RibbonColor)
42
            
43
            
43
            # Collision checking.
44
            # Collision checking.
44
            if prog.LightCycle.CollisionWithWall(self.Width, self.Height):
45
            if prog.LightCycle.CollisionWithWall(self.Width, self.Height):
77
        
78
        
78
        self._FrameRate = Rate
79
        self._FrameRate = Rate
79
    
80
    
80
    def _DrawLightRibbon(self, Vertices):
81
    def _DrawLightRibbon(self, Vertices, Color = Color.RED):
81
        '''
82
        '''
82
        Draws the light ribbon.
83
        Draws the light ribbon.
83
        '''
84
        '''
84
        
85
        
85
        pygame.draw.lines(self._Screen, (255, 0, 0), False, Vertices, 1)
86
        pygame.draw.lines(self._Screen, Color, False, Vertices, 1)
86
    
87
    
87
    def _HandleEvents(self, Events):
88
    def _HandleEvents(self, Events):
88
        '''
89
        '''

+ 3 - 0
src/pytron/tronverse/Heading.py

1
from tronverse.Enum import Enum
2
3
Heading = Enum(NORTH = 0, EAST = 1, SOUTH = 2, WEST = 3)

+ 4 - 3
src/pytron/LightCycle.py

1
from pytron.locals.Heading import Heading
1
from tronverse.Color import Color
2
from tronverse.Heading import Heading
2
3
3
class LightCycle:
4
class LightCycle(object):
4
    '''
5
    '''
5
    Light cycle vehicle
6
    Light cycle vehicle
6
    '''
7
    '''
7
    
8
    
8
    def __init__(self, Color, Direction, StartX, StartY):
9
    def __init__(self, Color, Direction = Heading.NORTH, StartX = 0, StartY = 0):
9
        '''
10
        '''
10
        Initializes a new instance of the LightCycle class.
11
        Initializes a new instance of the LightCycle class.
11
        '''
12
        '''

+ 18 - 4
src/pytron/Program.py

1
from pytron.LightCycle import LightCycle
1
from tronverse.Color import Color
2
from tronverse.LightCycle import LightCycle
2
3
3
class Program:
4
class Program(object):
4
    '''
5
    '''
5
    A user program
6
    A user program
6
    '''
7
    '''
36
        '''
37
        '''
37
        Use the baton to generate the light cycle.
38
        Use the baton to generate the light cycle.
38
        '''
39
        '''
39
        
40
        self.LightCycle = LightCycle(self._Id, Direction, StartX, StartY)
40
41
        color = Color.RED
42
43
        if self._Id == 0:
44
            color = Color.RED
45
        elif self._Id == 1:
46
            color = Color.BLUE
47
        elif self._Id == 2:
48
            color = Color.GREEN
49
        elif self._Id == 3:
50
            color = Color.YELLOW
51
        elif self._Id == 4:
52
            color = Color.WHITE
53
54
        self.LightCycle = LightCycle(color, Direction, StartX, StartY)

src/pytron/locals/__init__.py → src/pytron/tronverse/__init__.py