Pygame第一场比赛问题

时间:2012-10-21 23:06:07

标签: python pygame

我是Python和Pygame的新手,我开始制作一个简单的游戏,在飞越敌人的玩家身上拍摄,以测试我学到的东西。当它运行时,我只是得到错误,说它不想要字符串。它如下:

bgi="bg.png"     #BACKGROUND IMAGE
pi="man.png"     #PLAYER IMAGE
proji="proj.png" #PROJECTILE IMAGE

import pygame, sys            #Import
from pygame.locals import *   #Import

pygame.init() #Initialize

screen=pygame.display.set_mode((1000,600),0,32) #Display

background=pygame.image.load(bgi).convert() #Background
player=pygame.image.load(pi).convert_alpha() #Player

proji2=pygame.transform.rotate(proji, 180) #Rotate

x=0       #variables
y=0
mx=0
mv=0
projiy=0
player.pos=(x,y)

while True:                                   #Loop
    for event in pygame.event.get():
        if event.type==QUIT: #X Clicked
            pygame.quit()
            sys.exit()

        if event.type==KEYDOWN: #Key Press
            if event.key==K_a:
                mx=-1
            elif event.key==K_d:
                mx=+1
            elif event.key==K_s:
                screen.blit(proji2, player.pos)
                my=+1

        if event.type==KEYUP: #Key Release
            if event.key==K_a:
                mx=0
            elif event.key==K_d:
                mx=0
            elif event.key==K_s:
                my=0


    x+=mx
    projiy+=mv

    screen.blit(background, (0,0))  #Display
    screen.blit(player, (x,y))

    pygame.display.update()    #Update

任何人都可以帮我找到错误并向我解释为什么他们错了吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

看起来你忘记将“proj.png”转换为曲面。它仍然是你从未真正打开的文件的字符串名称。

答案 1 :(得分:0)

将前三行代码替换为以下内容:

bgi=pygame.image.load("bg.png").convert_alpha()
pi=pygame.image.load("man.png").convert_alpha()
proji=pygame.image.load("proj.png").convert_alpha()

您无法在屏幕上显示字符串,只能在曲面上显示。您需要使用pygame.image.load将其转换为曲面。尽管强烈建议将.convert_alpha()添加到最后会大大提高帧速率,但并非绝对必需。

玩得开心Pygaming!

相关问题