如何修复 Python 游戏中的图像?
How do I fix the image in my Python game?
我正在使用 pygame 模块制作游戏。每当我让我的猪移动时,它就会像这样出现:见上图。我怎样才能解决这个问题?我在想这与我如何 blit 图像有关,或者与我如何编写主循环有关。此外,当我设置背景图像时,它就开始这样做了。所以我去掉了背景图像以防止猪图像那样出现,但即使背景图像消失了它仍然像那样出现。这是我的代码:
主要游戏脚本:
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)
我是编程新手,这是我创建的第一款游戏,所以如果您也能给我建议并批评我的编码结构,我将不胜感激。
在画猪之前重画背景。这样它将覆盖以前的猪。这就是你应该做的。为了做到这一点,你不会每帧都加载图像,将图像加载并从顶部的函数中转换出来,然后每次都重新绘制背景
#...
screen.blit(sky_img, (0, 0))
pig.blitme()
pig.update()
#...
我正在使用 pygame 模块制作游戏。每当我让我的猪移动时,它就会像这样出现:见上图。我怎样才能解决这个问题?我在想这与我如何 blit 图像有关,或者与我如何编写主循环有关。此外,当我设置背景图像时,它就开始这样做了。所以我去掉了背景图像以防止猪图像那样出现,但即使背景图像消失了它仍然像那样出现。这是我的代码:
主要游戏脚本:
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)
我是编程新手,这是我创建的第一款游戏,所以如果您也能给我建议并批评我的编码结构,我将不胜感激。
在画猪之前重画背景。这样它将覆盖以前的猪。这就是你应该做的。为了做到这一点,你不会每帧都加载图像,将图像加载并从顶部的函数中转换出来,然后每次都重新绘制背景
#...
screen.blit(sky_img, (0, 0))
pig.blitme()
pig.update()
#...