我的基于pygame的代码不会呈现文本。文本对象为gem_count
。
main.py
import engine
import color
import time
import sys
import random
try:
if sys.argv[1] == "--debug":
debug = True
verbose = False
elif sys.argv[1] == "--verbose":
verbose = True
debug = True
except:
debug = False
verbose = False
def player_step(obj, dx, dy, others=[]):
if (obj.x + dx > 400 or obj.x + dx < 0) or (obj.y + -(dy) > 210 or obj.y + -(dy) < 0):
return
step_num = 0
frame = 1
for i in range(32):
obj.move(dx/32, -(dy)/32)
engine.background_image_fill("grass.png", 64, 64, 480, 288)
for image in others:
image.draw()
obj.draw(frame)
time.sleep(0.0025)
step_num += 1
if step_num == 12:
step_num = 0
if frame == 1:
if verbose:
print("%s frame changed to: 2" % obj)
frame = 2
else:
frame = 1
if verbose:
print("%s frame changed to: 1" % obj)
if debug:
print("%s moved to x: " % (obj) + str(obj.x) + " " + "y: " + str(obj.y))
engine.init()
engine.window("8-Bit Miner", 480, 288)
engine.background(color.GREEN)
abandoned_mine = engine.Image("abandoned_mine.png", 80, 80)
mine = engine.Image("mine.png", 80, 80)
mine_abandoned = True
lantern = engine.Image("lantern.png", 0, 0, size=(480, 288))
#create player
player = engine.Animation(16, 16, size=64, frames=["player_idle.png", "player_walk_01.png", "player_walk_02.png"])
gems = 15
gem_count = engine.Text(str(gems), 80, 40)
print("Press:\nB to rebuild mine\nM to mine for gems\n")
running = True
while running:
for event in engine.event_get():
if event.type == engine.QUIT:
running = False
break
for key in engine.get_pressed():
if key == engine.RIGHT:
player_step(player, 64, 0, [abandoned_mine if mine_abandoned else mine])
elif key == engine.LEFT:
player_step(player, -64, 0, [abandoned_mine if mine_abandoned else mine])
elif key == engine.UP:
player_step(player, 0, 64, [abandoned_mine if mine_abandoned else mine])
elif key == engine.DOWN:
player_step(player, 0, -64, [abandoned_mine if mine_abandoned else mine])
elif key == engine.K_G:
gem_count.draw()
if player.x == 80 and player.y == 80:
if key == engine.K_B:
if gems >= 10:
mine_abandoned = False
print("Fixed abandoned mine!")
gems -= 10
if key == engine.K_M:
if not mine_abandoned:
print("Mining...")
engine.background(color.BLACK)
lantern.draw()
time.sleep(5)
increase = random.choice([0, 0, 0, 0, 1, 2, 3, 4])
if increase > 0:
print("Found %i gems!" % increase)
else:
print("Didn't find any gems.")
gems += increase
else:
pass
if mine_abandoned:
abandoned_mine.draw()
else:
mine.draw()
gem_count.draw()
player.draw(0)
time.sleep(0.075)
engine.background_image_fill("grass.png", 64, 64, 480, 288)
engine.pyquit()
这是我用来简化某些任务的代码:
engine.py
#Game engine
import pygame
QUIT = pygame.QUIT
clock = pygame.time.Clock
#Keys
LEFT = pygame.K_LEFT
RIGHT = pygame.K_RIGHT
UP = pygame.K_UP
DOWN = pygame.K_DOWN
ESC = pygame.K_ESCAPE
K_B = pygame.K_b
K_M = pygame.K_m
K_G = pygame.K_g
class ImageError(Exception):
"""Raised when image loading or saving fails"""
class Error(Exception):
"""Raised for other errors"""
class Text:
def __init__(self, text, x, y):
self.font = pygame.font.Font(None, 30)
self.text = text
self.x = x
self.y = -(y)
def draw(self):
global game_surface
self.textsurf = self.font.render(self.text, True, (255, 255, 255))
game_surface.blit(self.textsurf, (self.x, self.y))
pygame.display.flip()
class Image:
def __init__(self, image, x, y, rotation=0, size=(64, 64)):
try:
self.pygame_image = pygame.image.load(image).convert_alpha()
except:
raise ImageError("Cannot load image: %s" % image)
self.x = x
self.y = y
self.angle = rotation
self.size = size
def draw(self):
global game_surface
if self.angle:
self.rotated_pygame_image = pygame.transform.rotate(self.pygame_image, self.angle)
else:
self.rotated_pygame_image = self.pygame_image
game_surface.blit(pygame.transform.scale(self.rotated_pygame_image, self.size), (self.x, self.y))
pygame.display.flip()
def move(self, dx, dy):
self.x += dx
self.y += -(dy)
class AnimFrame:
def __init__(self, image):
try:
self.pygame_image = pygame.image.load(image).convert_alpha()
except pygame.error:
raise ImageError("Couldn't load the image")
def draw(self, x, y, rotation=0, size=16):
global game_surface
if rotation:
self.rotated_pygame_image = pygame.transform.rotate(self.pygame_image, rot)
else:
self.rotated_pygame_image = self.pygame_image
game_surface.blit(pygame.transform.scale(self.rotated_pygame_image, size), (x, y))
pygame.display.flip()
def image(self):
return self.pygame_image
class Animation:
def __init__(self, x, y, size, frames, rotation=0):
self.x = x
self.y = y
self.rotation = rotation
self.size = (size, size)
self.anim = []
for frame in frames:
self.anim.append(AnimFrame(frame))
def draw(self, frame):
self.anim[frame].draw(self.x, self.y, rotation=self.rotation, size=self.size)
def move(self, dx, dy):
self.x += dx
self.y += dy
def init():
pygame.init()
def window(title, width, height):
global game_surface
game_surface = pygame.display.set_mode((width, height))
pygame.display.set_caption(title)
def event_get():
return pygame.event.get()
def get_pressed():
pressed = pygame.key.get_pressed()
keys = []
if pressed[pygame.K_LEFT]:
keys.append(LEFT)
if pressed[pygame.K_RIGHT]:
keys.append(RIGHT)
if pressed[pygame.K_UP]:
keys.append(UP)
if pressed[pygame.K_DOWN]:
keys.append(DOWN)
if pressed[pygame.K_b]:
keys.append(K_B)
if pressed[pygame.K_m]:
keys.append(K_M)
if pressed[pygame.K_g]:
keys.append(K_G)
return keys
def background(color):
global game_surface
game_surface.fill(color)
pygame.display.flip()
def background_image_fill(image, img_w, img_h, win_w, win_h):
global game_surface
for w in range(0, win_w, img_w):
for h in range(0, win_h, img_h):
game_surface.blit(pygame.transform.scale(pygame.image.load(image), (img_w, img_h)), (w, h))
def mouse_pos():
return pygame.mouse.get_pos()
def pyquit():
pygame.display.quit()
pygame.quit()
我无法在代码中看到任何问题。所以我不知道为什么gem_count
不会渲染。
答案 0 :(得分:0)
在engine.py:29中你有
self.y = -(y)
这似乎会将您的文字从屏幕上移开。删除-
,您可能会看到自己的文字。如果你不能,那么评论下面的内容可能会让你更容易看到。
engine.background_image_fill("grass.png", 64, 64, 480, 288)