24-10-2010, 09:16 PM
si grazie questo è per far scorrere la telecamera (ma come la collego allo sprite del personaggio eh <.<) e il tasto destro premuto sarà if evt.key == pygame.K_LEFT:
comunque ditemi se ci sono errori su questo codice:
#! usr/bin/env python
import pygame
screen = pygame.display.set_mode((800, 600))
background = pygame.image.load('Scrolling_BasicBackground.jpg').convert()
# we render the viewport in a smaller rectangle centered within the screen
# to give us room to print statistics.
#
vpRenderOffset = (80,60)
vpStatsOffset = (80,540)
vpCoordinate = 0
minHorzScrollBounds = 0
maxHorzScrollBounds = 1996 # 2636 - 640
advanceVelocity = 0
scrollVelocity = 10
# generate an event 30 times a second, and perform simulation update. this
# keeps the game running at the same speed in framerate-independent fashion.
#
UPDATE = pygame.USEREVENT
pygame.time.set_timer(UPDATE, int(1000.0/30))
if not pygame.font.get_init():
pygame.font.init()
arialFnt = pygame.font.SysFont('Arial', 16)
running = True
while running:
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
running = False
elif evt.type == pygame.KEYDOWN:
if evt.key == pygame.K_LEFT:
advanceVelocity += -scrollVelocity
elif evt.key == pygame.K_RIGHT:
advanceVelocity += scrollVelocity
elif evt.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT, {}))
elif evt.type == pygame.KEYUP:
if evt.key == pygame.K_LEFT:
advanceVelocity += scrollVelocity
elif evt.key == pygame.K_RIGHT:
advanceVelocity += -scrollVelocity
elif evt.type == UPDATE:
vpCoordinate += advanceVelocity
if vpCoordinate < minHorzScrollBounds:
vpCoordinate = minHorzScrollBounds
if vpCoordinate > maxHorzScrollBounds:
vpCoordinate = maxHorzScrollBounds
# render
screen.fill((0,0,0))
viewport = background.subsurface((vpCoordinate, 0) + (640, 480))
screen.blit(viewport, vpRenderOffset)
screen.blit(arialFnt.render('coordinate: %4d' % vpCoordinate, True, (255,255,255)), vpStatsOffset)
pygame.display.flip()
comunque ditemi se ci sono errori su questo codice:
#! usr/bin/env python
import pygame
screen = pygame.display.set_mode((800, 600))
background = pygame.image.load('Scrolling_BasicBackground.jpg').convert()
# we render the viewport in a smaller rectangle centered within the screen
# to give us room to print statistics.
#
vpRenderOffset = (80,60)
vpStatsOffset = (80,540)
vpCoordinate = 0
minHorzScrollBounds = 0
maxHorzScrollBounds = 1996 # 2636 - 640
advanceVelocity = 0
scrollVelocity = 10
# generate an event 30 times a second, and perform simulation update. this
# keeps the game running at the same speed in framerate-independent fashion.
#
UPDATE = pygame.USEREVENT
pygame.time.set_timer(UPDATE, int(1000.0/30))
if not pygame.font.get_init():
pygame.font.init()
arialFnt = pygame.font.SysFont('Arial', 16)
running = True
while running:
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
running = False
elif evt.type == pygame.KEYDOWN:
if evt.key == pygame.K_LEFT:
advanceVelocity += -scrollVelocity
elif evt.key == pygame.K_RIGHT:
advanceVelocity += scrollVelocity
elif evt.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT, {}))
elif evt.type == pygame.KEYUP:
if evt.key == pygame.K_LEFT:
advanceVelocity += scrollVelocity
elif evt.key == pygame.K_RIGHT:
advanceVelocity += -scrollVelocity
elif evt.type == UPDATE:
vpCoordinate += advanceVelocity
if vpCoordinate < minHorzScrollBounds:
vpCoordinate = minHorzScrollBounds
if vpCoordinate > maxHorzScrollBounds:
vpCoordinate = maxHorzScrollBounds
# render
screen.fill((0,0,0))
viewport = background.subsurface((vpCoordinate, 0) + (640, 480))
screen.blit(viewport, vpRenderOffset)
screen.blit(arialFnt.render('coordinate: %4d' % vpCoordinate, True, (255,255,255)), vpStatsOffset)
pygame.display.flip()