0

我正在 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() 
  1. 谁能给我一个可以学习pygame的资源链接。

  2. 在我的游戏中说,当我按下“q”然后它会暂停游戏,当我再次按下它时它会解冻任何人都可以给我建议如何做到这一点。我现在这有点含糊,但意思是每当按下“q”键时,它会阻止角色移动并且一切都停止。

4

3 回答 3

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()
于 2013-11-04T20:26:35.527 回答
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

是一个相当简化的版本。

于 2013-11-04T20:24:23.790 回答
0

默认的 Punch the chimp 教程是一个很好的起点。具体来说,你应该看看他们使用 pygame.time.Clock.tick 来限制帧率。

一旦你有一个游戏循环,最简单的暂停方法是使用“event.wait()”,它会停止并等待事件发生。您只需使用它等待直到按下取消暂停,然后继续事件循环。

于 2013-11-04T20:25:01.327 回答