免费电子书 (PDF)
暂停使或多或少像这样:
self.PAUSED = False
while True:
for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key==K_q:
self.PAUSED = not self.PAUSED
if not self.PAUSED:
x+=movex
y+=movey
screen.fill((r,0,0))
screen.blit(bif,(0,0))
screen.blit(player,(x,y))
pygame.display.update()
编辑:
您在类中没有代码,因此您必须在没有代码的PAUSED
情况下使用self.
具有暂停屏幕和键盘以及文本“PAUSED”的工作代码
import pygame, sys
from pygame.locals import *
try:
import Android
except ImportError:
Android = None
pygame.init()
if Android:
android.init()
android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)
size = width, height = 1276,650
screen = pygame.display.set_mode(size)
r = 0
bif = pygame.image.load("map.png")
pygame.display.set_caption("Pygame 2D RPG !")
x,y=0,0
movex, movey=0,0
character="boy.png"
player=pygame.image.load(character).convert_alpha()
PAUSED = False
font = pygame.font.SysFont("serif", 72)
text_paused = font.render("PAUSED", True, (255, 0, 0))
while True:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
elif event.key==K_q:
PAUSED = not PAUSED
if not PAUSED:
if event.type==KEYDOWN:
if event.key==K_a:
movex=-1
elif event.key==K_d:
movex=+1
elif event.key==K_w:
movey=-1
elif event.key==K_s:
movey=+1
if event.type==KEYUP:
if event.key==K_a:
movex=0
elif event.key==K_d:
movex=0
elif event.key==K_w:
movey=0
elif event.key==K_s:
movey=0
# --- changing values ---
if not PAUSED:
x+=movex
y+=movey
# --- drawing ---
screen.fill((r,0,0))
screen.blit(bif,(0,0))
screen.blit(player,(x,y))
if PAUSED:
screen.blit(text_paused, (100, 100))
pygame.display.update()
编辑:
带有类的版本(或多或少)
import pygame, sys
from pygame.locals import *
try:
import Android
except ImportError:
Android = None
#----------------------------------------------------------------------
class Background():
def __init__(self, screen):
self.screen = screen
self.x = 0
self.y = 0
self.r = 0
filename = "maps.png" # "start_screen.jpg" #
self.image = pygame.image.load(filename).convert_alpha()
def draw(self):
self.screen.fill((self.r,0,0))
self.screen.blit(self.image, (self.x, self.y))
#----------------------------------------------------------------------
class Player():
def __init__(self, screen):
self.screen = screen
self.x = 0
self.y = 0
self.movex = 0
self.movey = 0
filename = "boy.png" # "jar.png" #
self.image = pygame.image.load(filename).convert_alpha()
def move(self):
self.x += self.movex
self.y += self.movey
def draw(self):
self.screen.blit(self.image, (self.x, self.y))
def events(self, event):
if event.type == KEYDOWN:
if event.key == K_a:
self.movex = -1
elif event.key == K_d:
self.movex = +1
elif event.key == K_w:
self.movey = -1
elif event.key == K_s:
self.movey = +1
if event.type==KEYUP:
if event.key == K_a:
self.movex = 0
elif event.key == K_d:
self.movex = 0
elif event.key == K_w:
self.movey = 0
elif event.key == K_s:
self.movey = 0
#----------------------------------------------------------------------
class Game():
def __init__(self):
pygame.init()
if Android:
android.init()
android.map_key(android.KEYCODE_BACK, pygame.K_ESCAPE)
self.size = self.width, self.height = 1276, 650
self.screen = pygame.display.set_mode(self.size)
pygame.display.set_caption("Pygame 2D RPG !")
self.background = Background(self.screen)
self.player = Player(self.screen)
font = pygame.font.SysFont("serif", 72)
self.text_paused = font.render("PAUSED", True, (255, 0, 0))
def run(self):
PAUSED = False
RUNNING = True
while RUNNING:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUNNING = False
if event.type==KEYDOWN:
if event.key == K_ESCAPE:
RUNNING = False
elif event.key==K_q:
PAUSED = not PAUSED
if not PAUSED:
self.player.events(event)
# --- changing values ---
if not PAUSED:
self.player.move()
# --- drawing ---
self.background.draw()
self.player.draw()
if PAUSED:
self.screen.blit(self.text_paused, (100, 100))
pygame.display.update()
pygame.quit()
sys.exit()
#----------------------------------------------------------------------
if __name__ == '__main__':
Game().run()