Added several possible features to single-player, including infinite hits (mess-around-with-Physics mode) and an "AI" player.

This commit is contained in:
jpenner 2005-06-15 18:09:46 +00:00
parent e904d0bf05
commit 251444bc15
2 changed files with 23 additions and 8 deletions

View file

@ -10,7 +10,7 @@
<vnodes> <vnodes>
<v t="jpenner.20050319120705" a="E"><vh>Files</vh> <v t="jpenner.20050319120705" a="E"><vh>Files</vh>
<v t="jpenner.20050305105206" a="E" <v t="jpenner.20050305105206" a="E"
expanded="jpenner.20050601180947,jpenner.20050305124252,jpenner.20050319143816,jpenner.20050604112932,jpenner.20050605102056,jpenner.20050305121157,jpenner.20050306103246,jpenner.20050305122430,"><vh>@thin TennisForTwo.py</vh></v> expanded="jpenner.20050601180947,jpenner.20050305124252,jpenner.20050319143816,jpenner.20050604112932,jpenner.20050605102056,jpenner.20050305121157,jpenner.20050604175241,jpenner.20050305120654,jpenner.20050320112219,jpenner.20050306103246,jpenner.20050305122430,"><vh>@thin TennisForTwo.py</vh></v>
<v t="jpenner.20050604112932.1" <v t="jpenner.20050604112932.1"
expanded="jpenner.20050604112932.6,jpenner.20050604112932.16,jpenner.20050604112932.30,jpenner.20050604112932.41,"><vh>@thin widgets.py</vh></v> expanded="jpenner.20050604112932.6,jpenner.20050604112932.16,jpenner.20050604112932.30,jpenner.20050604112932.41,"><vh>@thin widgets.py</vh></v>
<v t="jpenner.20050604113053" <v t="jpenner.20050604113053"
@ -26,7 +26,7 @@ expanded="jpenner.20050604113053.2,"><vh>@thin widgetsdemo.py</vh></v>
</v> </v>
<v t="jpenner.20050319120705.1" a="E"><vh>Utilities</vh> <v t="jpenner.20050319120705.1" a="E"><vh>Utilities</vh>
<v t="jpenner.20050305121227" a="E"><vh>@run TennisForTwo.py</vh></v> <v t="jpenner.20050305121227" a="E"><vh>@run TennisForTwo.py</vh></v>
<v t="jpenner.20050320113047"><vh>@run TennisForTwo.py client localhost 7554</vh></v> <v t="jpenner.20050320113047" a="E"><vh>@run TennisForTwo.py client localhost 7554</vh></v>
<v t="jpenner.20050604113053.7" a="E"><vh>@run widgetsdemo.py</vh></v> <v t="jpenner.20050604113053.7" a="E"><vh>@run widgetsdemo.py</vh></v>
<v t="jpenner.20050307190605"><vh>@run PyGame2Exe.py py2exe</vh></v> <v t="jpenner.20050307190605"><vh>@run PyGame2Exe.py py2exe</vh></v>
<v t="jpenner.20050604144534.1"><vh>@run python2.4 pygame2app.py py2app</vh></v> <v t="jpenner.20050604144534.1"><vh>@run python2.4 pygame2app.py py2app</vh></v>

View file

