1

我对 Pygame 甚至 Python 还是很陌生,但我知道当其中的某些内容不正确时,它会在 Python Shell 中显示一些文本,告诉您有一些错误。我实际上遇到了很多,这一次,它终于运行并显示了窗口,但它没有响应。我知道我的整个代码中可能存在一些错误,所以请随时纠正我(请解释一下,因为我对这些东西还是新手)。

代码在下面,但如果它可以帮助,如果你要求它,我会看看我是否也可以发布文件。无论如何,这是代码:

#import Modules
import os, sys
import pygame
from pygame.locals import *

background_img="C:/Users/JM/Documents/Python/Pygame_Alpha/background_img.jpg"
cursor_img="C:/Users/JM/Documents/Python/Pygame_Alpha/pygameCursor.png"

def load_image(img_file, colorkey=None):
    file_pathname = os.path.join("\Users\JM\Documents\Python\Pygame_Alpha",img_file)

    try:
        image = pygame.image.load(file_pathname).convert_alpha()

    except pygame.error, message:
        print "Can't load image:", file_pathname
        raise SystemExit, message

    image = image.convert()

    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
    image.set_colorkey(colorkey, RLEACCEL)

    return image, image.get_rect()



#Main character's position and movements
char_x,char_y = 0,0
char_go_x,char_go_y = 0,0

#Main char class
class char(pygame.sprite.Sprite):
 """Main Character"""
     def __init__(self):
        pygame.sprite.Sprite.__init__(self)#call Sprite initializer
        self.image, self.rect = load_image("char_img.png", -1)
        self.jumping = 0

    def update(self):
        self.rect.midtop = char_x,char_y
        if self.jumping == 1:
        self.rect.move_ip(-35,-3)


    def char_no_jump(self):
        self.jumping = 0

    pygame.init()
    pygame.display.set_caption("pygame_Alpha")
    screen = pygame.display.set_mode((800,480),0,32)
    background = pygame.image.load(background_img).convert()
    cursor = pygame.image.load(cursor_img).convert_alpha()

    char = char()

    clock = pygame.time.Clock()
    millisec = clock.tick()
    sec = millisec/1000.0

    char_fall = sec*25
    jump = sec*50

    #blit the background
    screen.blit(background,(0,0))

    #Main Loop
    while 1:

       #Tell pygame not to exceed 60 FPS
       clock.tick(60)


       #Events
       for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            #Events triggered when a key/s is/are pressed
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                    sys.exit()
                elif event.key == K_UP or event.key == K_w:
                    char.jumping = 1

                elif event.key == K_DOWN or event.key == K_s:
                    char_go_y += 1
                elif event.key == K_LEFT or event.key == K_a:
                    char_go_x -= 0.5                
                elif event.key == K_RIGHT or event.key == K_d:
                    char_go_x += 0.75
                    if char_x > 800:
                        char_x = 0

            #Events triggered when a key/s is/are released
            if event.type == KEYUP:
                if event.key == K_UP or event.key == K_w:
                    char_go_y += 1
                elif event.key == K_DOWN or event.key == K_s:
                    char_go_y = 0
                elif event.key == K_LEFT or event.key == K_a:
                    char_go_x = 0
                    if char_x < 0:
                    char_x = 0
                elif event.key == K_RIGHT or event.key == K_d:
                    char_go_x = 0
                    if char_x > 700:
                    char_x = 0

        char.update()
        while char_y < 200:
        char_go_y += char_fall

        if char_y > 200:
        char_y = 200

        #Update values of position of Main Char
        char_x += char_go_x
        char_y += char_go_y

        #Position Variables of Cursor Image, setting its values equal to cursor pos, and blit it to screen
        cursor_x,cursor_y = pygame.mouse.get_pos()
        cursor_x -= cursor.get_width()/2
        cursor_y -= cursor.get_height()/2
        screen.blit(cursor,(cursor_x,cursor_y))

        pygame.display.update()
4

3 回答 3

2

唔...

while char_y < 200:
           char_go_y += char_fall

除非你有一些我没有看到的有趣的别名,如果 char_y < 200(它应该在开始时,它总是因为你正在更新 char_go_y。

如果这不是问题,仍然建议添加一些打印来确定它是否通过循环。

于 2012-05-15T14:57:28.817 回答
0

运行时空闲时是否有任何错误消息?每当屏幕冻结时,您就会出现问题,但是在不知道错误消息的情况下很难确定是什么。也许打开图片有问题,您应该尝试将 .convert() 放在图片文件名的末尾,但这只是猜测。

于 2012-05-15T14:05:01.600 回答
0

调用 pygame.quit 和 sys.exit 可能会导致问题。通常你在 pygame 中永远不需要它们。

代替:

#Main Loop
while 1:

   #Tell pygame not to exceed 60 FPS
   clock.tick(60)


   #Events
   for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

做这个

#Main Loop
done = False
while not done:
   clock.tick(60)

   for event in pygame.event.get():
        if event.type == QUIT:
            done = True
        if event.type == KEYDOWN:
            if event.key == K_ESC:
                done = True
于 2013-02-07T20:08:22.777 回答