Advertisement
Guest User

Twisted Pygame Client

a guest
Mar 13th, 2011
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.86 KB | None | 0 0
  1. import pygame
  2. from pygame.locals import *
  3. from twisted.internet import reactor, protocol
  4.  
  5.  
  6. class Player():
  7.     ''' Player object, controlled with the arrow keys'''
  8.     def __init__(self):
  9.         self.image = pygame.image.load('resources/character.png').convert()
  10.         self.rect = self.image.get_rect()
  11.         self.rect.center = (200,200)
  12.     def move(self, directionX, directionY):
  13.         ''' Gets called every frame'''
  14.         self.rect.centerx += directionX * 5
  15.         self.rect.centery += directionY * 5
  16.  
  17. class Game():
  18.     '''Main game'''
  19.  
  20.     screen = pygame.display.set_mode((800,640))
  21.     pygame.display.set_caption('Client')
  22.     background_color = pygame.Surface(screen.get_size()).convert()
  23.     background_color.fill((240,50,0))
  24.     FPS = 30
  25.  
  26.     player = Player()
  27.     counter = 0
  28.  
  29.     running = True
  30.     def loop(self):
  31.         '''gets called every frame
  32.        Gets user events, updates game state, and renders the game
  33.        '''
  34.  
  35.         # checking for user events
  36.         for event in pygame.event.get():
  37.             if event.type == pygame.QUIT:
  38.                 self.running = False
  39.                
  40.             if event.type == pygame.KEYDOWN:
  41.                 if event.key == pygame.K_SPACE:
  42.                     print 'space!'
  43.                    
  44.         pressed = pygame.key.get_pressed()
  45.         directionX = pressed[K_d] - pressed[K_a]
  46.         directionY = pressed[K_s] - pressed[K_w]
  47.  
  48.         # updating the game state
  49.         self.player.move(directionX, directionY)
  50.  
  51.         # drawing the game state
  52.         self.screen.blit(self.background_color, (0,0))
  53.         self.screen.blit(self.player.image, (self.player.rect.topleft))
  54.        
  55.         pygame.display.flip()
  56.  
  57.         # continue the loop
  58.         if self.running == True:
  59.             reactor.callLater((1 / self.FPS), self.loop)
  60.        
  61.         else:
  62.             reactor.stop()
  63.  
  64.  
  65.  
  66. class ClientProtocol(protocol.Protocol):
  67.     """Once connected, send a message, then print the result."""
  68.    
  69.     def connectionMade(self):
  70.         self.transport.write("hello server! the client is started!")
  71.    
  72.     def dataReceived(self, data):
  73.         "As soon as any data is received, write it back."
  74.         print "just got data from the server:", data
  75.    
  76.     def connectionLost(self, reason):
  77.         print "connection lost"
  78.  
  79. class ClientFactory(protocol.ClientFactory):
  80.     protocol = ClientProtocol
  81.  
  82.     def clientConnectionFailed(self, connector, reason):
  83.         print "Connection failed - Exiting"
  84.         print reason
  85.         reactor.stop()
  86.    
  87.     def clientConnectionLost(self, connector, reason):
  88.         print "Connection lost - Exiting"
  89.         #reactor.stop()
  90.  
  91.  
  92. factory = ClientFactory()
  93. reactor.connectTCP('localhost', 24100, factory)
  94. reactor.callWhenRunning(Game().loop)
  95.  
  96. print 'Start!'
  97. reactor.run()
  98. print 'Server has stopped running'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement