Inyoung Kim 김인영
Inyoung Kim 김인영

Reputation: 1597

Python, PyGame image not displaying

I am new to pygame and just learned what class is

I wanted to make the player shoot bullets but the bullets aren't drawn on the screen.

There are 4 python files

  1. Game.py: The main file. Gets inputs, updates and draws screen
  2. background.py: class for background
  3. player.py: class for player and has player actions
  4. ect.py: bullet class. Initialization of bullet object.

So what I thought was to first load image and make surface for each bullet, second update position, then draw image on surface. If bullet is near destination delete it. While running every time I press spacebar to call shoot function this warning comes up"libpng warning: iCCP: known incorrect sRGB profile". I believe I can ignore this.Anyways I wasn't able to find out why the bullet image didn't show up. Can anyone please help me with this? Thanks. And any advice is welcomed!

Game.py

import pygame as pyg
from pygame.locals import *
import sys
import player
import background, ect

#Screen size
s_width = 1280
s_height = 720
screen_size = (s_width,s_height)

#Player
p_speed = 5

#Initialization
pyg.init()
screen = pyg.display.set_mode(screen_size)
pyg.display.set_caption("Shoot")


#Set background
Background = background.Background("forest2.jpeg",(0,0))

#Set Player
Player= player.Player("Crab.png",100,100,640,360)

#Input
keys = {"right":False , "left":False ,  "up":False , "down":False , "bullet":False }
bullets = []

#Main Loop
running = True
while running:

    #Get inputs
    for event in pyg.event.get():
        if event.type == QUIT:
            pyg.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                keys['left'] = True
            elif event.key == K_RIGHT:
                keys['right'] = True
            elif event.key == K_UP:
                keys['up'] = True
            elif event.key == K_DOWN:
                keys['down'] = True
            elif event.key == K_SPACE:
                keys['bullet'] = True
            elif event.key == K_q:
                pyg.quit()
                sys.exit()


        elif event.type == KEYUP:
            if event.key == K_LEFT:
                keys['left'] = False
            elif event.key == K_RIGHT:
                keys['right'] = False
            elif event.key == K_UP:
                keys['up'] = False
            elif event.key == K_DOWN:
                keys['down'] = False
            elif event.key == K_SPACE:
                keys['bullet'] = False

    #Get time
    Player.clock.tick(60)
    dt = Player.clock.get_time()
    dt *= 0.001

    #Update values
    if keys['right']:
        Player.x += p_speed
    if keys['left']:
        Player.x -= p_speed
    if (keys['up'] and Player.on_ground):
        Player.jump()
    if keys['bullet']:
        Player.shoot(bullets)

    if Player.on_ground == False: #Gravity
        Player.give_force(0,-750)

    Player.update_pos(dt)

    if Player.y >= 500:
        Player.y = 500
        Player.y_vel = 0
        Player.y_accel = 0

        Player.on_ground = True

    for bullet in bullets:
        bullet.update(dt)
        if (bullet.desx - bullet.x)<0.3:
            del bullet


    #Draw
    screen.fill([255,255,255])
    screen.blit(Background.image,Background.rect)
    screen.blit(Player.player_sprite,(Player.x,Player.y))

    for bullet in bullets:
        bullet.draw_bullet()

    #Update game state
    pyg.display.flip()
    pyg.display.update()

player.py

#imports
import pygame as pyg
import sys, math
import ect




class Player(pyg.sprite.Sprite):
    """
    This class represents the player image
    and contains player actions
    """

    def __init__(self, player_sprite_file, width, height, x, y):
        pyg.sprite.Sprite.__init__(self)

        #initialize variables(Size, Coordinate, clock)
        self.width = width
        self.height = height
        self.x = x + width/2
        self.y = y - height/2
        self.on_ground = False
        self.clock = pyg.time.Clock()
        self.x_vel = 0
        self.y_vel = 0
        self.x_accel = 0
        self.y_accel = 0

        #create sprite(1. Load 2. Scale 3. Get rect 4. Create surface
        self.player_sprite = pyg.image.load(player_sprite_file)
        self.player_sprite = pyg.transform.scale(self.player_sprite, (width,height))
        self.player_sprite_rect = self.player_sprite.get_rect()
        self.player_sprite_rect.left ,self.player_sprite_rect.top = self.x, self.y

    def move(self,x,y): #Moves player to destination
        self.x = x + self.width/2
        self.y = y - self.height/2


    def give_force(self, x_accel, y_accel): #Input: A vector(direction, amount) for now lets keep the direction only up like a jump
        self.x_accel += x_accel
        self.y_accel += y_accel

    def update_pos(self,dt): #Updates 1. time 2. velocity based on acceleration 3. position based on velocity

        self.x_vel += self.x_accel*dt
        self.y_vel += self.y_accel*dt

        self.x += self.x_vel*dt
        self.y -= self.y_vel*dt

    def jump(self):
        self.y_vel += 0
        self.give_force(0,6000)
        self.on_ground = False

    def shoot(self,bullets):
        bullets.append(ect.Bullet(self.x, self.y,self.x+300,self.y))

ect.py

#Imports
import random, math, pygame as pyg
from pygame.locals import *


class Bullet(pyg.sprite.Sprite):
    """
    This class represents bullets
    """

    def __init__(self,x,y,desx, desy):
        pyg.sprite.Sprite.__init__(self)
        self.x = x
        self.y = y
        self.desx = desx
        self.desy = desy
        h = math.sqrt(math.pow(desx-x,2) + math.pow(desy-y,2))
        self.speed = 1 + random.randrange(-1,1)
        self.speedx = self.speed*math.acos((desx - x)/h)
        self.speedy = self.speed*math.asin((desy - y)/h)

        self.surface = pyg.Surface((128,128))
        self.image = pyg.image.load("bullet.png")
        self.image = pyg.transform.scale(self.image, (128, 128))
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = x + 64, y -64

    def update(self,dt):
        self.x += (self.speedx*dt)
        self.y -= (self.speedy*dt)

    def draw_bullet(self):
        self.surface.blit(self.image,(self.x -64,self.y + 64))

Upvotes: 2

Views: 694

Answers (1)

skrx
skrx

Reputation: 20488

You only blit the self.image of the bullets onto the self.surface instead of the screen surface. Pass the screen to the draw_bullet method and then blit the self.image onto it.

# In the main while loop.
for bullet in bullets:
    bullet.draw_bullet(screen)

#---------------------
# In the Bullet class.
def draw_bullet(self, screen):
    screen.blit(self.image, (self.x -64, self.y + 64))

Upvotes: 1

Related Questions