为什么玩家精灵和相机无法移动?

时间:2018-01-08 14:04:32

标签: python python-3.x pygame

您必须单击退出按钮才能看到正在变暗的图形,即使您命令它移动,播放器也不会移动。我不明白这里可能出现什么问题。它是功能还是我错过了更新?

以下是代码:

import pygame
import random
import os
from pygame import *

WIDTH = 1024
HEIGHT = 700
HALF_WIDTH = (WIDTH / 2)
HALF_HEIGHT = (HEIGHT / 2)

FPS = 30
game_folder = os.path.dirname(__file__)
car = os.path.join(game_folder,"img")

DEPTH = 32
DISPLAY = (WIDTH, HEIGHT)
FLAGS = 0
CAMERA_SLACK = 30


#Colors
RED =(255,0,0)
BLUE =(0,255,0)
GREEN =(0,0,255)
WHITE =(255,255,255)
BLACK= (0,0,0)

def main():
    global cameraX, cameraY
    #INITIALIZE
    pygame.init()
    screen = pygame.display.set_mode(DISPLAY,FLAGS,DEPTH)
    pygame.display.set_caption("Plato's Life")
    clock = pygame.time.Clock()

    up = down = left = right = False
    bg = Surface((32,32))
    bg.convert()
    bg.fill(WHITE)
    group= pygame.sprite.Group()
    player = Player(32, 32)    
    platforms = []
    #making Level
    x = y = 0
Level= ["TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T                                                                                                      T",
    "T  TT        TT  TTTTTTT TTTTTTT    TTTTTTT      TTTTTTT    TT  TTTTTT TT    TTTTT TTTTTT TT  TT TTTTT T",
    "T  TT        TT  TT      TT         TT   TT      TT    TT   TT  TT  TT TT    TT TT TT     TT  TT TT    T",
    "T  TT        TT  TTTTTTT TTTTT      TT   TT      TT     TT  TT  TT  TT TT    TT TT TT     TT  TT TTTT  T",
    "T  TT        TT  TT      TT         TTTTTTTTT    TT     TT  TT  TTTTTT TT    TT TT TT TTT TT  TT TT    T",
    "T  TT        TT  TT      TT         TT   TT TT   TT    TT   TT  TT  TT TT    TT TT TT   T TT  TT TT    T",
    "T  TTTTTTTTT TT  TT      TTTTTTT    TTTTTTT  TT  TTTTTTT    TT  TT  TT TTTTT TTTTT TTTTTT TTTTTT TTTTT T",
    "T      TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT",
    "T          TT  TT          TT  TT           TT  TT  TTT       TTT       TTT       TTT        TT      TTT",
    "T  TTTTTT  TTTTTT  TTTTTT  TTTTTT  TTTTTT   TTTTTT  TTT  TTT  TTT  TTT  TTT  TTT  TTT  TTT   TT     TTTT",
    "T  TT  TT          TT  TT          TT  TT                TTT       TTT       TTT       TTT         TTTTT",
    "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT    TTTTTT",
    "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT   TTTTTT",
    "T                                                                                              T       T",
    "T  TTTTTTTTTT    TTTTTTTTTT   TTT         TTTTTTTTT  TTTTTTTTT  TTTTTTTTT    TTTTTTTTTT       TTT      T",
    "T  TTTTTTTTTT    TTT     TT   TTT         TTT   TTT     TTT     TTT   TTT    TTTTTTTTTT      TTTTT     T",
    "T  TTTTTTTTTT    TTT     TT   TTT         TTT   TTT     TTT     TTT   TTT    TTTTTTTTTT     TTTTTTT    T",
    "T  TTTTTTTTTT    TTTTTTTTTT   TTT         TTTTTTTTT     TTT     TTT   TTT    TTTTTTTTTT       TTT      T",
    "T  TTTTTTTTTT    TTT          TTT         TTT   TTT     TTT     TTT   TTT    TTTTTTTTTT       TTT      T",
    "T  TTTTTTTTTT    TTT          TTTTTTTTTT  TTT   TTT     TTT     TTTTTTTTT    TTTTTTTTTT       TTT      T",
    "T                                                                                                      T",
    "T  TTTTT  T  TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT",
    "T  TTTTT  T  TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT",
    "T  TTTTT  T  TTTTT                                                                    TTTT             T",
    "T  TTTTT  T  TTTTT      TT               TTTT                   TTTT                  TTTT   TTTT      T",
    "T  TTTTT  T  TTTTT     TTTTTTT  TTT              TTT     T              TTT    TTTTTTTTT   T",
    "T  TTTTT  T  TTTTT    TTTTTT         TTT      TTT          TTT       TTT          TTT    TTT  TT  TTT  T",
    "T  TTTTT    TTTTTT   TTTTTTTT       TTT         TTT      TTT           TTT      TTT    TTT    TT   TTT T",
    "T  TTTTT    TTTTTT  TTTTTTTTTT    TTT    TTT      TTT  TTT              TTT   TTT    TTT      TT    TTTT",
    "T  TTTTT  T        TTTTTTTTTTTTTTTTTT    TTT        TTTT                   TTTT    TTTT       TT     TTT",
    "T  TTTTT  T TTTTT             TT  TTT    TTT                                                  TT       T",
    "T  TTTTT  T                   T          TTT      TTT  TTT   TTT   TTT               TTTTT    TT  TTT  T",
    "T  TTTTT  TT          TTTTTT  T  TTTTTTTTTTT      TTT  TTT   TTT   TTT              TT  TT    TT  TTT  T",
    "T  TTTTT  TTT         TTTTTT     TTT              TTT  TTT   TTT   TTT             TTT  TTT   TT  TTT  T",
    "T  TTTTTTTTTTT        TTTTTT     TTT       TTTTTTTTTTTTTTTTTTTTTTTTTTT     TTTTTTTTTTT  TTTTTTTTTTTTT  T",
    "T  TTTTTTTTTTTT       TTTTTT     TTT           T                                                       T",
    "T  TTTTTTTTTTTTTTTTTTTTTTTTT   TTTTTTT   TTTTTTT       TTT   TTT     TTTTTTT   TTTTTTT   TTTTTTT       T",
    "T  TT  TTT           TT   TTTTTTT   TTTTTTT   TTTTTTT  TTT   TTT     TT   TTTTTTT   TTTTTTT   TTTTTTT  T",
    "TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT",
        ]

    #build level
    for rows in Level:
        for col in rows:
            if col == "T":
                p = Platform(x,y)
                platforms.append(p)
                group.add(p)
            if col == "E":
                e= Exitblock(x,y)
                platforms.append(e)
                group.add(e)
            x += 32
       y += 32
       x = 0

    #camera
    total_level_width = len(Level[0])* 32
    total_level_height = len(Level) * 32
    camera = Camera(complex_camera,total_level_width, total_level_height)


    #events
    running= True
    while running:
        clock.tick(FPS)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running= False
            if event.type == KEYDOWN and event.key == K_ESCAPE:
                raise SystemExit
            if event.type == KEYDOWN and event.type == K_LEFT:
                left = True
            if event.type == KEYDOWN and event.type == K_RIGHT:
                right = True
            if event.type == KEYDOWN and event.type == K_UP:
                up = True
            if event.type == KEYDOWN and event.type == K_DOWN:
                down = True

            if event.type == KEYUP and event.type == K_UP:
                up = False
            if event.type == KEYUP and event.type == K_DOWN:
                down = False
            if event.type == KEYUP and event.type == K_RIGHT:
                right = False
            if event.type == KEYUP and event.type == K_LEFT:
                up = False



    #Background
    for y in range(32):
       for x in range(32):
           screen.blit(bg,(x * 32, y * 32))

    camera.update(player)

    player.update(up, down, left, right, platforms)
    for e in group:
        screen.blit(e.image, camera.apply(e))
        group.add(player)

    #screen fill
    group.draw(screen)



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

