Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- from twisted.internet import reactor, protocol
- class Player():
- ''' Player object, controlled with the arrow keys'''
- def __init__(self):
- self.image = pygame.image.load('resources/character.png').convert()
- self.rect = self.image.get_rect()
- self.rect.center = (200,200)
- def move(self, directionX, directionY):
- ''' Gets called every frame'''
- self.rect.centerx += directionX * 5
- self.rect.centery += directionY * 5
- class Game():
- '''Main game'''
- screen = pygame.display.set_mode((800,640))
- pygame.display.set_caption('Client')
- background_color = pygame.Surface(screen.get_size()).convert()
- background_color.fill((240,50,0))
- FPS = 30
- player = Player()
- counter = 0
- running = True
- def loop(self):
- '''gets called every frame
- Gets user events, updates game state, and renders the game
- '''
- # checking for user events
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- self.running = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_SPACE:
- print 'space!'
- pressed = pygame.key.get_pressed()
- directionX = pressed[K_d] - pressed[K_a]
- directionY = pressed[K_s] - pressed[K_w]
- # updating the game state
- self.player.move(directionX, directionY)
- # drawing the game state
- self.screen.blit(self.background_color, (0,0))
- self.screen.blit(self.player.image, (self.player.rect.topleft))
- pygame.display.flip()
- # continue the loop
- if self.running == True:
- reactor.callLater((1 / self.FPS), self.loop)
- else:
- reactor.stop()
- class ClientProtocol(protocol.Protocol):
- """Once connected, send a message, then print the result."""
- def connectionMade(self):
- self.transport.write("hello server! the client is started!")
- def dataReceived(self, data):
- "As soon as any data is received, write it back."
- print "just got data from the server:", data
- def connectionLost(self, reason):
- print "connection lost"
- class ClientFactory(protocol.ClientFactory):
- protocol = ClientProtocol
- def clientConnectionFailed(self, connector, reason):
- print "Connection failed - Exiting"
- print reason
- reactor.stop()
- def clientConnectionLost(self, connector, reason):
- print "Connection lost - Exiting"
- #reactor.stop()
- factory = ClientFactory()
- reactor.connectTCP('localhost', 24100, factory)
- reactor.callWhenRunning(Game().loop)
- print 'Start!'
- reactor.run()
- print 'Server has stopped running'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement