Pygame:添加背景

时间:2016-10-07 17:38:33

标签: python python-2.7

我正在为我的高年级做一个项目。它是一个我可以移动用户的游戏。我想在我所有的图片背后添加一个背景。我到处搜索,但我似乎无法找到解决方案。有人可以帮忙吗?

import pygame
import os

class Player(object):  
    def __init__(self):
        self.image = pygame.image.load("player1.png")
        self.image2 = pygame.transform.flip(self.image, True, False) 
        self.coffee=pygame.image.load("coffee.png")
        self.computer=pygame.image.load("computer.png")        

        self.flipped = False
        self.x = 0
        self.y = 0


    def handle_keys(self):
        """ Movement keys """
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_DOWN]: 
            self.y += dist 
        elif key[pygame.K_UP]: 
            self.y -= dist 
        if key[pygame.K_RIGHT]: 
            self.x += dist
            self.flipped = False
        elif key[pygame.K_LEFT]:
            self.x -= dist
            self.flipped = True

    def draw(self, surface):
        if self.flipped:
            image = self.image2
        else:
            image = self.image
        surface.blit(image, (self.x, self.y))
        surface.blit(self.coffee, (700,500))
        surface.blit(self.computer,(0,500))


pygame.init()

screen = pygame.display.set_mode((810, 610))    #creates the screen

player = Player() 
clock = pygame.time.Clock()

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()      # quit the screen
            running = False

    player.handle_keys()       # movement keys
    screen.fill((255,255,255)) # fill the screen with white
    player.draw(screen)        # draw the player to the screen
    pygame.display.update()    # update the screen

    clock.tick(60)             # Limits Frames Per Second to 60 or less

1 个答案:

答案 0 :(得分:1)

背景图像与任何其他图像没有区别。先吧.blit吧。