@ -2,7 +2,7 @@
#@+node:jpenner.20050305105206:@thin TennisForTwo.py #@+node:jpenner.20050305105206:@thin TennisForTwo.py
#@@language python #@@language python
import pygame, math, cPickle, sys, StringIO, socket, re, urllib import pygame, math, cPickle, sys, StringIO, socket, re, urllib, random
from pygame.locals import * from pygame.locals import *
from twisted.internet import task, reactor, protocol, udp from twisted.internet import task, reactor, protocol, udp
from widgets import * from widgets import *
@ -25,6 +25,7 @@ SINGLE_PLAYER = 10
#globals #globals
class Game: class Game:
currentplayer = 1 currentplayer = 1
AIPlayer = 0
SeverPort = 7554 SeverPort = 7554
Port = None Port = None
#@nonl #@nonl
@ -49,6 +50,8 @@ def makeAngle(pos):
def anglePos(rect, angle, radius): def anglePos(rect, angle, radius):
return( (rect.centerx + (math.cos(angle) * radius)), (rect.centery + (math.sin(angle) * radius))) return( (rect.centerx + (math.cos(angle) * radius)), (rect.centery + (math.sin(angle) * radius)))
def velocityFromAngle(angle):
return (math.cos(angle) * 12, math.sin(angle) * 9)
#@-node:jpenner.20050427175747:Angles #@-node:jpenner.20050427175747:Angles
#@+node:jpenner.20050605110605:IP address #@+node:jpenner.20050605110605:IP address
def getMyIP(): def getMyIP():
@ -123,7 +126,7 @@ class Event:
self.fromplayer = Game.myplayer self.fromplayer = Game.myplayer
class ClickEvent (Event): class ClickEvent (Event):
def __init__(self, xvel, yvel, player): def __init__(self, (xvel, yvel), player):
Event.__init__(self, EV_CLICK) Event.__init__(self, EV_CLICK)
self.xvel = xvel self.xvel = xvel
self.yvel = yvel self.yvel = yvel
@ -283,9 +286,11 @@ class MainMenu(BaseMenu):
#@nl #@nl
def startSingle(self): def startSingle(self):
Game.myplayer = SINGLE_PLAYER # Game.myplayer = SINGLE_PLAYER
Game.myplayer = 1
Game.isClient = False Game.isClient = False
Game.rules.infinitehits = True # Game.rules.infinitehits = True
Game.AIPlayer = 2
Game.gameMgr.changeState(Game.gameMgr.STATE_SINGLE_PLAYER) Game.gameMgr.changeState(Game.gameMgr.STATE_SINGLE_PLAYER)
def startServer(self): def startServer(self):
@ -611,6 +616,14 @@ class NetworkPhysicsEngine:
#@-others #@-others
#@nonl #@nonl
#@-node:jpenner.20050320112219:Physics #@-node:jpenner.20050320112219:Physics
#@+node:jpenner.20050615085910:AI
class AIPlayer:
def tick(self):
if random.randint(0,13) == 1:
angle = math.radians(random.randint(180 + 10, 270 - 10))
Game.evMgr.postEvent( ClickEvent(velocityFromAngle(angle), Game.AIPlayer) )
#@-node:jpenner.20050615085910:AI
#@+node:jpenner.20050307180329:Sound #@+node:jpenner.20050307180329:Sound
class SoundEngine: class SoundEngine:
def __init__(self): def __init__(self):
@ -670,7 +683,8 @@ class InputManager:
def sdl_event(self, event): def sdl_event(self, event):
if event.type == MOUSEBUTTONUP: if event.type == MOUSEBUTTONUP:
angle = makeAngle(event.pos) angle = makeAngle(event.pos)
Game.evMgr.postEvent( ClickEvent(math.cos(angle) * 12, math.sin(angle) * 9, Game.myplayer) ) log("userangle: " + str(math.degrees(angle)))
Game.evMgr.postEvent( ClickEvent(velocityFromAngle(angle), Game.myplayer) )
if event.type == KEYUP: if event.type == KEYUP:
Game.evMgr.postEvent( ServeEvent() ) Game.evMgr.postEvent( ServeEvent() )
@ -748,7 +762,7 @@ class GameMgr (StateMachine):
StateMachine.__init__(self, StateMachine.__init__(self,
[GameState(MainMenu(), mainMenu.onEnter), [GameState(MainMenu(), mainMenu.onEnter),
GameState(GameLoop([inputMgr, Game.evMgr, self.physicsMgr, self.graphicsMgr]), self.gameEnter), GameState(GameLoop([inputMgr, Game.evMgr, self.physicsMgr, self.graphicsMgr]), self.gameEnter),
GameState(GameLoop([inputMgr, Game.evMgr, PhysicsEngine(), self.graphicsMgr]), self.gameEnter), GameState(GameLoop([inputMgr, AIPlayer(), Game.evMgr, PhysicsEngine(), self.graphicsMgr]), self.gameEnter),
GameState(GameLoop([Game.evMgr, self.serverWait]), self.serverEnter, self.networkExit), GameState(GameLoop([Game.evMgr, self.serverWait]), self.serverEnter, self.networkExit),
GameState(GameLoop([Game.evMgr, self.clientWait]), self.clientEnter, self.networkExit) GameState(GameLoop([Game.evMgr, self.clientWait]), self.clientEnter, self.networkExit)
]) ])
@ -782,6 +796,7 @@ class GameMgr (StateMachine):
#@-node:jpenner.20050305122430:Game Loop #@-node:jpenner.20050305122430:Game Loop
#@+node:jpenner.20050319125841:Setup #@+node:jpenner.20050319125841:Setup
def main(): def main():
random.seed()
pygame.init() pygame.init()
startLogging('tennis.log') startLogging('tennis.log')