Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pygame First Game Problems

Tags:

python

pygame

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!

like image 439
user1763931 Avatar asked Mar 11 '26 05:03

user1763931


1 Answers

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.

like image 121
SingleNegationElimination Avatar answered Mar 12 '26 20:03

SingleNegationElimination



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!