[Tutor] Fwd: uploading images in pygame

fatima butt fatimabttt at gmail.com
Wed Apr 17 05:14:15 EDT 2019


the python version is 3.7.3
computer is acer SWIFT
The error I get is following:
Traceback (most recent call last):
  File "C:\Users\ammah\OneDrive\Documents\project1\myCode.py.py
<http://mycode.py.py/>", line 84, in <module>
    background = pygame.image.load(path.join(img_dir,"ship1.jpg")).convert()
pygame.error: Couldn't open
C:\Users\ammah\OneDrive\Documents\project1\ship1.jpg
>>>

The code that I entered in my Python shell is as following:
# Pygame template - skeleton for a new pygame project
import pygame
import random
from os import path

img_dir = path.dirname(__file__)


WIDTH = 480
HEIGHT = 600
FPS = 60

# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255,255,0)

pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()


class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((80,70))
        self.image.fill(GREEN)
        self.rect =self.image.get_rect()
        self.rect.centerx = WIDTH / 2
        self.rect.bottom = HEIGHT -10
        self.speedx = 0

    def update(self):
        self.speedx = 0
        keystate = pygame.key.get_pressed()
        if keystate[pygame.K_LEFT]:
            self.speedx = 5
        if keystate[pygame.K_RIGHT]:
            self.speedx = -5
        self.rect.x += self.speedx

    def shoot(self):
        bullet = Bullet(self.rect.centerx, self.rect.top)
        all_sprites.add(bullet)
        bullets.add(bullet)

class Mob(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface ((40,30))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.x =random.randrange(WIDTH-self.rect.width)
        self.rect.y=random.randrange(-100,-40)
        self.speedy=random.randrange(1,8)

    def update(self):
        self.rect.y += self.speedy
        if self.rect.top > HEIGHT +10:
            self.rect.x =random.randrange(WIDTH-self.rect.width)
            self.rect.y=random.randrange(-100,-40)
            self.speedy=random.randrange(1,8)

class Bullet(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((10,20))
        self.image.fill(YELLOW)
        self.rect = self.image.get_rect()
        self.rect.bottom = y
        self.rect.centerx = x
        self.speedy = -10

    def update(self):
        self.rect.y += self.speedy
        if self.rect.bottom<0:
            self.kill()

#Load all game graphics
background = pygame.image.load(path.join(img_dir,"ship1.jpg")).convert()
background_rect = background.get_rect()



all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
    m = Mob()
    all_sprites.add(m)
    mobs.add(m)
# Game loop
running = True
while running:
    # keep loop running at the right speed
    clock.tick(FPS)
    # Process input (events)
    for event in pygame.event.get():
        # check for closing window
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                player.shoot()
    # Update
    all_sprites.update()
    #check to see if a bullet hit a mob
    #check to see if a mob hit the player
    hits = pygame.sprite.spritecollide(player,mobs,False)
    if hits:
        running = False

    # Draw / render
    screen.fill(BLACK)
    screen.blit(background, background_rect)
    all_sprites.draw(screen)
    # *after* drawing everything, flip the display
    pygame.display.flip()

pygame.quit()


On Tue, 16 Apr 2019 at 23:34, Alan Gauld via Tutor <tutor at python.org> wrote:

> On 16/04/2019 20:54, fatima butt wrote:
> > please i am getting error..
>
> Hi, we need to be quite specific here about the details
> because it is not clear exactly what you are trying to do.
>
> > i am trying to upload image from openspaceart
> > from internet .
>
> Just to be clear.
> Uploading means going from your computer onto a server
> on the internet. So, I'm assuming what you are trying
> to do is download an image from openspaceart to your
> computer. Is that correct?
>
> Or are you in fact trying to upload an image from your
> computer to the openspaceart server?
>
> There is a big difference between the two.
>
> > I have downloaded the image on my desktop
>
> So it sounds like you have succeeded in downloading
> the image from the server and now have a copy on
> your local computer? Is that correct?
>
> > and i am trying
> > to upload this to the pygame.
>
> But pygame is not on a network it is on your computer
> so you don't need to upload the image, you should
> just need to access it from within pygame.
>
> To help with that we need to know exactly what you
> are trying to do with the image in pygame. Are you
> displaying it as a background? Creating sprites?
> Using it as part of a GUI, maybe on a button?
>
> It would really help if you can show us some code.
> Even if it doesn't work it will help us understand
> what you are trying to do.
>
> Also if you get any error messages send them too.
> But send it as text, the list does not accept
> attachments since they pose a security risk.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list