From 251444bc15d9fe642c795c5280bcfa0c6cfe191f Mon Sep 17 00:00:00 2001 From: jpenner Date: Wed, 15 Jun 2005 18:09:46 +0000 Subject: [PATCH] Added several possible features to single-player, including infinite hits (mess-around-with-Physics mode) and an "AI" player. --- TennisForTwo.leo | 4 ++-- TennisForTwo.py | 27 +++++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/TennisForTwo.leo b/TennisForTwo.leo index 73c65ea..e36e92d 100644 --- a/TennisForTwo.leo +++ b/TennisForTwo.leo @@ -10,7 +10,7 @@ Files @thin TennisForTwo.py +expanded="jpenner.20050601180947,jpenner.20050305124252,jpenner.20050319143816,jpenner.20050604112932,jpenner.20050605102056,jpenner.20050305121157,jpenner.20050604175241,jpenner.20050305120654,jpenner.20050320112219,jpenner.20050306103246,jpenner.20050305122430,">@thin TennisForTwo.py @thin widgets.py @thin widgetsdemo.py Utilities @run TennisForTwo.py -@run TennisForTwo.py client localhost 7554 +@run TennisForTwo.py client localhost 7554 @run widgetsdemo.py @run PyGame2Exe.py py2exe @run python2.4 pygame2app.py py2app diff --git a/TennisForTwo.py b/TennisForTwo.py index 209e4c4..7de787b 100644 --- a/TennisForTwo.py +++ b/TennisForTwo.py @@ -2,7 +2,7 @@ #@+node:jpenner.20050305105206:@thin TennisForTwo.py #@@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 twisted.internet import task, reactor, protocol, udp from widgets import * @@ -25,6 +25,7 @@ SINGLE_PLAYER = 10 #globals class Game: currentplayer = 1 + AIPlayer = 0 SeverPort = 7554 Port = None #@nonl @@ -49,6 +50,8 @@ def makeAngle(pos): def anglePos(rect, 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.20050605110605:IP address def getMyIP(): @@ -123,7 +126,7 @@ class Event: self.fromplayer = Game.myplayer class ClickEvent (Event): - def __init__(self, xvel, yvel, player): + def __init__(self, (xvel, yvel), player): Event.__init__(self, EV_CLICK) self.xvel = xvel self.yvel = yvel @@ -283,9 +286,11 @@ class MainMenu(BaseMenu): #@nl def startSingle(self): - Game.myplayer = SINGLE_PLAYER +# Game.myplayer = SINGLE_PLAYER + Game.myplayer = 1 Game.isClient = False - Game.rules.infinitehits = True +# Game.rules.infinitehits = True + Game.AIPlayer = 2 Game.gameMgr.changeState(Game.gameMgr.STATE_SINGLE_PLAYER) def startServer(self): @@ -611,6 +616,14 @@ class NetworkPhysicsEngine: #@-others #@nonl #@-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 class SoundEngine: def __init__(self): @@ -670,7 +683,8 @@ class InputManager: def sdl_event(self, event): if event.type == MOUSEBUTTONUP: 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: Game.evMgr.postEvent( ServeEvent() ) @@ -748,7 +762,7 @@ class GameMgr (StateMachine): StateMachine.__init__(self, [GameState(MainMenu(), mainMenu.onEnter), 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.clientWait]), self.clientEnter, self.networkExit) ]) @@ -782,6 +796,7 @@ class GameMgr (StateMachine): #@-node:jpenner.20050305122430:Game Loop #@+node:jpenner.20050319125841:Setup def main(): + random.seed() pygame.init() startLogging('tennis.log')