我在pygame中为android制作游戏,这是我的代码:
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()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
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
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
x+=movex
y+=movey
screen.fill((r,0,0))
screen.blit(bif,(0,0))
screen.blit(player,(x,y))
pygame.display.update()
任何人都可以给我一个资源链接,我可以学习pygame。
当我按下“q”时,在我的游戏中说它然后暂停游戏,当我再次按下它时,解冻可以让任何人给我一个如何做到这一点的建议。我现在这有点模糊,但是什么意思是每当按下“q”键时它就会阻止角色移动而一切都停止。
答案 0 :(得分:1)
免费电子书(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()
答案 1 :(得分:0)
http://www.pygame.org/wiki/tutorials 看起来像是一些教程的好地方。我不确定你的寻找有多先进,所以它们可能有点基础。乍一看,这看起来很不错:http://programarcadegames.com/index.php?chapter=bitmapped_graphics_and_sound&lang=en#section_12
这可能是一种非常笨重的方式,但你可以创建一个重复的无限循环,直到你再次按q键:
if <q key pressed>:
while 1:
sleep(1)
if <q key pressed>:
break
是一个相当简化的版本。
答案 2 :(得分:0)
默认Punch the chimp tutorial是一个非常好的起点。具体来说,你应该看看他们使用pygame.time.Clock.tick来限制帧速率。
一旦你有游戏循环,最简单的暂停方法是使用“event.wait()”,它会停止并等待直到事件发生。您只需使用它来等待按下取消暂停,然后继续事件循环。