【发布时间】:2023-02-21 22:59:29
【问题描述】:
我正在尝试制作一个特定的对象,在这种情况下,一个平台在屏幕上显示为浮动矩形,而无需使用 blit。我已经为该平台创建了一个类,但它只是没有出现在屏幕上。我在网上看到其他人的代码是这样工作的,但我不明白我做错了什么。背景无需 blit 即可工作,但平台不支持
我尝试按照教程的确切步骤在线搜索解决方案事件,但它不起作用。有人可以帮忙吗? 主文件
import pygame, sys, time
from settings import *
from sprites import BG
from sprites import Platform
pygame.init()
class Main:
def __init__(self):
# setup
self.display = pygame.display.set_mode((WIDTH, HEIGHT)) # , pygame.FULLSCREEN
pygame.display.set_caption("Jumpy")
self.clock = pygame.time.Clock()
# groups
self.all_sprites = pygame.sprite.Group()
# sprite setup
Platform(self.all_sprites)
BG(self.all_sprites)
def run(self):
while True:
# events
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# game logic
self.all_sprites.update()
self.all_sprites.draw(self.display)
self.clock.tick(FPS)
pygame.display.update()
if __name__ == '__main__':
main = Main()
main.run()
精灵
import pygame
from settings import *
class BG(pygame.sprite.Sprite):
def __init__(self, groups):
super().__init__(groups)
self.image = pygame.image.load("bg_img.png").convert()
self.image = pygame.transform.scale(self.image, (WIDTH, HEIGHT))
self.rect = self.image.get_rect(topleft=(0, 0))
class Platform(pygame.sprite.Sprite):
def __init__(self, groups):
super().__init__(groups)
self.image = pygame.image.load("platform1.png")
self.rect = self.image.get_rect(midleft=(HEIGHT / 2, WIDTH / 2))
【问题讨论】:
标签: python pygame pygame-surface