- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在 Python 3.2 中创建了一个游戏,并尝试使用 cx-freeze 将其转换为 .exe,但是当我尝试运行 .exe 时,出现错误。
游戏
import pygame, sys, random
from pygame.locals import *
pygame.init()
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Dodger")
def game():
WIDTH = 800
HEIGHT = 800
PLAYERSIZE = 20
PLAYERSPEED = 4
ENEMYSIZE = 10
ENEMYSPEED = 3
score = 0
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
GREY = (100, 100, 100)
BLUE = (0, 0, 255)
Up = False
Down = False
Left = False
Right = False
titleFont = pygame.font.SysFont(None, 100)
titleText = titleFont.render('Dodger', True, WHITE)
titleRect = titleText.get_rect()
titleRect.centerx = windowSurface.get_rect().centerx
titleRect.centery = 50
menuFont = pygame.font.SysFont(None, 70)
startText = menuFont.render('Start', True, WHITE)
startRect = startText.get_rect()
startRect.centerx = windowSurface.get_rect().centerx
startRect.centery = 213
difficultyText = menuFont.render('Difficulty', True, WHITE)
difficultyRect = difficultyText.get_rect()
difficultyRect.centerx = windowSurface.get_rect().centerx
difficultyRect.top = 320
quitText = menuFont.render('Quit', True, WHITE)
quitRect = quitText.get_rect()
quitRect.centerx = windowSurface.get_rect().centerx
quitRect.centery = 582
oneText = menuFont.render('1', True, WHITE)
oneRect = oneText.get_rect()
oneRect.centerx = 250
oneRect.centery = 421
twoText = menuFont.render('2', True, WHITE)
twoRect = twoText.get_rect()
twoRect.centerx = 350
twoRect.centery = 421
threeText = menuFont.render('3', True, WHITE)
threeRect = threeText.get_rect()
threeRect.centerx = 450
threeRect.centery = 421
fourText = menuFont.render('4', True, WHITE)
fourRect = fourText.get_rect()
fourRect.centerx = 550
fourRect.centery = 421
difficulty = 1
loop = 1
while loop:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
posX, posY = pos
if posY > 390 and posY < 452:
if posX > 200 and posX < 300:
difficulty = 1
elif posX > 300 and posX < 400:
difficulty = 2
elif posX > 400 and posX < 500:
difficulty = 3
elif posX > 500 and posX < 600:
difficulty = 4
elif posY > 120 and posY < 306 and posX > 120 and posX < WIDTH - 120:
loop = 0
elif posY > 492 and posY < WIDTH - 120 and posX > 120 and posX < WIDTH - 120:
pygame.quit()
sys.exit()
if event.type == KEYUP:
if event.key == ord('1'):
difficulty = 1
elif event.key == ord('2'):
difficulty = 2
elif event.key == ord('3'):
difficulty = 3
elif event.key == ord('4'):
difficulty = 4
elif event.key == K_RETURN:
loop = 0
if difficulty == 1:
choiceRect = pygame.Rect(201, 391, 99, 61)
elif difficulty == 2:
choiceRect = pygame.Rect(301, 391, 99, 61)
elif difficulty == 3:
choiceRect = pygame.Rect(401, 391, 99, 61)
elif difficulty == 4:
choiceRect = pygame.Rect(501, 391, 99, 61)
windowSurface.fill(BLACK)
#draw main menu box
pygame.draw.line(windowSurface, WHITE, (120, 120), (WIDTH - 120, 120))
pygame.draw.line(windowSurface, WHITE, (120, 120), (120, HEIGHT - 120))
pygame.draw.line(windowSurface, WHITE, (WIDTH - 120, HEIGHT - 120), (120, HEIGHT - 120))
pygame.draw.line(windowSurface, WHITE, (WIDTH - 120, HEIGHT - 120), (WIDTH - 120, 120))
#draw lines to divide menu
pygame.draw.line(windowSurface, WHITE, (120, 306), (WIDTH - 120, 306))
pygame.draw.line(windowSurface, WHITE, (120, 492), (WIDTH - 120, 492))
pygame.draw.line(windowSurface, WHITE, (200, 390), (WIDTH - 200, 390))
pygame.draw.line(windowSurface, WHITE, (200, 452), (WIDTH - 200, 452))
pygame.draw.line(windowSurface, WHITE, (200, 390), (200, 452))
pygame.draw.line(windowSurface, WHITE, (WIDTH - 200, 390), (WIDTH - 200, 452))
pygame.draw.line(windowSurface, WHITE, (300, 390), (300, 452))
pygame.draw.line(windowSurface, WHITE, (400, 390), (400, 452))
pygame.draw.line(windowSurface, WHITE, (500, 390), (500, 452))
pygame.draw.rect(windowSurface, GREY, choiceRect)
windowSurface.blit(titleText, titleRect)
windowSurface.blit(startText, startRect)
windowSurface.blit(difficultyText, difficultyRect)
windowSurface.blit(quitText, quitRect)
windowSurface.blit(oneText, oneRect)
windowSurface.blit(twoText, twoRect)
windowSurface.blit(threeText, threeRect)
windowSurface.blit(fourText, fourRect)
pygame.display.update()
difficulty += 1
Player = pygame.Rect((WIDTH/2)-(PLAYERSIZE/2),(HEIGHT/2) - (PLAYERSIZE/2), PLAYERSIZE, PLAYERSIZE)
enemies = []
for enemy in range(0, difficulty):
if enemy == 1:
enemies.append(pygame.Rect(1, 1, ENEMYSIZE, ENEMYSIZE))
elif enemy == 2:
enemies.append(pygame.Rect(WIDTH - PLAYERSIZE, 1, ENEMYSIZE, ENEMYSIZE))
elif enemy == 3:
enemies.append(pygame.Rect(1, HEIGHT - PLAYERSIZE, ENEMYSIZE, ENEMYSIZE))
elif enemy == 4:
enemies.append(pygame.Rect(WIDTH - PLAYERSIZE, HEIGHT - PLAYERSIZE, ENEMYSIZE, ENEMYSIZE))
loop = 1
while loop:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_UP or event.key == ord('w'):
Up = True
Down = False
if event.key == K_DOWN or event.key == ord('s'):
Down = True
Up = False
if event.key == K_LEFT or event.key == ord('a'):
Left = True
Right = False
if event.key == K_RIGHT or event.key == ord('d'):
Right = True
Left = False
if event.type == KEYUP:
if event.key == K_UP or event.key == ord('w'):
Up = False
if event.key == K_DOWN or event.key == ord('s'):
Down = False
if event.key == K_LEFT or event.key == ord('a'):
Left = False
if event.key == K_RIGHT or event.key == ord('d'):
Right = False
if event.key == K_ESCAPE:
pygame.quit()
pygame.exit()
if Up and Player.top >= 0:
Player.top -= PLAYERSPEED
if Down and Player.bottom <= HEIGHT:
Player.bottom += PLAYERSPEED
if Left and Player.left >= 0:
Player.left -= PLAYERSPEED
if Right and Player.right <= WIDTH:
Player.right += PLAYERSPEED
i = 0
for enemy in enemies:
if enemies[i].top > Player.top:
enemies[i].top -= ENEMYSPEED
if enemies[i].top < Player.top:
enemies[i].top += ENEMYSPEED
if enemies[i].left > Player.left:
enemies[i].left -= ENEMYSPEED
if enemies[i].left < Player.left:
enemies[i].left += ENEMYSPEED
i += 1
windowSurface.fill(BLACK)
pygame.draw.rect(windowSurface, GREEN, Player)
i = 0
for enemy in enemies:
pygame.draw.rect(windowSurface, RED, enemies[i])
if Player.colliderect(enemies[i]):
loop = 0
i += 1
pygame.display.update()
mainClock.tick(40)
score += 1
score = str(score)
endRect = pygame.Rect(windowSurface.get_rect().centerx - 250, windowSurface.get_rect().centery - 75, 500, 150)
scoreText = menuFont.render('Score: ' + score, True, RED)
scoreRect = scoreText.get_rect()
scoreRect.centerx = windowSurface.get_rect().centerx
scoreRect.centery = windowSurface.get_rect().centery
pygame.draw.rect(windowSurface, BLUE, endRect)
windowSurface.blit(scoreText, scoreRect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYUP:
if event.key == K_RETURN:
game()
game()
我的设置.py
import sys
from cx_Freeze import setup, Executable
setup(
name = "Dodger",
version = "3.2",
description = "My Game",
executables = [Executable("dodger.py", base = "Win32GUI")])
当我尝试运行它时,弹出一个窗口,我得到以下信息 error.
编辑:我在 dodger.py 中添加了“import pygame._view”,现在我得到了这个 error.
最佳答案
我相信你的问题是你没有使用系统字体。 cx_freeze exe 不知道如何读取 None
当我遇到这个问题时,我将字体从 None
更改为系统字体。使用下面的脚本可以找到系统上安装了哪些字体。
import pygame
print(pygame.font.get_fonts())
input()
pygame.font.get_fonts()
将显示所有已安装系统字体的列表。您所要做的就是将 titleFont = pygame.font.SysFont(None, 100)
更改为 titleFont = pygame.font.SysFont('miriam', 100)
或类似的东西('miriam' 是我系统上安装的字体)。
关于python - 使用 cx-freeze 为 pygame 制作 .exe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15735868/
Surface.blit在 1.8 中有一个新参数:混合。定义了以下值: BLEND_ADD BLEND_SUB BLEND_MULT BLEND_MIN BLEND_MAX BLEND_RGBA_A
import sys import pygame import pygame.locals as pgl class Test: def __init__(self): pyg
我对 PyGame 比较陌生。我正在尝试制作一个简单的程序来显示表示鼠标在屏幕上的位置的字符串。 import pygame, sys from pygame.locals import * pyga
我有总是在后台运行的音乐和一些在触发时会播放声音的事件。音乐效果很好。 pygame.mixer.music.load(os.path.join(SOUND_FOLDER, 'WateryGrave.
我有这些代码 FONT = pygame.font.Font("font/calibri.ttf", 50) FONT.size = 25 但是编译器说 AttributeError: 'pygame
有我正在导入的图像: look_1 = pygame.image.load('data\\png\\look1.png').convert_alpha() 我试图减少它的大小是这样的: pygame.
我正在为我的 pygame 制作一个帮助屏幕,每当我运行它时,我都会收到此错误消息: > self.surface.blit(self.helpscreen) TypeError: argument
在 pyGame 中应用程序,我想渲染 SVG 中描述的无分辨率 GUI 小部件。 我怎样才能做到这一点? (我喜欢 OCEMP GUI 工具包,但它的渲染似乎依赖于位图) 最佳答案 这是一个完整的例
有没有办法将多首歌曲加载到 Pygame 中?我不是在谈论这样的音效; crash_sound = pygame.mixer.Sound("crash.ogg") #and pygame.mixer.
我还有一个问题。当我尝试运行我的代码时,pygame 启动然后立即停止。 这是我的代码: import pygame import os import time import random pygam
我正在使用 pymunk 和 pygame 开发一个项目。我正在使用 PivotJoint 约束将我的 body 连接在一起。如果可能的话,我想让关节不可见 - 有什么办法可以做到这一点吗?现在关节在
我使用 fedora 20、Python 2.7 和 virtualenv 1.10.1。我想在 virtualenv 中安装 pygame,我得到了 You are installing a pot
尝试将文本添加到矩形中并使用箭头键在屏幕上移动矩形。我想让文字不会超出边缘。到目前为止,我已经在没有将其放入 Rect 中的情况下工作了,但我想让 Rect 函数工作。现在文本只是反弹回来,我不知道要
我是第一次玩 pygame(总的来说我是 python 的新手),想知道是否有人可以帮助我... 我正在制作一款小型射击游戏,希望能够为坏人创建一个类。我的想法是类应该继承自 pygame.Surfa
我在 Windows 10 机器上运行了 python 3.9.1。我通过 pip 在我的机器上安装了 pygame 2.0.1 (python -m pip install https://gith
错误: File "/home/alien/cncell/core/animator.py", line 413, in create_animation_from_data pygame
这是代码。 5000 个弹跳旋转的红色方块。 (16x16 png) 在 pygame 版本上,我获得 30 fps,但使用 pyglet 获得 10 fps。对于这种事情,OpenGl 不应该更快吗
我以为 pygame.font.Font 是用来加载 .ttf 字体的,如果没有 .ttf 文件在同一个目录下就无法加载字体,但我看过一个视频,有人在没有 .ttf 文件的情况下加载字体。 ttf 字
我正在尝试使用 Travis CI 设置一个项目。项目也使用 pygame。我曾多次尝试设置它 - 但它似乎失败了。 我得到的最接近的是以下内容: .travis.yml : language: py
这个问题已经有答案了: Python error "ImportError: No module named" (38 个回答) 已关闭 3 年前。 我正在使用 Mac 并输入 pip install
我是一名优秀的程序员,十分优秀!