class Player (pygame.sprite.Sprite):
   def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image= pygame.image.load(os.path.join(car, "plato.png")).convert()
        self.image.set_colorkey(WHITE)
        self.rect= Rect(x, y, 32, 32)
        self.speedx= 0
        self.speedy= 0

   def update(self, up, down, left, right, platforms):
        self.speedx,self.speedy = 0,0
        if left:
            self.speedx = -10
        elif right:
            self.speedx = 10
        elif up:
            self.speedy -= 10
        elif down:
            self.speedy += 10
        self.rect.x += self.speedx
        self.collide(self.speedx, 0, platforms)
        self.rect.y += self.speedy
        self.collide(0,self.speedy ,platforms)

    def collide(self, speedx, speedy, platforms):
        for t in platforms:
            if pygame.sprite.collide_rect(self, t):
                if isinstance (p, Exitblock):
                    pygame.event.post(pygame.event.Event(QUIT))
                if speedx > 0:
                    self.rect.right = t.rect.left
                if speedx < 0:
                    self.rect.left = t.rect.right
                if speedy > 0:
                    self.rect.bottom = t.rect.top
                if speedy < 0:
                    self.rect.top = t.rect.bottom
#Camera
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):
        return target.rect.move(self.state.topleft)

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

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

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 at the left edge
    l = max(-(camera.width- WIDTH), l)   # stop scrolling at the right edge
    t = max(-(camera.height- HEIGHT), t) # stop scrolling at the bottom
    t = min(0, t)                           # stop scrolling at the top
    return Rect(l, t, w, h)

#Platform
class Platform(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = Surface((32, 32))
        self.image.convert()
        self.image.fill(Color("#DDDDDD"))
        self.rect = Rect(x, y, 32, 32)

   def update(self):
       pass

class ExitBlock(Platform):
    def __init__(self, x, y):
        Platform.__init__(self, x, y)
        self.image.fill(Color("#0033FF"))

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:1)

问题在于事件循环。您正在检查是否event.type == K_LEFT等,而您应该检查是否event.key == K_LEFT。这是固定循环:

for event in pygame.event.get():
    if event.type == pygame.QUIT:
        running = False
    elif event.type == KEYDOWN:
        if event.key == K_ESCAPE:
            running = False
        elif event.key == K_LEFT:
            left = True
        elif event.key == K_RIGHT:
            right = True
        elif event.key == K_UP:
            up = True
        elif event.key == K_DOWN:
            down = True
    elif event.type == KEYUP:
        if event.key == K_UP:
            up = False
        elif event.key == K_DOWN:
            down = False
        elif event.key == K_RIGHT:
            right = False
        elif event.key == K_LEFT:
            left = False

相机没有用,因为绘图代码有点复杂。以下是事件循环下方的代码(将player添加到while循环上方的group):

# Update the camera and the player.
player.update(up, down, left, right, platforms)
camera.update(player)

# Draw everything.
screen.fill((30, 30, 30))
for e in group:
    screen.blit(e.image, camera.apply(e))
pygame.display.flip()

此外,在collide方法中,您必须更改isinstance行:

def collide(self, speedx, speedy, platforms):
    for t in platforms:
        if pygame.sprite.collide_rect(self, t):
            if isinstance(t, ExitBlock):