您有事件循环,draw_sprite()
因此它运行到游戏结束并且window.set_background
永远不会执行。
您的代码构造不正确。
我尝试更正它并稍后发送代码。
顺便说一句:请参阅:pygame.sprite.Sprite。
编辑:
如何组织代码的简单示例。
现在它在一个文件中。在 Pygame 中始终只有一个窗口,因此无需制作 Panel+Widnow。你有一个事件循环在run()
. 所有代码都在 Window 内(创建 Sprite、更改背景、绘图等)。
我将精灵添加到(python)列表并从列表中绘制所有精灵 - 玩家是例外 - 所以我只能从列表中删除最后一个精灵:/如果你需要更好的东西,请参阅pygame.sprite.Sprite()和pygame.sprite.Group( ) .
用于Arrows
移动红球,Space
暂停游戏,ESC
退出。
最后我附上了我的位图。
import pygame
#from pygame.locals import *
#--------------------------------------------------------------------
# class for single sprite
#--------------------------------------------------------------------
class MySprite():
def __init__(self, image, x, y):
self.image = pygame.image.load(image)
image_rect = self.image.get_rect()
# Rect class to use "Sprite collision detect" - in the future
# In rect you have sprite position and size
# You can use self.rect.x, self.rect.y, self.rect.width, self.rect.height
# and self.rect.center, self.rect.centerx, self.rect.top, self.rect.bottomright etc.
self.rect = pygame.rect.Rect(x, y, image_rect.width, image_rect.height)
def draw(self, screen):
screen.blit(self.image, self.rect)
#--------------------------------------------------------------------
# class for player
#--------------------------------------------------------------------
class MyPlayer(MySprite):
def __init__(self, image, x, y):
# parent constructor always as a first in __init__
MySprite.__init__(self, image, x, y)
self.speed_x = self.speed_y = 0
#-----------------------------
def set_speed(self, x, y):
self.speed_x = x
self.speed_y = y
#-----------------------------
def update(self):
self.rect.x += self.speed_x
self.rect.y += self.speed_y
if self.rect.centerx < 0 :
self.rect.centerx = 800
elif self.rect.centerx > 800 :
self.rect.centerx = 0
if self.rect.centery < 0 :
self.rect.centery = 600
elif self.rect.centery > 600 :
self.rect.centery = 0
#--------------------------------------------------------------------
class Window():
def __init__(self, width, height):
#--------------------
self.rect = pygame.Rect(0, 0, width, height)
# or
self.width, self.height = width, height
#--------------------
pygame.init()
# most users and tutorials call it "screen"
self.screen = pygame.display.set_mode(self.rect.size)
#############################################################
self.foreground = None
self.background = None
self.set_background("background.jpg")
self.set_foreground("ball3.png")
#################################################
self.player = MyPlayer("ball1.png", 100, 200)
self.sprites_list = []
self.add_sprite(MySprite("ball2.png", 100, 400))
self.add_sprite(MySprite("ball2.png", 300, 500))
self.add_sprite(MySprite("ball2.png", 300, 200))
self.remove_last_sprite()
#-----------------------------
# red text "PAUSE"
font = pygame.font.SysFont("", 72)
self.text_pause = font.render("PAUSE", True, (255, 0, 0))
# center text on screen
screen_center = self.screen.get_rect().center
self.text_pause_rect = self.text_pause.get_rect(center=screen_center)
#--------------------------
def add_sprite(self, sprite):
self.sprites_list.append(sprite)
#--------------------------
def remove_last_sprite(self):
if self.sprites_list:
del self.sprites_list[-1]
#--------------------------
def draw_sprites(self, screen):
for sprite in self.sprites_list:
sprite.draw(screen)
#--------------------------
def draw_background(self, screen):
screen.fill((0,64,0)) # clear screen to green
if self.background:
screen.blit(self.background, (0,0))
#--------------------------
def draw_foreground(self, screen):
if self.foreground:
screen.blit(self.foreground, (0,0))
#--------------------------
def draw_world(self, image):
temp = pygame.Surface(self.rect.size, pygame.SRCALPHA, 32).convert_alpha()
image_rect = image.get_rect()
for x in range(0, self.rect.width, 60):
for y in range(0,self.rect.width, 60):
temp.blit(image,(x,y))
return temp
#--------------------------
def set_foreground(self, image=None):
if image:
img = pygame.image.load(image)
self.foreground = self.draw_world(img)
#--------------------------
def set_background(self, image=None):
if image:
self.background = pygame.image.load(image)
#--------------------------
def run(self):
clock = pygame.time.Clock()
RUNNING = True
PAUSED = False
while RUNNING:
#--- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUNNING = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
RUNNING = False
elif event.key == pygame.K_SPACE:
PAUSED = not PAUSED
if event.key == pygame.K_UP:
self.player.set_speed(0,-10)
elif event.key == pygame.K_DOWN:
self.player.set_speed(0,10)
elif event.key == pygame.K_LEFT:
self.player.set_speed(-10,0)
elif event.key == pygame.K_RIGHT:
self.player.set_speed(10,0)
if event.type == pygame.KEYUP:
if event.key in (pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT):
self.player.set_speed(0,0)
#--- changes ----
if not PAUSED:
# change elements position
self.player.update()
#--- draws ---
self.draw_background(self.screen)
self.draw_foreground(self.screen)
self.draw_sprites(self.screen)
self.player.draw(self.screen)
if PAUSED:
# draw pause string
self.screen.blit(self.text_pause, self.text_pause_rect.topleft)
pygame.display.update()
#--- FPS ---
clock.tick(25) # 25 Frames Per Seconds
#--- finish ---
pygame.quit()
#----------------------------------------------------------------------
Window(800,600).run()
球1.png
球
2.png 球3.png
背景.jpg
截屏