pygame

pygame

Postby Esther » Fri May 17, 2013 3:08 pm

Hi evryone, pls m currently workin on ma project, nd I need to load a file that contains instructions into the pygame window or screen, buh with these codes I av nt bin able to open the file into the screen, a blank space comes up instead of the instructions for the game. This are the codes for the instruction module plz help
Code: Select all
import pygame, os

WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
WIDTH, HEIGHT = 800, 500
class Instructions:

    def __init__(self):
        self.highfont = pygame.font.SysFont("Arial", 24)
        self.instructions = self.highfont.render("Instructions:", True, WHITE)
        self.back = self.highfont.render("BACK", True, WHITE)
        self.estee = self.highfont.render("Estee", True, WHITE)
       
        f = open("C:\Users\Esther\Desktop\instruction.txt", "r")
       
        f.close()

        self.frame_m4 = 2
        self.update_time_m4 = 0

    def render(self, screen, background, pieces):
        t = pygame.time.get_ticks()
        if self.MENU_I:
            screen.blit(background, (0, 0))
            screen.blit(self.instructions, (WIDTH/2 - self.highscore.get_width()/2, 15))
            screen.blit(self.back, (WIDTH/2 - self.backscore.get_width()/2, 400))
            screen.blit(self.estee, (600, 480))
            screen.blit(pieces[self.frame_m4], (370, 393))
           
        else:
            screen.blit(background, (0, 0))
            screen.blit(self.highscore, (WIDTH/2 - self.highscore.get_width()/2, 15))
            screen.blit(self.tryagain, (WIDTH/2 - self.tryagain.get_width()/2, 400))
            screen.blit(pieces[self.frame_m3], (343, 393))
           

                 
        if t - self.update_time_m4 > 1000:
            self.frame_m4 += 1
            if self.frame_m4 == 3:
                self.frame_m4 = 0
            self.update_time_m4 = t
               
Esther
 
Posts: 9
Joined: Tue Mar 12, 2013 5:38 pm

Re: pygame

Postby Mekire » Fri May 17, 2013 3:52 pm

I'm sorry I can't tell you anything from that. I need to see more of your program. Where are you updating the screen? Why are there so many attributes in the Instructions class that you use that were never defined? At the very least I will need to see your main game loop where this render method is called. If possible, and not too long show me your whole program or at least a working part.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: pygame

Postby Esther » Tue May 21, 2013 10:43 pm

this is the menu module of the game

Code: Select all
"""first phase"""
import pygame, os

WIDTH, HEIGHT = 800, 500
WHITE = (255, 255, 255)
GRAY = (170, 170, 170)
magenta3 = (205, 0, 205, 255)
 

