我想知道为什么我的playerIMG无法在pygame中加载

2023-12-25

下面是我的代码,这是我的第一个 pygame 项目,任何帮助将不胜感激! 我认为这与

def 玩家(玩家,玩家):
pygame.display.update()

代码块。尽管当我使用它时,有时它甚至不会显示我的背景对象,而只显示屏幕填充黑色。

import pygame

#initializing the game
pygame.init()

FPS = 60
black = (0,0,0)
white = (255,255,255)

#creating the screen and setting the size
gameDisplay = pygame.display.set_mode((800,600))

#setting the title of the window
pygame.display.set_caption('Space Crashers')

#going into our files and loading the image for the background
background = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\newBackground.jpg')

#player image and model
playerImg = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\blueShip.png')
#these cords are for the player ship to be in the middle of the screen
playerX = 370
playerY = 480

#creating player function
def player(x,y):
    gameDisplay.blit(playerImg, (x,y))




# window creation
clock = pygame.time.Clock()
running = True
while running:
    
     #setting the background to black
    gameDisplay.fill(black)
   
    #then changing the background to the image we loaded
    gameDisplay.blit(background,(0,0))
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             running = False
    pygame.display.update() 
    
        # if the keystroke is pressed, the player will move
    if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX -= .01
            if event.key == pygame.K_RIGHT:
                playerX += .01
            if event.key == pygame.K_UP:
                playerY -= .01
            if event.key == pygame.K_DOWN:
                playerY += .01
    
    
    
               
def player(playerX,playerY):         
        pygame.display.update()             
clock.tick(FPS)   
pygame.quit()
quit()

See 按住键时如何使精灵移动 https://stackoverflow.com/questions/9961563/how-can-i-make-a-sprite-move-when-key-is-held-down。还了解缩进 https://docs.python.org/3/reference/lexical_analysis.html。您的代码必须具有正确的缩进才能正常工作。

典型的 PyGame 应用程序循环必须:

  • 限制每秒帧数以限制 CPU 使用率pygame.time.Clock.tick https://www.pygame.org/docs/ref/time.html#pygame.time.Clock
  • 通过调用以下任一方法处理事件pygame.event.pump() https://www.pygame.org/docs/ref/event.html#pygame.event.pump or pygame.event.get() https://www.pygame.org/docs/ref/event.html#pygame.event.get.
  • 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
  • 清除整个显示或绘制背景
  • 绘制整个场景(blit所有对象)
  • 通过调用更新显示pygame.display.update() https://www.pygame.org/docs/ref/display.html#pygame.display.update or pygame.display.flip() https://www.pygame.org/docs/ref/display.html#pygame.display.flip
import pygame

#initializing the game
pygame.init()

FPS = 60
black = (0,0,0)
white = (255,255,255)

#creating the screen and setting the size
gameDisplay = pygame.display.set_mode((800,600))

#setting the title of the window
pygame.display.set_caption('Space Crashers')

#going into our files and loading the image for the background
background = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\newBackground.jpg')

#player image and model
playerImg = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\blueShip.png')
#these cords are for the player ship to be in the middle of the screen
playerX = 370
playerY = 480

# creating player function
def player(x,y):
    gameDisplay.blit(playerImg, (x,y))

# application loop
clock = pygame.time.Clock()
running = True
while running:
    
    # handle the events 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             running = False

    # update the game states and positions of objects
    keys = pygame.key.get_pressed()
    playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT])  * 3
    playerY += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 3

    # clear the entire display or draw the background
    gameDisplay.fill(black)
    gameDisplay.blit(background,(0,0))

    # draw the entire scene (blit all the objects)
    player(playerX, playerY)
    
    # update the display 
    pygame.display.update()  

    # limit the frames per second to limit CPU usage with
    clock.tick(FPS)   

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

