【问题标题】:How to initialize in pygame?如何在pygame中初始化?
【发布时间】:2022-01-07 15:28:55
【问题描述】:

我尝试使用下面的代码使用 pygame 进行初始化。

import pygame
pygame.init()

但我得到 2 个错误。

pygame 2.0.1 (SDL 2.0.14, Python 3.8.10)
Hello from the pygame community. https://www.pygame.org/contribute.html
ftruncate() failed: File too large
Failed to create secure directory (//.config/pulse): No such file or directory
ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_card_driver returned             error: No such file or directory
ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_concat returned error:                         No such file or directory
ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
ALSA lib conf.c:4732:(_snd_config_evaluate) function snd_func_refer returned error: No         such file or directory
ALSA lib conf.c:5220:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2642:(snd_pcm_open_noupdate) Unknown PCM default
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    pygame.key.get_repeat()
pygame.error: video system not initialized

谁能帮帮我?

我的代码如下。

# importing pygame module
import pygame

# importing sys module
import sys

# initialising pygame
pygame.init()

# creating display
display = pygame.display.set_mode((300, 300))

# creating a running loop
while True:
    
    # creating a loop to check events that
    # are occuring
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        
        # checking if keydown event happened or not
        if event.type == pygame.KEYDOWN:
        
            # if keydown event happened
            # than printing a string to output
            print("A key has been pressed")

【问题讨论】:

  • 请发布为您提供堆栈跟踪的确切代码。它在您的示例代码中没有的行上出错。
  • 您是否尝试在笔记本环境中运行 pygame,内核托管在云中? Pygame 需要访问本地视频系统,在您的计算机上 - 它不能像那样在云上运行,
  • pygame可以在网站上运行吗?我正在使用 onlinegdb.com。

标签: python pygame


【解决方案1】:

你可以在replit.com上运行你的代码:

replit.com/@Rabbid76/PyGame-in-browser

新建repl时,选择Pygame模板:

【讨论】:

  • @VihaanMody 你在哪里得到“无输出”。这对我来说可以。如果您有问题,则与您的系统有关。抱歉,我无法修复您的系统。我已经使用 Repls 实现了大约 100 个 PyGame 示例,它们都运行良好。
  • 对不起,我现在确定问题出在系统上。
猜你喜欢
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 2013-06-06
  • 2016-12-04
相关资源
最近更新 更多