class Menu:

    def __init__(self, fps = 40,):
        self.logo = pygame.image.load(os.path.join("graphics", "C:\Users\Esther\Pictures\games\logo4.png")).convert()
       
       
        #scale on frame(wide and long)
        self.logo = pygame.transform.scale(self.logo, (int(3.5*WIDTH/4), HEIGHT/3))
        self.logo.set_colorkey(self.logo.get_at((0, 0)))
        self.ship = pygame.image.load(os.path.join("graphics", "C:\Users\Esther\Pictures\games\Red.png")).convert()
        self.ship.set_colorkey(self.ship.get_at((0, 0)))
        self.gamelab = pygame.image.load(os.path.join("graphics", "C:\Users\Esther\Pictures\games\Lgamelab.png")).convert()
        self.gamelab.set_colorkey(self.gamelab.get_at((0, 0)))
       

        font = pygame.font.SysFont("Arial", 30)
        self.text = font.render("HESTY CARES", 1, WHITE)
        self.newgame_o = font.render("New Game", True, WHITE)
        self.newgame_f = font.render("New Game", True, magenta3)
        self.settings_o = font.render("Quit", True, WHITE)
        self.settings_f = font.render("Quit", True, magenta3)
        self.highscores_o = font.render("High Scores", True, WHITE)
        self.highscores_f = font.render("High Scores", True, magenta3)
        self.instructions_o = font.render("Instructions", True, WHITE)
        self.instructions_f = font.render("Instructions", True, magenta3)
       
        self.fontchoice = 0

        self.frame = 0
        self.frame_m = 0
        self.frame_m2 = 1
        self.delay = 1000/fps
        self.update_time = 0
        self.update_time_m = 0

    def render(self, screen, background, pieces):
        t = pygame.time.get_ticks()
        screen.blit(background, (0, 0))
        screen.blit(self.logo, (50, 100))
        screen.blit(self.text, (20,20))
        screen.blit(self.ship, (340,220))
        screen.blit(self.gamelab, (750, 450))
        #screen.blit(self.asteroid_pieces[self.frame], (WIDTH/2 - self.asteroid_pieces[self.frame].get_width()/2,
                                                      # HEIGHT/2 - self.asteroid_pieces[self.frame].get_height()/2))
        screen.blit(pieces[self.frame_m], (700, 50))
        screen.blit(pieces[self.frame_m2], (300, 50))
        if t - self.update_time > self.delay:
            self.frame += 1
            if self.frame_m == 3:
                self.frame_m = 0
            if self.frame == 142:
                self.frame = 0
            self.update_time = t

        if t - self.update_time_m > 1000:
            self.frame_m += 1
            self.frame_m2 += 1
            if self.frame_m == 3:
                self.frame_m = 0
            if self.frame_m2 == 3:
                self.frame_m2 = 0
            self.update_time_m = t
           
        if self.fontchoice == 0:
            screen.blit(self.newgame_o, (WIDTH/2 - self.newgame_o.get_width()/2, 2*HEIGHT/3))
            screen.blit(self.highscores_f, (WIDTH/2 - self.highscores_f.get_width()/2, 2*HEIGHT/3 + 35))
            screen.blit(self.instructions_f, (WIDTH/2 - self.instructions_f.get_width()/2, 2*HEIGHT/3 + 70))
            screen.blit(self.settings_f, (WIDTH/2 - self.settings_f.get_width()/2, 2*HEIGHT/3 + 105))

        elif self.fontchoice == 1:
            screen.blit(self.newgame_f, (WIDTH/2 - self.newgame_f.get_width()/2, 2*HEIGHT/3))
            screen.blit(self.highscores_o, (WIDTH/2 - self.highscores_f.get_width()/2, 2*HEIGHT/3 + 35))
            screen.blit(self.instructions_f, (WIDTH/2 - self.instructions_f.get_width()/2, 2*HEIGHT/3 + 70))
            screen.blit(self.settings_f, (WIDTH/2 - self.settings_o.get_width()/2, 2*HEIGHT/3 + 105))
           

        elif self.fontchoice == 2:
            screen.blit(self.newgame_f, (WIDTH/2 - self.newgame_f.get_width()/2, 2*HEIGHT/3))
            screen.blit(self.highscores_f, (WIDTH/2 - self.highscores_o.get_width()/2, 2*HEIGHT/3 + 35))
            screen.blit(self.instructions_o, (WIDTH/2 - self.instructions_f.get_width()/2, 2*HEIGHT/3 + 70))
            screen.blit(self.settings_f, (WIDTH/2 - self.settings_f.get_width()/2, 2*HEIGHT/3 + 105))
           

        elif self.fontchoice == 3:
            screen.blit(self.newgame_f, (WIDTH/2 - self.newgame_f.get_width()/2, 2*HEIGHT/3))
            screen.blit(self.highscores_f, (WIDTH/2 - self.highscores_o.get_width()/2, 2*HEIGHT/3 + 35))
            screen.blit(self.instructions_f, (WIDTH/2 - self.instructions_f.get_width()/2, 2*HEIGHT/3 + 70))
            screen.blit(self.settings_o, (WIDTH/2 - self.settings_f.get_width()/2, 2*HEIGHT/3 + 105))
           






and here is the main program, aw its starts
Code: Select all
 """This is the central module of the game"""
import pygame, os
from sprite import Sprite
from sound import Sound
from startup import Startup
from menu import Menu
from handleevent import HandleEvent
from game import Game
from highscore import Highscore
from instructions import Instructions

WIDTH, HEIGHT = 800, 500

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Spaceship Travel")


background = pygame.image.load(os.path.join("graphics", "C:\Users\Esther\Pictures\games\Space.jpg")).convert()
background = pygame.transform.scale(background, (WIDTH, HEIGHT))