我想知道为什么我的playerIMG无法在pygame中加载 的相关文章

  • php 发送带有图像的电子邮件

    我正在尝试发送一封带有图片的电子邮件 我的电子邮件正文是 当我收到电子邮件时 我看不到图片 相反 我 看 img src http planet earth bogus us icons secret pictures gif 我知道这是因
  • 同情因子简单关系

    我在 sympy 中有一个简单的因式分解问题 无法解决 我在 sympy 处理相当复杂的积分方面取得了巨大成功 但我对一些简单的事情感到困惑 如何得到 phi 2 2 phi phi 0 phi 0 2 8 因式分解 phi phi 0 2
  • 垂直线 axvline 在 matplotlib 的 loglog 图中绘制位于错误位置的线

    我在使用 axvline 在 matplotlib 的 loglog 图中绘制垂直线时遇到问题 第一个问题是垂直线没有出现在正确的位置 第二个问题 可能相关的是 当我放大或平移绘图时 垂直线只是保持在原位 并且没有通过平移 滑动绘图 或放大
  • 如何在D3节点中放置图像?

    到目前为止 我已经创建了这些 D3 节点 用于创建可折叠的层次树 到目前为止 这些节点的颜色为 AA1C1C 深红色 以表明如果您单击它们 它们将扩展到更多节点 我想要做的是在节点中使用图像中的位置 这对于所有用户来说都是一个加号 以知道它
  • Keras,如何获取每一层的输出?

    我已经用 CNN 训练了一个二元分类模型 这是我的代码 model Sequential model add Convolution2D nb filters kernel size 0 kernel size 1 border mode
  • 将 stdout 重定向到 Python 中的文件? [复制]

    这个问题在这里已经有答案了 如何将 stdout 重定向到 Python 中的任意文件 当长时间运行的 Python 脚本 例如 Web 应用程序 从 ssh 会话内启动并处于后台 并且 ssh 会话关闭时 应用程序将引发 IOError
  • 如何通过 Python socket.send() 发送字符串以外的任何内容

    我对 Python 编程非常陌生 但出于必要 我必须快速地将一些东西组合在一起 我正在尝试通过 UDP 发送一些数据 除了当我执行 socket send 时 我必须以字符串形式输入数据之外 一切都正常 这是我的程序 这样你就可以看到我在做
  • 使用 Pandas 查找自滚动高点以来的周期数

    我在 Pandas 中使用rolling max函数 http pandas pydata org pandas docs stable computation html moving rolling statistics moments
  • Python NLP 英式英语与美式英语

    我目前正在用Python 进行NLP 工作 然而 在我的语料库中 既有英式英语也有美式英语 实现 实现 我正在考虑将英式英语转换为美式英语 但是 我没有找到一个好的工具 包来做到这一点 有什么建议么 我也找不到包 但试试这个 请注意 我必须
  • 打印一个 Jupyter 单元中定义的所有变量

    有没有一种更简单的方法来以漂亮的方式显示单个单元格中定义的所有变量的名称和值 我现在做的方式是这样的 但是当有30个或更多变量时我浪费了很多时间 您可以使用whos http ipython readthedocs io en stable
  • python 语言环境奇怪的错误。这究竟是怎么回事?

    所以今天我升级到了 bazaar 2 0 2 我开始收到这条消息 顺便说一句 我在雪豹上 bzr warning unknown locale UTF 8 Could not determine what text encoding to
  • 将 JSON 字符串传递给 Django 模板

    我一直在用头撞墙 试图找出为什么我无法将从 Django 模型生成的 JSON 字符串传递到模板的 javascript 静态文件中 事实证明 问题不在模型级别 使用serializers serialize 在脚本本身中放入相同的字符串将
  • 检查对象数组中的多个属性匹配

    我有一个对象数组 它们都是相同的对象类型 并且它们有多个属性 有没有办法返回一个较小的对象数组 其中所有属性都与测试用例 字符串匹配 无论该属性类型是什么 使用列表理解all http docs python org 3 library f
  • Selenium 网页抓取与动态内容和隐藏数据表上的美丽汤

    真的需要这个社区的帮助 我正在使用 Selenium 和 Beautiful Soup 对 Python 中的动态内容进行网页抓取 问题是定价数据表无法解析为 Python 即使使用以下代码 html browser execute scr
  • 如何在Python中正确声明ctype结构+联合?

    我正在制作一个二进制数据解析器 虽然我可以依靠 C 但我想看看是否可以使用 Python 来完成该任务 我对如何实现这一点有一些了解 我当前的实现如下所示 from ctypes import class sHeader Structure
  • 如何从c++调用python

    我是Python新手 我尝试像这样从 C 调用 python 脚本 在 Raspberry Pi 中 std string pythonCommand python Callee py a b int res system pythonCo
  • tweepy 流到 sqlite 数据库 - 语法错误[重复]

    这个问题在这里已经有答案了 可能的重复 tweepy 流到 sqlite 数据库 语法无效 https stackoverflow com questions 9434205 tweepy stream to sqlite database
  • 在没有numpy的情况下在python中分配变量NaN

    大多数语言都有一个 NaN 常量 您可以使用它为变量分配值 NaN python 可以在不使用 numpy 的情况下做到这一点吗 是的 使用math nan https docs python org 3 library math html
  • 在 pip 中为 Flask 应用程序构建 docker 映像失败

    from alpine latest RUN apk add no cache python3 dev pip3 install upgrade pip WORKDIR backend COPY backend RUN pip no cac
  • 将笔记本生成的 HTML 片段转换为 LaTeX 和 PDF

    在我的笔记本里有时会有 from IPython display import display HTML display HTML h3 The s is important h3 question of the day 但当我后来将笔记本

随机推荐