我一直在尝试使用Python制作游戏,但是当我移动角色时,它会留下一条痕迹。我知道它并没有显示出那么多,但是如果你靠近,你可以看到路径,这真的困扰我。
这是我的代码:
import pygame
import os
from pygame import mixer
pygame.init()
mixer.init()
width = 800
height = 600
black = (0,0,0)
white = (255,255,255)
ship_width = 56
ship_height = 64
disp = pygame.display.set_mode((width,height))
pygame.display.set_caption("space_game")
clock = pygame.time.Clock()
background = pygame.image.load(os.path.join("Backgrounds", "Space.png"))
Ship00 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship00.png"))
Ship01 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship01.png"))
Ship02 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship02.png"))
Ship03 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship03.png"))
Ship04 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship04.png"))
Ship05 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship05.png"))
Ship06 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship06.png"))
Ship07 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship07.png"))
Ship08 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship08.png"))
Ship09 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship09.png"))
Ship10 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship10.png"))
Ship11 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship11.png"))
Ship12 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship12.png"))
Ship13 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship13.png"))
Ship14 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship14.png"))
Ship15 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship15.png"))
Ship16 = pygame.image.load(os.path.join("Images", "Animations", "Ship
Animation", "Ship16.png"))
images = [Ship00, Ship01, Ship02, Ship03, Ship04, Ship05, Ship06, Ship07,
Ship08, Ship09, Ship10, Ship11, Ship12, Ship13, Ship14, Ship15, Ship16]
mixer.music.load(os.path.join("Music", "space_song.wav"))
mixer.music.play()
def gameLoop():
x = (width * 0.45)
y = (height * 0.8)
x_ch = 0
y_ch = 0
x_bg = 0
index = 0
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == ord("a"):
x_ch = -5
elif event.key == ord("d"):
x_ch = 5
elif event.key == ord("w"):
y_ch = -5
elif event.key == ord("s"):
y_ch = 5
if event.type == pygame.KEYUP:
if event.key == ord("a") or event.key == ord("d"):
x_ch = 0
if event.key == ord("w") or event.key == ord("s"):
y_ch = 0
x += x_ch
y += y_ch
if x > width - ship_width or x < 0:
x_ch = 0
if y > height - ship_height or y < 0:
y_ch = 0
x_loop = x_bg % background.get_rect().height
disp.blit(background, (0, x_loop - background.get_rect().height))
if x_loop < height:
disp.blit(background, (0, x_loop))
x_bg += 5
index += 1
index %= len(images)
current_image = images[index]
disp.blit(current_image, (x,y))
pygame.display.update()
clock.tick(60)
gameLoop()
pygame.quit()
quit()
我该如何解决这个问题? (我知道这条路几乎是看不见的,但我希望我的游戏很好)我有一个背景,在一个循环中向下移动,为什么跟踪不随之移动?
答案 0 :(得分:3)
在翻转显示屏之前,您需要对背景进行blit。这将“擦除”#39;小道。然后绘制当前图像并翻转显示。
disp.blit(background, (0,0)) # or whatever location you need
disp.blit(current_image, (x,y))
pygame.display.flip()
答案 1 :(得分:2)
这是一个带滚动背景的完整解决方案:
import pygame
pygame.init()
width = 800
height = 600
black = (0, 0, 0)
white = (255, 255, 255)
ship_width = 56
ship_height = 64
disp = pygame.display.set_mode((width, height))
pygame.display.set_caption("space_game")
clock = pygame.time.Clock()
background = pygame.image.load('background.png')
background_height = background.get_rect().height
ship = pygame.image.load('ship.png')
def game_loop():
x = width * 0.45
y = height * 0.80
x_ch = 0
y_ch = 0
x_bg = 0
game_exit = False
while not game_exit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_exit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
x_ch = -5
if event.key == pygame.K_d:
x_ch = 5
if event.key == pygame.K_w:
y_ch = -5
if event.key == pygame.K_s:
y_ch = 5
if (event.key == pygame.K_ESCAPE) or (event.key == pygame.K_q):
game_exit = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_d:
x_ch = 0
if event.key == pygame.K_w or event.key == pygame.K_s:
y_ch = 0
if (x + x_ch) > width - ship_width or (x + x_ch) < 0:
x_ch = 0
if (y + y_ch) > height - ship_height or (y + y_ch) < 0:
y_ch = 0
x += x_ch
y += y_ch
x_loop = x_bg % background_height
disp.blit(background, (0, x_loop - background_height))
if x_loop < height:
disp.blit(background, (0, x_loop))
x_bg += 5
disp.blit(ship, (x, y))
pygame.display.update()
clock.tick(60)
game_loop()
pygame.quit()
结果:
背景透明的船舶:
满天星斗的背景:
我发布这个答案,以防有人需要一个工作示例作为起点。
答案 2 :(得分:0)
我认为你错过了pygame.display.flip()
。在pygame.display.update()
行之后添加。