class Play(Sprite, Startup, Menu, Game, Highscore, Sound, Instructions, HandleEvent):

    def __init__(self):
        pygame.init()
        self.ON = True
        self.STARTUP = True
        self.MENU = False
        self.MENU_H = False
        self.MENU_I = False
        self.GAME = False
        self.HIGHSCORE = False
        self.INSTRUCTIONS = False
        self.BLIT = True

        #pygame.mouse.set_visible(False)

        self.initializer()
        self.playgame()

    def initializer(self):
        Sprite.__init__(self)
        Sound.__init__(self)
        Startup. __init__(self)
        Menu.__init__(self)
        Game.__init__(self)
        Highscore.__init__(self)
        Instructions.__init__(self)

    def playgame(self):

        self.maintrack.play(-1)

        while self.ON:
            if self.STARTUP:
                Startup.render(self, screen, background, self.spaceman_pieces)
                pygame.display.update()
            elif self.MENU:
                Menu.render(self, screen, background, self.spaceman_pieces)
                pygame.display.update()
            elif self.GAME:
                Game.render(self, screen, background, pygame.time.get_ticks())
                pygame.display.update()
            elif self.HIGHSCORE:
                Highscore.render(self, screen, background, self.spaceman_pieces)
                pygame.display.update()
            elif self.INSTRUCTIONS:
                Instructions.render(self, screen, background, self.spaceman_pieces)
                pygame.display.update()
            for event in pygame.event.get():
                HandleEvent.__init__(self, event)
           
               
   



plss i rili dnt kno why the file isnt opening on the screen in the instruction frame with these codes for the instruction module,pls help
Code: Select all
import pygame, os

WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
WIDTH, HEIGHT = 800, 500
class Instructions:

    def __init__(self):
        self.highfont = pygame.font.SysFont("Arial", 24)
        self.instructions = self.highfont.render("Instructions:", True, WHITE)
        self.back = self.highfont.render("BACK", True, WHITE)
        self.estee = self.highfont.render("Estee", True, WHITE)
       
        f = open("C:\Users\Esther\Desktop\instruction.txt", "r")
       
        f.close()

        self.frame_m4 = 2
        self.update_time_m4 = 0

    def render(self, screen, background, pieces):
        t = pygame.time.get_ticks()
        if self.MENU_I:
            screen.blit(background, (0, 0))
            screen.blit(self.instructions, (WIDTH/2 - self.highscore.get_width()/2, 15))
            screen.blit(self.back, (WIDTH/2 - self.backscore.get_width()/2, 400))
            screen.blit(self.estee, (600, 480))
            screen.blit(pieces[self.frame_m4], (370, 393))
           
        else:
            screen.blit(background, (0, 0))
            screen.blit(self.highscore, (WIDTH/2 - self.highscore.get_width()/2, 15))
            screen.blit(self.tryagain, (WIDTH/2 - self.tryagain.get_width()/2, 400))
            screen.blit(pieces[self.frame_m3], (343, 393))
           

                 
        if t - self.update_time_m4 > 1000:
            self.frame_m4 += 1
            if self.frame_m4 == 3:
                self.frame_m4 = 0
            self.update_time_m4 = t
               
Esther
 
Posts: 9
Joined: Tue Mar 12, 2013 5:38 pm

Re: pygame

Postby Mekire » Wed May 22, 2013 12:12 am

Well again, I'm not really sure how you expect it to work, but let's start with the obvious.

This:
Code: Select all
f = open("C:\Users\Esther\Desktop\instruction.txt", "r")

f.close()
currently does nothing. You open the file; do nothing with it; then close it. So yeah, not sure what's going on there.

Now the next thing. The following variables were never declared:
Code: Select all
#undefined
self.highscore
self.backscore
self.tryagain
This may work in your program because you collapsed all the namespaces together into your Play class but it is not good.

A class should generally encapsulate all of the things it deals with. Mixins can be an exception to this but all of your other classes shouldn't be mixins here. If a class is a smaller block create an instance of it rather than inheriting from them.

Edit: My stupidity omitted.

If you can upload your entire program somewhere including all resources (graphics etc.) I will see what I can do. If you can't even clarify for me why you have undefined variables though then there is nothing I can do. In the meantime I suggest you read through some basic class tutorials as you are not using them correctly.

-Mek
Last edited by Mekire on Wed May 22, 2013 12:57 am, edited 1 time in total.
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: pygame

Postby Mekire » Wed May 22, 2013 12:27 am

Follow up on classes:

This is what you should be doing:
Code: Select all
class Main:
    def __init__(self):
        self.y = 5
        self.MySomething = Something()

