From: <Blu...@us...> - 2010-09-11 23:32:21
|
Revision: 401 http://virtplayground.svn.sourceforge.net/virtplayground/?rev=401&view=rev Author: BlueWolf_ Date: 2010-09-11 23:32:12 +0000 (Sat, 11 Sep 2010) Log Message: ----------- Starting with the avatar-code. Code isn't much commented yet as the code will change a lot with every new change. Most is still temporary Modified Paths: -------------- trunk/client/playground.py Added Paths: ----------- trunk/client/playground_avatar.py Modified: trunk/client/playground.py =================================================================== --- trunk/client/playground.py 2010-09-11 17:53:53 UTC (rev 400) +++ trunk/client/playground.py 2010-09-11 23:32:12 UTC (rev 401) @@ -17,6 +17,7 @@ from functions import * import gui +from playground_avatar import Avatar class Playground(): @@ -39,7 +40,10 @@ # Playground stuff self.backgrounds = {} self.viewport = None - self.direction_pressed = None + self.direction = None + self.avatars = {} + self.this_user = None + self.avatar_offset = None def event(self, ev): @@ -59,30 +63,49 @@ elif self.status == "playground": - if ev.type == KEYDOWN: - if ev.key == K_UP and self.direction_pressed != 0: - self.direction_pressed = 0 - sh['timer'].start("playground-move", self.playground_move, \ - time.time(), self.viewport.top, 0) - elif ev.key == K_DOWN and self.direction_pressed != 1: - self.direction_pressed = 1 - sh['timer'].start("playground-move", self.playground_move, \ - time.time(), self.viewport.top, 1) - elif ev.key == K_LEFT and self.direction_pressed != 2: - self.direction_pressed = 2 - sh['timer'].start("playground-move", self.playground_move, \ - time.time(), self.viewport.left, 2) - elif ev.key == K_RIGHT and self.direction_pressed != 3: - self.direction_pressed = 3 - sh['timer'].start("playground-move", self.playground_move, \ - time.time(), self.viewport.left, 3) - - elif ev.type == KEYUP: + if ev.type == KEYDOWN or ev.type == KEYUP: + if ev.key in [K_UP, K_DOWN, K_LEFT, K_RIGHT]: - self.direction_pressed = None - sh['timer'].stop("playground-move") - + keys = pygame.key.get_pressed() + keys = (keys[K_UP], keys[K_RIGHT], keys[K_DOWN], + keys[K_LEFT]) + + direction = None + # Multiple keys + if keys[0] and keys[1]: + direction = 1 + elif keys[1] and keys[2]: + direction = 3 + elif keys[2] and keys[3]: + direction = 5 + elif keys[3] and keys[0]: + direction = 7 + + # Single keys + elif keys[0]: + direction = 0 + elif keys[1]: + direction = 2 + elif keys[2]: + direction = 4 + elif keys[3]: + direction = 6 + + if direction == None and self.direction != None: + # Stop the timer. We are not walking + sh['timer'].stop("playground-walk") + + elif self.direction == None or \ + direction != self.direction[0]: + # We started walking or changed direction + + self.direction = (direction, time.time(), + self.this_user['pos'][:]) + sh['timer'].start("playground-walk", + self.playground_walk) + + def call(self, name, data): if name == "status": # A status update if data['status'] == "login": @@ -94,6 +117,11 @@ for name in self.backgrounds.keys(): sh['downloader'].remove_usage(name) self.backgrounds = {} + self.viewport = None + self.direction = None + self.avatars = {} + self.this_user = None + self.avatar_offset = None # Load the data for the login self.loginbg = load_image(False, "images", "loginbg.png") @@ -124,7 +152,30 @@ self.logingui = None # Load the data for the playground - self.viewport = Rect(0, 0, 1000, 700) + + # Load all users + self.this_user = sh['client'].userlist[sh['client'].cid] + for user in sh['client'].userlist.values(): + if user['pos'] != None: + self.avatars[user['cid']] = Avatar(user) + + to_center = ( + (self.surf.get_width()/2) - \ + (self.avatars[self.this_user['cid']].size[0]/2), + (self.surf.get_height()/2) - \ + (self.avatars[self.this_user['cid']].size[1]/2), + ) + self.avatar_offset = ( + self.this_user['pos'][0] - to_center[0], + self.this_user['pos'][1] - to_center[1],\ + ) + + self.viewport = Rect( + self.avatar_offset[0], + self.avatar_offset[1], + 1000, + 700) + self.oldsurf = self.surf.copy() self.transit = 1 self.playground_render(None, True) @@ -232,7 +283,7 @@ rect = Rect(rect) # Blit the background - bg_offset = (self.viewport.left%250, self.viewport.top%250) + bg_offset = (-self.viewport.left%250, -self.viewport.top%250) x_min = bg_offset[0]-250 if x_min == -250: x_min = 0 x_max = x_min + 250 * ((float(self.viewport.width) / 250)+1) @@ -257,6 +308,25 @@ self.surf.blit(background, rect, bg_area) + # Blit the users + for avatar in self.avatars.values(): + avatar_rect = Rect( + avatar.user['pos'][0] - self.viewport[0], + avatar.user['pos'][1] - self.viewport[1], + avatar.size[0], + avatar.size[1], + ) + + if not rect.colliderect(avatar_rect): continue + + avatar_area = Rect( + rect.left - avatar_rect.left, + rect.top - avatar_rect.top, + rect.width, + rect.height) + avatar.blit(self.surf, avatar_area) + + if self.transit != None: # An transition is happening self.oldsurf.set_alpha(self.transit*255) self.surf.blit(self.oldsurf, (0,0)) @@ -293,21 +363,37 @@ self.playground_render() - def playground_move(self, name, startime, startpos, direction): - # Temporary test function to walk - - timeline = time.time() - startime + def playground_walk(self, name): + direction, starttime, startpos = self.direction + timeline = time.time() - starttime speed = 175 move = timeline*speed if direction == 0: - self.viewport.top = startpos - move + self.this_user['pos'][1] = startpos[1] - move elif direction == 1: - self.viewport.top = startpos + move + self.this_user['pos'][1] = startpos[1] - (move/1.7) + self.this_user['pos'][0] = startpos[0] + (move/1.7) elif direction == 2: - self.viewport.left = startpos - move + self.this_user['pos'][0] = startpos[0] + move elif direction == 3: - self.viewport.left = startpos + move + self.this_user['pos'][0] = startpos[0] + (move/1.7) + self.this_user['pos'][1] = startpos[1] + (move/1.7) + elif direction == 4: + self.this_user['pos'][1] = startpos[1] + move + elif direction == 5: + self.this_user['pos'][1] = startpos[1] + (move/1.7) + self.this_user['pos'][0] = startpos[0] - (move/1.7) + elif direction == 6: + self.this_user['pos'][0] = startpos[0] - move + elif direction == 7: + self.this_user['pos'][0] = startpos[0] - (move/1.7) + self.this_user['pos'][1] = startpos[1] - (move/1.7) + + self.viewport.topleft = ( + self.this_user['pos'][0]+self.avatar_offset[0], + self.this_user['pos'][1]+self.avatar_offset[1], + ) self.playground_render() Added: trunk/client/playground_avatar.py =================================================================== --- trunk/client/playground_avatar.py (rev 0) +++ trunk/client/playground_avatar.py 2010-09-11 23:32:12 UTC (rev 401) @@ -0,0 +1,34 @@ +## This file is part of Virtual Playground +## Copyright (c) 2009 Jos Ratsma + Koen Koning + +## This program is free software; you can redistribute it and/or +## modify it under the terms of the GNU General Public License +## as published by the Free Software Foundation; either +## version 2 of the License, or (at your option) any later version. + +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. + +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +from functions import * + +class Avatar(): + def __init__(self, user): + self.user = user + self.lastpos = user['pos'] + + self.avatar = None + self.size = (40,40) + sh['downloader'].get_file("happyblock.png", "img", self.download) + + def download(self, filename, cache): + self.avatar = cache + + def blit(self, surf, area): + if self.avatar: + surf.blit(self.avatar, (0,0), area) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |