2013-03-05 54 views
0

我有一个问题,我希望你们能帮助我。 在这场足球比赛中,当我在球场周围移动球员时,球门不会停留在那里,他们随着球移动。PyGame独立的背景图像

例子: http://i45.tinypic.com/2vif4t2.gif

import pygame 
import random 
import sys, glob 
from pygame.locals import * 



class Ball(pygame.sprite.Sprite): 

    def __init__(self,x,y,image_x): 
     self.bottom = 20 
     self.image = pygame.image.load("res/images/ball1.png") 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 
     screen.blit(self.image,(self.rect.x,self.rect.y)) 
     self.change_x=x 
     self.change_y=y 





class Field(pygame.sprite.Sprite): 

    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     #self.field = pygame.image.load('res/images/field.png').convert() 
     #self.field_rect = self.field.get_rect() 
     #self.field_rect.x = 0 
     #screen.blit(self.field,(self.field_rect.x,self.field_rect.y)) 


    def goal_top(self):  
     self.goal_top = pygame.image.load("res/images/goal-top.png") 
     screen.blit(self.goal_top,(425,0)) 

    def goal_bottom(self): 

     self.goal_bottom = pygame.image.load("res/images/goal-btm.png") 
     self.goal_bottom_rect = self.goal_bottom.get_rect() 
     self.goal_bottom_rect.y = 720 

     screen.blit(self.goal_bottom,(425,self.goal_bottom_rect.y))   




class Player(pygame.sprite.Sprite): 

    def __init__(self,x,y,image_x,image_y): 
     self.bottom = 20 
     self.image = pygame.image.load("res/images/player/boy.png") 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 
     screen.blit(self.image,(self.rect.x,self.rect.y)) 
     self.change_x=x 
     self.change_y=y 



    def changespeed_x(self,x): 
     self.change_x = x 

    def changespeed_y(self,y): 
     self.change_y = y 




class Camera(object): 
    def __init__(self, camera_func, width, height): 
     self.camera_func = camera_func 

     self.state = Rect(0, 0, width, height) 


    def apply(self, target): 

     try: 
      return target.rect.move(self.state.topleft) 
     except AttributeError: 
      return map(sum, zip(target, self.state.topleft)) 


    def update(self, target): 
     self.state = self.camera_func(self.state, target.rect) 


def complex_camera(camera, target_rect): 
    l, t, _, _ = target_rect 
    _, _, w, h = camera 
    l, t, _, _ = -l + HALF_WIDTH, -t+HALF_HEIGHT, w, h 

    l = min(0, l)       # stop scrolling left 
    l = max(-(camera.width-WIN_WIDTH), l) # stop scrolling right 
    t = max(-(camera.height-WIN_HEIGHT), t) # stop scrolling bottom 
    t = min(0, t)       # stop scrolling at the top 
    return Rect(l, t, w, h) 




pygame.init() 

# Set the height and width of the screen 
WIN_WIDTH = 963 
WIN_HEIGHT = 760 
HALF_WIDTH = int(WIN_WIDTH/2) 
HALF_HEIGHT = int(WIN_HEIGHT/2) 

size=[WIN_WIDTH,WIN_HEIGHT] 
screen=pygame.display.set_mode(size) 
#screen=pygame.display.set_mode(size,FULLSCREEN) 

pygame.display.set_caption("Soccer") 
clock=pygame.time.Clock() 


x=500 
y=280 


set_ball_x = 500 
set_ball_y = 300 
shot = False 
type='' 
dir_y=0 



##INITIAL POSITION OF IMAGES 
image_x=0 
image_y=0 
ball_image_x=0 


dir_y=0 

    ##CAMERA TRACK 
total_level_width = 963 ##BACKGROUND (Field) width 
total_level_height = 1200 ##BACKGROUND (Field) height 
camera = Camera(complex_camera, total_level_width, total_level_height) 
background = pygame.image.load('res/images/field.png').convert() 
background_rect = background.get_rect() 




while 1: 


    clock.tick(20) 

    screen.blit(background, camera.apply((0,0))) 

    ball = Ball(set_ball_x,set_ball_y,ball_image_x) 


    camera.update(ball) 

    player = Player(x,y,image_x,image_y) 


    field = Field() 
    field.goal_top() 
    field.goal_bottom() 



    if pygame.key.get_pressed()[pygame.K_RIGHT]: 
     x=x+6 
     pos =+1 
     image_x = 0 

     if image_y < 90: 
      image_y +=30 
     else: 
      image_y = 0 



    if pygame.key.get_pressed()[pygame.K_LEFT]: 
     x=x-6 
     image_x = 130 

     if image_y < 90: 
      image_y +=30 
     else: 
      image_y = 0 



    if pygame.key.get_pressed()[pygame.K_UP]: 
     y=y-6 

     image_x = 195 


     if image_y < 90: 
      image_y +=30 
     else: 
      image_y = 0 
    if pygame.key.get_pressed()[pygame.K_DOWN]: 
     y=y+6 


     image_x = 70 


     if image_y < 90: 
      image_y +=30 
     else: 
      image_y = 0  


    if pygame.sprite.collide_rect(player,ball): 
     set_ball_x = player.change_x 
     set_ball_y = player.change_y+20 

    if pygame.key.get_pressed()[pygame.QUIT]: 
      sys.exit() 

    if pygame.key.get_pressed()[pygame.K_q]: ## KEY "Q" to EXIT 
      sys.exit() 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      sys.exit() 

    pygame.display.flip()  
    pygame.display.update() 

任何想法,为什么发生这种情况? 谢谢

回答

1

什么是Camera.apply()在做什么?

此:

l, t, _, _ = target_rect 
_, _, w, h = camera 

应该是:

loc = target_rect.topleft 
size = camera.size 

而且

l, t, _, _ = -l + HALF_WIDTH, -t+HALF_HEIGHT, w, h 

如果你想获得中心坐标?您使用

loc = target_rect.center 
+0

首先感谢您的答复。 Camera.apply()是使“更新”屏幕与坐标一起上下移动背景的原因。我没有与你给我的代码运气...问题是,goal_bottom.y应该是像-1100的东西,但如果我把一个值不是屏幕的大小,它永远不会显示。 – tsa 2013-03-06 16:03:55

+0

要使对象滚动,请使用'世界坐标',将其转换为'屏幕坐标'以blit。这意味着你存储了一个顶点坐标,并修改它来滚动。这是一个使用tilesets的例子。但它比听起来简单。 http://code.google.com/p/ninmonkey/source/browse/examples/maptiles_numpy/map.py – ninMonkey 2013-03-06 16:13:18

+0

谢谢猴子,但我认为问题是在班级相机我会尝试一种不同的方式场移动,也许与rect.y一起移动场中的物体。 – tsa 2013-03-06 22:41:49