class Something:
    def __init__(self):
        self.x = 2
    def some_meth(self):
        print("Main shouldn't have this.")

Better = Main()
print("Main.MySomething.x: {}".format(Better.MySomething.x))
Better.MySomething.some_meth()

-Mek

Edit: Based on Drago's post I looked back and realized that you inherited from every single other class in your Play class. So you weren't committing quite as cardinal an error as I originally thought you were. On the other hand, you are still collapsing everything into your Play namespace, so the details here still stand. You are inheriting for the wrong reason. Think of classes as inheriting from things more fundamental than themselves and containing things that would be parts of themselves. As Play is the bigger thing you wouldn't inherit the class HighScore; you would have an instance of it within your Play class. My apologies for possibly causing more confusion :D
Last edited by Mekire on Wed May 22, 2013 1:00 am, edited 2 times in total.
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: pygame

Postby DrakeMagi » Wed May 22, 2013 12:28 am

Esther I think you need to read what class inheritance is for.
I use it as plugin, overrides and polymorph.

Looks like you are trying to do page flipping. I could be wrong.
here an example.
Code: Select all
import pygame
pygame.init()

# Page class is use as an inheritance
class Page(object):
   def Entrance(self):
      pass
      
   def OnExit(self):
      pass
   
   def Blit(self, surface):
      pass
      
   def Event(self, event):
      pass
      
   def Update(self, tick):
      pass

# made to interface with Page Class
class Handler(object):
   def __init__(self, caption, width, height, flags = 0, depth=0 ):
      self.Screen = pygame.display.set_mode( (width, height), flags, depth )
      pygame.display.set_caption( caption )

      self.Clock = pygame.time.Clock()
      self.Tick = pygame.time.get_ticks()
      self.Fps = 60
      
      self.Pages = {}
      
      self.__current = None
      self.__new_page = None
      
   def SetPage(self, name):
      self.__new_page = name

   def Loop(self):
      Run = True;
      
      while(Run):
         if self.__new_page is not None:
            if self.__current is not None:
               self.Pages[self.__current].OnExit()
            self.__current = self.__new_page
            self.Pages[self.__current].Entrance()
            self.__new_page = None
            
         for event in pygame.event.get():
            if event.type == pygame.QUIT:
               Run = False
            else:
               if self.__current is not None:
                  self.Pages[self.__current].Event(event)
                  
         if self.__current is not None:
            self.Pages[self.__current].Blit(self.Screen)
            
         self.Tick = pygame.time.get_ticks()
         if self.__current is not None:
            self.Pages[self.__current].Update(self.Tick)
               
         pygame.display.flip()
         self.Clock.tick( self.Fps )

# example how it works
class Intro( Page ):
   def __init__(self):
      font = pygame.font.Font(None, 30)
      self.header = font.render("Welcome to my Intro Page.", 1, (0,200,0) )
      self.footer = font.render("Press Spacebar to continue ", 1, (0,200,0))

   def Blit(self, surface):
      surface.fill((0,0,0))
      surface.blit(self.header, (250,50))
      surface.blit(self.footer, (250,550))
      
   def Event(self, event):
      if event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
            screen.SetPage('Play')
            
class Play( Page ):
   def Blit(self,surface):
      surface.fill((0,100,200))
      
if __name__ == '__main__':
   screen = Handler("Sample Page Flipping", 800, 600)
   screen.Pages['Intro'] = Intro()
   screen.Pages['Play'] = Play()
   screen.SetPage('Intro')
   screen.Loop()
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 111
Joined: Sun May 12, 2013 8:36 pm

Re: pygame

Postby Esther » Wed May 22, 2013 8:53 pm

tanks, buh this is where ma major problem is, using he code below, m trying to open a file unto the pygame window, buh smtin is wrong cos the file doesnt come up to d screen.

Code: Select all
f = open("C:\Users\Esther\Desktop\instruction.txt", "r")

f.close()


i also tried it dis way:
Code: Select all
f = open("C:\Users\Esther\Desktop\instruction.txt", "r")
        f.readline()
        f.close()

but stil it doesnt read the file unto the screen.
all m trying to do is to open a file and read it unto the pygame window or screen. wud be glad if u guys can help out, tnxs :)
Esther
 
Posts: 9
Joined: Tue Mar 12, 2013 5:38 pm

Re: pygame

Postby DrakeMagi » Wed May 22, 2013 9:41 pm

