当我移动我的 pygame 角色时,它会留下痕迹[重复]

2024-05-11

我一直在尝试用 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()

我怎样才能解决这个问题? (我知道轨迹几乎看不见,但我希望我的游戏很好)我有一个循环向下移动的背景,为什么轨迹不随之移动?


在翻转显示之前,您需要位图背景。这将“擦除”痕迹。然后绘制当前图像并翻转显示。

    disp.blit(background, (0,0)) # or whatever location you need
    disp.blit(current_image, (x,y))
    pygame.display.flip()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当我移动我的 pygame 角色时,它会留下痕迹[重复] 的相关文章

随机推荐

  • 为单个列表注册事件处理程序

    我有一个共享点事件处理程序 我想为单个列表激活它 而不是站点中的所有列表 我该怎么办 得到答案了 我们需要运行这段代码 也许在控制台应用程序中 不过 我仍然不知道如何在添加事件处理程序后将其删除 string siteUrl Console
  • 如何更改Python使用的SQLite版本?

    我在 Debian 9 12 上安装了 Python 3 8 和 SQLite 3 16 2 并且需要升级到较新版本的 SQLite 我已经下载并编译了 SQLite 网站上提供的合并 并将其放入 usr bin 所以当我这样做时 sqli
  • 通过列计数拆分时重复表头

    我正在 Magento 中输出产品列表 作为包装在表格中的简单列表 由于此列表可能会很长 100 个以上产品 因此我使用了来自这里的想法 https stackoverflow com questions 21001803 how to h
  • Solr PatternReplaceCharFilterFactory 未替换为指定模式

    所以我对 Solr 很陌生 但我尝试使用 PatternReplaceCharFilterFactory 对将存储的电话号码字符串进行一些预处理 这是该字段的配置
  • 在 xcode 中,有没有办法验证所有 NSLocalizedStrings 的密钥?

    除了运行其中包含 NSLocalizedString 的每个代码路径之外 是否有一种方法可以验证所有 NSLocalizedString 是否都具有实际存在于所有捆绑包的所有 Localized strings 文件中的密钥 例如 一个键中
  • FluentAssertions ShouldNotThrow 无法识别异步方法/Func

    我正在尝试检查异步方法是否抛出具体异常 为此 我使用 MSTEST 和 FluentAssertions 2 0 1 我已经检查过这个关于 Codeplex 的讨论 http fluentassertions codeplex com wo
  • 在 try 中使用零合并运算符? for 抛出并返回可选值的函数

    我想在以下两种情况下使用 nil coalescing 运算符设置默认值 函数抛出错误 函数返回 nil 请看一下下面的代码片段 我有以下问题 为什么 item1 为零 item1和item2的初始化有什么区别 enum VendingMa
  • 自定义 php 论坛 - 显示新的/未读的帖子

    我自己使用 php 编写了一个自定义论坛脚本 我决定不使用 phpbb 和其他工具 因为我希望我所做的事情具有 100 的灵活性 不过我遇到了一个问题 如何向用户显示帖子是否是新的 未读的 我想到了两种解决方案 1 饼干 2 数据库 我不想
  • mocha——手表和猫鼬模型

    如果我让 mocha 监视更改 每次保存文件时 mongoose 都会抛出以下错误 OverwriteModelError 无法覆盖Client模型一旦编译 我知道猫鼬不允许两次定义模型 但我不知道如何让它与mocha watch clie
  • 如何调试.NET Windows Service OnStart方法?

    我用 NET 编写的代码仅在作为 Windows 服务安装时才会失败 该故障甚至不允许服务启动 我不知道如何进入 OnStart 方法 如何 调试 Windows 服务应用程序 http msdn microsoft com en us l
  • 如何在查询中生成序列号?

    我们使用 PostgreSQL v8 2 3 如何在查询输出中生成序列号 我想显示查询返回的每一行的序列号 例子 SELECT employeeid name FROM employee 我希望生成并显示从一开始的每一行的序列号 你有两个选
  • 如何使用 javascript 禁用组合键?

    I would like to disable view source shortcut key for IE using JavaScript To disable Ctrl C I am using the following func
  • 超慢的表格布局性能

    我遇到了糟糕的 TableLayout 性能 我在这里读过一些帖子 谈论同样的事情 Android 动态创建表 性能不佳 https stackoverflow com questions 9813427 android dynamical
  • pdflatex: \includegraphics{} -> 找不到文件

    首先 我知道这个问题已经存在了成百上千次 但我在过去四个小时内找到的给出的答案都没有解决我的具体问题 我在这里变得疯狂 我将非常感谢任何帮助和建议 尝试编译一个非常简单的 tex 文件 其中包括 包括图形命令 我最终收到 文件未找到 错误
  • AS3 [Event(name="", type="")],有什么意义?

    我使用 FlashDevelop3 R2 和 Flex 3 3 SDK 进行开发 在很多情况下我必须使用嵌入元数据标签 如下所示 Embed source path to file private var Asset Class 我很好地理
  • Spring Oauth2. DaoAuthenticationProvider 中未设置密码编码器

    我对 Spring Oauth 和 Spring Security 很陌生 我正在尝试在我的项目中使用 client credentials 流程 现在 我设法使用自己的 CustomDetailsS ervice 来从系统中已存在的数据库
  • 通过API更新Twitter背景

    我在通过 Twitter 的 API 更新背景时遇到了一些问题 target url http www google com logos 11th birthday gif ch curl init curl setopt ch CURLO
  • 用于验证 InetSocketAddresses 的正则表达式(ipv4/v6 + 端口地址)

    我在寻找testedipv4 和 ipv6 的正则表达式InetSocket地址 http download oracle com javase 6 docs api java net InetSocketAddress html toSt
  • 在 WebView 中打开 PDF 文件

    大约 2 天 我尝试在我的自定义中打开 PDF 文件WebvView 这是我的WebView code import android app AlertDialog import android app ProgressDialog imp
  • 当我移动我的 pygame 角色时,它会留下痕迹[重复]

    这个问题在这里已经有答案了 我一直在尝试用 Python 制作一个游戏 但是当我移动我的角色时 它会留下痕迹 我知道它并没有显示出那么多 但如果你靠近的话 你可以看到这条踪迹 这真的让我很困扰 这是我的代码 import pygame im