如何在我的Python游戏中修复图像?

时间:2017-08-10 21:10:38

标签: python pygame pygame-surface

enter image description here

我正在使用pygame模块制作游戏。每当我让我的猪移动它就会出现这样的情况:见上图。我怎样才能解决这个问题?我认为这与我对图像的模糊方式有关,或者与我对主循环的编码方式有关。此外,它在我设置背景图像时开始这样做。所以我把背景图像关闭以阻止猪图像像那样出来,但即使背景图像消失,它仍然会像那样出现。这是我的代码:

主要游戏脚本:

import sys

import pygame

from pig import Pig

pygame.init()

# Dimensions of screen.
screen_width = 800
screen_height = 600

# Set the screen display and caption.
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pig Heaven")


# Objects
pig = Pig(screen)


def sky_blitme(screen):
    """Set background sky image."""
    sky_img = pygame.image.load('images/sky.png')
    sky_img = pygame.transform.scale(sky_img, (800, 600))
    screen.blit(sky_img, (0, 0))


sky_blitme(screen)

while True:

    # Accept events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        # Keydown events
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                pig.moving_right = True
                pig.orientation = 'right'
            elif event.key == pygame.K_LEFT:
                pig.moving_left = True
                pig.orientation = 'left'
            elif event.key == pygame.K_UP:
                pig.moving_up = True
            elif event.key == pygame.K_DOWN:
                pig.moving_down = True

        # Keyup events
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                pig.moving_right = False
            elif event.key == pygame.K_LEFT:
                pig.moving_left = False
            elif event.key == pygame.K_UP:
                pig.moving_up = False
            elif event.key == pygame.K_DOWN:
                pig.moving_down = False

    pig.blitme()
    pig.update()

    pygame.display.flip()

这是猪模块:

import pygame


class Pig():

    def __init__(self, screen):
        """Initialize the pig and set its starting position."""
        self.screen = screen

        # Load the pig image and set pig and screen to rect.
        self.image = pygame.image.load('images/pig.png')
        self.image2 = pygame.transform.flip(self.image, True, False)
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # Start the pig at the bottom center of the screen.
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # Pig direction
        self.orientation = 'down'

        # Speed of the pig
        self.pig_speed = 1.5
        self.center = float(self.pig_speed)

        # Set a variable for each movement.
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        """Update the position of the pig.
        Set boundaries on the edges of the screen."""

        if self.rect.right <= self.screen_rect.right:
            if self.moving_right:
                self.rect.centerx += self.pig_speed

        if self.rect.left > 0:
            if self.moving_left:
                self.rect.centerx -= self.pig_speed

        if self.rect.top > 0:
            if self.moving_up:
                self.rect.bottom -= self.pig_speed

        if self.rect.bottom <= self.screen_rect.bottom:
            if self.moving_down:
                self.rect.bottom += self.pig_speed

    def blitme(self):
        if self.orientation == 'right':
            self.screen.blit(self.image, self.rect)
        elif self.orientation == 'left':
            self.screen.blit(self.image2, self.rect)
        else:
            self.screen.blit(self.image, self.rect)

我是编程新手,这是我正在创建的第一款游戏,所以如果你能给我建议并批评我的编码结构,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

在画猪之前重绘背景。这样它将覆盖以前的猪。这就是你应该怎么做的。为了使它不会每帧都加载图像,从图像加载并转换出顶部的函数,然后每次只重绘背景

#...
screen.blit(sky_img, (0, 0))
pig.blitme()
pig.update()
#...