To render string to pygame surface you will have to
1. make a pygame.font
2. render text with font
3. blit render image to surface

so make pygame font
make an empty list
loop through file append font.render(text)
then loop through render text and blit them to surface in (Draw/Blit) loop

in my example code. show how to make text to be render to screen.
Linux: won't find windows here.
Linux: the choice of a GNU generation.
https://github.com/DrakeMagi
DrakeMagi
 
Posts: 111
Joined: Sun May 12, 2013 8:36 pm

Re: pygame

Postby Mekire » Wed May 22, 2013 11:56 pm

So, yes. One of your problems is you don't know how to open files; in which case you may want to forgo game programming for a while and go back and review.

This would get the strings of your file into a list that you could then render:
Code: Select all
import os #goes at the top of the file
#Path should be relative to your main module.
filepath = os.path.join("relative","path","to","instructions.txt")
with open(filepath) as myfile:
    self.data_strings = myfile.readlines()
Note that the above DOES NOT render the text to the screen; You would need to later iterate through this list and render each line; and then finally blit. Note also that we use os.path.join instead of the explicit path. This is a good habit to get in to.

You could also render in the same step as Drake suggested:
Code: Select all
import os
self.rendered_instructions = []
filepath = os.path.join("relative","path","to","instructions.txt")
with open(filepath) as myfile:
    for line in myfile:
        rend = some_font.render(line, 1, (255,255,255))
        self.rendered_instructions.append(rend)
Then iterate and blit the images somewhere in your draw phase.

Aside from this, as stated your program has structural issues. You should really look into reviewing inheritance and classes in general. You will be able to get your game working this way, but that isn't the point. You could write your entire game in the global namespace with hundreds of global variables too; it doesn't mean you should. Collapsing everything into a single class is almost as bad.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: pygame

Postby Esther » Sat May 25, 2013 12:29 am

i rili appreciate bin workin on this and notin seems to apen, tried it i had an error IOError: [Errno 2] No such file or directory, a little help plz
Code: Select all
self.rendered_instruction = []
        filepath = os.path.join(r"C:\Users\Esther\Desktop", "instruction.txt")
        with open(filepath) as myfile:
            for line in myfile:
                rend = font.render(line, 1, (255,255,255))
                self.rendered_instruction.append(rend)
Esther
 
Posts: 9
Joined: Tue Mar 12, 2013 5:38 pm

Re: pygame

Postby Mekire » Sat May 25, 2013 2:57 am

This just shows simple rendering and blitting from a list of words, but using a file really isn't any different.
Code: Select all
import pygame as pg,sys

class Control:
    def __init__(self):
        self.done = False
        pg.init()
        self.screen= pg.display.set_mode((500,500))
        self.Clock = pg.time.Clock()
       
        lines = ["Spam","Eggs","Milk","Cheese"]
        myfont = pg.font.SysFont('timesnewroman', 30)
        self.rendered_instruction = self.render(lines,myfont)

    def render(self,lines,font):
        rendered= []
        for line in lines:
            rend = font.render(line, 1, (255,255,255))
            rendered.append(rend)
        return rendered

    def event_loop(self):
        keys = pg.key.get_pressed()
        for event in pg.event.get():
            if event.type == pg.QUIT or keys[pg.K_ESCAPE]:
                self.done = True

    def blit_text(self):
        for i,msg in enumerate(self.rendered_instruction):
            msg_rect = msg.get_rect(center=(250,50+50*i))
            self.screen.blit(msg,(msg_rect))

    def main(self):
        while not self.done:
            self.event_loop()
            self.screen.fill(0)
            self.blit_text()
            pg.display.update()
            self.Clock.tick(60)

if __name__ == "__main__":
    RunIt = Control()
    RunIt.main()
    pg.quit();sys.exit()

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: pygame

Postby Mekire » Sat May 25, 2013 3:22 am

Same as previous example but using a file instead. Download the attached file and place it and this script in the same directory.

Code: Select all
import pygame as pg,sys

