user1763931
user1763931

Reputation: 33

Pygame First Game Problems

I'm new at Python and Pygame and I started making a simple game to shoot under the player who is flying above the enemies to test what I have learned. When it runs I just get errors saying it does not want strings. It is as following:

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

Can anyone help me find the errors and explain to me why they are wrong? Thanks!

Upvotes: 3

Views: 379

Answers (2)

Replace the first three lines of code to the following:

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

You cannot display a string onto the screen, only surfaces. You need to use pygame.image.load to turn it into a surface. Adding .convert_alpha() to the end greatly improves the framerate, but is not absolutely required, though it is highly recommended.

Have fun Pygaming!

Upvotes: 0

SingleNegationElimination
SingleNegationElimination

Reputation: 156148

looks like you forgot to convert the "proj.png" to a surface. it's still the string name of the file you never actually opened.

Upvotes: 2

Related Questions