class Control:
    def __init__(self):
        self.done = False
        pg.init()
        self.screen= pg.display.set_mode((600,400))
        self.screen_rect = self.screen.get_rect()
        self.Clock = pg.time.Clock()

        with open("again.txt") as myfile:
            lines = [line.strip() for line in myfile]
        myfont = pg.font.SysFont('timesnewroman', 25)
        self.rendered_instruction = self.render_lines(lines,myfont,(255,255,255))

    def render_lines(self,lines,font,color):
        return [font.render(line, 1, color) for line in lines]

    def event_loop(self):
        keys = pg.key.get_pressed()
        for event in pg.event.get():
            if event.type == pg.QUIT or keys[pg.K_ESCAPE]:
                self.done = True

    def blit_text(self):
        for i,msg in enumerate(self.rendered_instruction):
            msg_rect = msg.get_rect(center=(self.screen_rect.centerx,50+50*i))
            self.screen.blit(msg,(msg_rect))

    def main(self):
        while not self.done:
            self.event_loop()
            self.screen.fill(0)
            self.blit_text()
            pg.display.update()
            self.Clock.tick(60)

if __name__ == "__main__":
    RunIt = Control()
    RunIt.main()
    pg.quit();sys.exit()

-Mek

Edit: As to your IOError, you should be using a relative path from your module to the file. The easiest way to do this would be to simply place the script and the file in the same directory. Then you wouldn't need to worry about the path. You would just use the filename. If you use an absolute path from your "C:" then this program will never run on anyone else's computer. My advice to use os.path.join meant that you shouldn't be useing "/" or "\" or a directory separator of any kind in your paths. os.path.join adds the directory separator that your operating system expects for you.

-Mek
Attachments
again.txt
(197 Bytes) Downloaded 181 times
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: pygame

Postby Esther » Mon May 27, 2013 10:38 pm

hi, tanks yep i got that the codes worked but my codes arent contained in d same text editor, they are sparated in different modules since it a game. heres the instruction module which stil doesnt open my file to the screen.
Code: Select all
import pygame, sys

WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)

WIDTH, HEIGHT = 800, 500



class Instructions:

    def __init__(self):
        self.done = False
        myfont = pygame.font.SysFont('timesnewroman', 25)
        self.instructions = self.font.render("Instructions:", True, WHITE)
        self.back = self.font.render("BACK", True, WHITE)
        self.estee = self.font.render("Estee", True, WHITE)
       
       
        with open("instruction.txt") as myfile:
            lines = [line.strip() for line in myfile]
        myfont = pg.font.SysFont('timesnewroman', 25)
        self.rendered_instruction = self.render(lines,myfont,(255,255,255))
       

        self.frame_m4 = 2
        self.update_time_m4 = 0

     
    def render(self, screen, background, lines, font, color):
       
        t = pygame.time.get_ticks()
        #self.lines = True
        #self.font = True
        #self.color = True
        if self.MENU_I:
            screen.blit(background, (0, 0))
            screen.blit(self.instructions, (WIDTH/2 - self.instructions.get_width()/2, 15))
            screen.blit(self.back, (WIDTH/2 - self.backscore.get_width()/2, 400))
            screen.blit(self.estee, (780, 480))
            screen.blit(pieces[self.frame_m4], (370, 393))
            #screen.blit(self.rendered_instruction, (WIDTH/2 - self.instruction.get_width()/2, 15))
           
            for line in lines:
                rend = font.render(line, 1, WHITE)
                return
            for i,msg in enumerate(self.rendered_instruction):
                msg_rect = msg.get_rect(center=(WIDTH/2 - self.instruction.get_width()/2, 15))
                screen.blit(msg,(msg_rect))
           

        if t - self.update_time_m4 > 1000:
            self.frame_m4 += 1
            if self.frame_m4 == 3:
                self.frame_m4 = 0
            self.update_time_m4 = t
               
 








and here is where the whole rendering and bliting of the game is for all the modules i av:
Code: Select all
 """This is the central module of the game"""
import pygame, os
from sprite import Sprite
from sound import Sound
from startup import Startup
from menu import Menu
from handleevent import HandleEvent
from game import Game
from highscore import Highscore
from instructions import Instructions

WIDTH, HEIGHT = 800, 500

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Spaceship Battle")


background = pygame.image.load(os.path.join("graphics", "C:\Users\Esther\Pictures\games\Space.jpg")).convert()
background = pygame.transform.scale(background, (WIDTH, HEIGHT))

class Play(Sprite, Startup, Menu, Game, Highscore, Sound, Instructions, HandleEvent):

    def __init__(self):
        pygame.init()
        self.ON = True
        self.STARTUP = True
        self.MENU = False
        self.MENU_H = False
        self.MENU_I = False
        self.GAME = False
        self.HIGHSCORE = False
        self.INSTRUCTIONS = False
        self.BLIT = True

        #pygame.mouse.set_visible(False)

        self.initializer()
        self.playgame()

    def initializer(self):
        Sprite.__init__(self)
        Sound.__init__(self)
        Startup. __init__(self)
        Menu.__init__(self)
        Game.__init__(self)
        Highscore.__init__(self)
        Instructions.__init__(self)

    def playgame(self):

        self.maintrack.play(-1)

        while self.ON:
            if self.STARTUP:
                Startup.render(self, screen, background, self.spaceman_pieces)
                pygame.display.update()
            elif self.MENU:
                Menu.render(self, screen, background, self.spaceman_pieces)
                pygame.display.update()
            elif self.GAME:
                Game.render(self, screen, background, pygame.time.get_ticks())
                pygame.display.update()
            elif self.HIGHSCORE:
                Highscore.render(self, screen, background, self.spaceman_pieces)
                pygame.display.update()
            elif self.INSTRUCTIONS:
                Instructions.render(self, screen, background, lines, font, color)
                pygame.display.update()
            for event in pygame.event.get():
                HandleEvent.__init__(self, event)
 

wud appreciate a little help fuda,tnxs :)
Esther
 
Posts: 9
Joined: Tue Mar 12, 2013 5:38 pm

Re: pygame

Postby Mekire » Tue May 28, 2013 12:24 am

The arguments your render function takes don't match your call.

Also I have no clue what that return statement is doing in your render method.
Code: Select all
for line in lines:
    rend = font.render(line, 1, WHITE)
    return
Assuming lines isn't an empty iterable, you never make it to the instruction rendering part below.
Code: Select all
for i,msg in enumerate(self.rendered_instruction):
    msg_rect = msg.get_rect(center=(WIDTH/2 - self.instruction.get_width()/2, 15))
    screen.blit(msg,(msg_rect))
The above never runs; not to mention the fact that if it does all the text will blit on top of itself (that is what the i enumerator is for).

Honestly I would like to help you but you really seem like you need to go back and study some basics. The previous example I posted does what you are asking for; there is no difference between it and your program regardless of how many modules you use. Files need to be where you can find them. Whether this means they are in the same directory or a different directory isn't the point.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: pygame

Postby Mekire » Tue May 28, 2013 1:33 am

This is the best attempt I can give you with what you are providing:

Change your instruction file to this:
Code: Select all
import pygame, sys

WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)

WIDTH, HEIGHT = 800, 500



class Instructions:

    def __init__(self):
        self.done = False
        myfont = pygame.font.SysFont('timesnewroman', 25)
        self.instructions = self.font.render("Instructions:", True, WHITE)
        self.back = self.font.render("BACK", True, WHITE)
        self.estee = self.font.render("Estee", True, WHITE)

        with open("instruction.txt") as myfile:
            lines = [line.strip() for line in myfile]
        myfont = pg.font.SysFont('timesnewroman', 25)
        self.rendered_instruction = self.render_lines(lines,myfont,(255,255,255))

        self.frame_m4 = 2
        self.update_time_m4 = 0

    def render_lines(self,lines,font,color):
        return [font.render(line, 1, color) for line in lines]

    def render(self, screen, background):
        t = pygame.time.get_ticks()
        if self.MENU_I:
            screen.blit(background, (0, 0))
            screen.blit(self.instructions, (WIDTH/2 - self.instructions.get_width()/2, 15))
            screen.blit(self.back, (WIDTH/2 - self.backscore.get_width()/2, 400))
            screen.blit(self.estee, (780, 480))
            screen.blit(pieces[self.frame_m4], (370, 393))

            for i,msg in enumerate(self.rendered_instruction):
                msg_rect = msg.get_rect(center=(WIDTH/2 - self.instruction.get_width()/2, i*50+15))
                screen.blit(msg,msg_rect)

        if t - self.update_time_m4 > 1000:
            self.frame_m4 += 1
            if self.frame_m4 == 3:
                self.frame_m4 = 0
            self.update_time_m4 = t


And change these the lines that call render for instructions in your playgame() function to this:
Code: Select all
elif self.INSTRUCTIONS:
    Instructions.render(self, screen, background)
    pygame.display.update()

No guarantee that this will remotely work, but give it a shot.
-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: Google [Bot] and 2 guests