Help with pygame

Discussion in 'Python' started by Daniel Kersgaard, Jul 16, 2013.

  1. I'm having a little trouble, tried Googling it, but to no avail. Currently,I'm working on making a snake game, however I'm stuck on a simple border. The only thing I need help with is when you run the program, the bottom right corner of the border is missing. I'm not sure why. And I know I'm no where near finished, I've simply got a wall, and a randomly placed piece of food. A classmate mentioned increasing the BLOCK_SIZE variable in the width for loop, but that simply moved the top and bottom walls over one pixel. I'mstuck, and any help would be greatly appreciated! And I'm not sure if there is a better or easier way of providing my code, so I just pasted it below..

    import pygame as pg
    import random as rnd
    import sys

    #define colors using rgb
    RED = (255,0,0)
    RED_DARK = (150,0,0)
    GREEN = (0,255,0)
    GREEN_DARK = (0,150,0)
    BLUE = (0,0,255)
    BLUE_DARK = (0,0,150)
    WHITE = (255,255,255)
    BLACK = (0,0,0)

    #block size
    BLOCK_SIZE = 30


    #play area and game speed
    WIDTH = 25
    HEIGHT = 25
    SPEED = 8
    SPEED_TICK =2
    SPEED_NIC = 5
    SHORT = 12
    LONG = 1


    UP = 0
    DOWN = 1
    LEFT = 2
    RIGHT = 3


    class food:

    #class constructor
    def __init__(self, surface, min_xcord, max_xcord, min_ycord, max_ycord):
    self.surface = surface
    self.min_xcord = min_xcord
    self.max_xcord = max_xcord
    self.min_ycord = min_ycord
    self.max_ycord = max_ycord

    self.apple = pg.Surface((BLOCK_SIZE, BLOCK_SIZE))
    self.apple.set_alpha(255)
    self.apple.fill(RED)
    #get food position
    def getPosition(self):
    return (rnd.randint(self.min_xcord, self.max_xcord), rnd.randint(self.min_ycord, self.max_ycord))

    #draw the food on the play area
    def draw(self):
    position = self.getPosition()

    self.surface.blit(self.apple, (position[0] * BLOCK_SIZE, position[1] * BLOCK_SIZE))

    def drawWalls(surface):

    # create wall block
    wallblock = pg.Surface((BLOCK_SIZE, BLOCK_SIZE))
    wallblock.set_alpha(255)
    wallblock.fill(BLUE)

    #left and right walls
    for y in range(HEIGHT):
    surface.blit(wallblock, (0, y * BLOCK_SIZE))
    surface.blit(wallblock, (WIDTH * BLOCK_SIZE, y * BLOCK_SIZE))

    for x in range(WIDTH):
    surface.blit(wallblock, (x * BLOCK_SIZE, 0))
    surface.blit(wallblock, (x * BLOCK_SIZE, HEIGHT * BLOCK_SIZE))

    pg.display.flip()

    def main():

    #initalize pygame
    pg.init()

    #initalize the main screen, screen is 'pygame.Surface'
    screen = pg.display.set_mode(((WIDTH + 1) * BLOCK_SIZE, (HEIGHT + 1) * BLOCK_SIZE))
    screen.fill(BLACK)

    drawWalls(screen)

    myfood = food(screen, 1, 24, 1, 24)
    myfood.draw()

    pg.display.flip()


    main()
    Daniel Kersgaard, Jul 16, 2013
    #1
    1. Advertising

  2. On Wed, Jul 17, 2013 at 3:29 AM, Daniel Kersgaard
    <> wrote:
    > def drawWalls(surface):
    >
    > #left and right walls
    > for y in range(HEIGHT):
    > surface.blit(wallblock, (0, y * BLOCK_SIZE))
    > surface.blit(wallblock, (WIDTH * BLOCK_SIZE, y * BLOCK_SIZE))
    >
    > for x in range(WIDTH):
    > surface.blit(wallblock, (x * BLOCK_SIZE, 0))
    > surface.blit(wallblock, (x * BLOCK_SIZE, HEIGHT * BLOCK_SIZE))


    Hm. I'm not entirely sure as I don't have pygame to test your code on,
    but this strikes me as odd: you're blitting the x loop once for every
    iteration of the y loop. Shouldn't the two loops be at the same
    indentation?

    I think you perhaps want to offset one of the lines. Currently, you're
    running x from 0 up, and y from 0 up, so you're drawing the (0,0) cell
    twice. If you add 1 to one of them, you should be able to draw all
    four walls correctly. Alternatively, leave this as it is, and just add
    one more draw at (WIDTH, HEIGHT) to fill in the last square.

    ChrisA
    Chris Angelico, Jul 16, 2013
    #2
    1. Advertising

  3. I didn't even think about that! I added one more draw and it worked like a charm, thanks so much! I'm not sure why I couldn't think of that!
    Daniel Kersgaard, Jul 16, 2013
    #3
  4. Daniel Kersgaard

    Dave Angel Guest

    On 07/16/2013 01:29 PM, Daniel Kersgaard wrote:
    > I'm having a little trouble, tried Googling it, but to no avail. Currently, I'm working on making a snake game, however I'm stuck on a simple border. The only thing I need help with is when you run the program, the bottom right corner of the border is missing. I'm not sure why. And I know I'm no where near finished, I've simply got a wall, and a randomly placed piece of food. A classmate mentioned increasing the BLOCK_SIZE variable in the width for loop, but that simply moved the top and bottom walls over one pixel. I'm stuck, and any help would be greatly appreciated! And I'm not sure if there is a better or easier way of providing my code, so I just pasted it below.
    >
    > import pygame as pg
    > import random as rnd
    > import sys
    >
    > #define colors using rgb
    > RED = (255,0,0)
    > RED_DARK = (150,0,0)
    > GREEN = (0,255,0)
    > GREEN_DARK = (0,150,0)
    > BLUE = (0,0,255)
    > BLUE_DARK = (0,0,150)
    > WHITE = (255,255,255)
    > BLACK = (0,0,0)
    >
    > #block size
    > BLOCK_SIZE = 30
    >
    >
    > #play area and game speed
    > WIDTH = 25
    > HEIGHT = 25
    > SPEED = 8
    > SPEED_TICK =2
    > SPEED_NIC = 5
    > SHORT = 12
    > LONG = 1
    >
    >
    > UP = 0
    > DOWN = 1
    > LEFT = 2
    > RIGHT = 3
    >
    >
    > class food:
    >
    > #class constructor
    > def __init__(self, surface, min_xcord, max_xcord, min_ycord, max_ycord):
    > self.surface = surface
    > self.min_xcord = min_xcord
    > self.max_xcord = max_xcord
    > self.min_ycord = min_ycord
    > self.max_ycord = max_ycord
    >
    > self.apple = pg.Surface((BLOCK_SIZE, BLOCK_SIZE))
    > self.apple.set_alpha(255)
    > self.apple.fill(RED)
    > #get food position
    > def getPosition(self):
    > return (rnd.randint(self.min_xcord, self.max_xcord), rnd.randint(self.min_ycord, self.max_ycord))
    >
    > #draw the food on the play area
    > def draw(self):
    > position = self.getPosition()
    >
    > self.surface.blit(self.apple, (position[0] * BLOCK_SIZE, position[1] * BLOCK_SIZE))
    >
    > def drawWalls(surface):
    >
    > # create wall block
    > wallblock = pg.Surface((BLOCK_SIZE, BLOCK_SIZE))
    > wallblock.set_alpha(255)
    > wallblock.fill(BLUE)
    >
    > #left and right walls
    > for y in range(HEIGHT):
    > surface.blit(wallblock, (0, y * BLOCK_SIZE))
    > surface.blit(wallblock, (WIDTH * BLOCK_SIZE, y * BLOCK_SIZE))
    >
    > for x in range(WIDTH):
    > surface.blit(wallblock, (x * BLOCK_SIZE, 0))
    > surface.blit(wallblock, (x * BLOCK_SIZE, HEIGHT * BLOCK_SIZE))
    >


    Two things wrong here. One is that your lines are zero based and
    therefore they don't fill the far end, and other is that you draw the
    horizontal lines many times.

    #left and right walls
    for y in range(HEIGHT):
    surface.blit(wallblock, (0, y * BLOCK_SIZE))
    surface.blit(wallblock, (WIDTH * BLOCK_SIZE, (y+1) * BLOCK_SIZE))

    #top and bottom walls
    wallblock.fill(GREEN) #REMOVE ME
    for x in range(WIDTH):
    surface.blit(wallblock, ((x+1) * BLOCK_SIZE, 0))
    surface.blit(wallblock, (x * BLOCK_SIZE, HEIGHT * BLOCK_SIZE))

    What I did here was to (temporarily) color the top and bottom walls
    GREEN instead of BLUE, and you can better see what happens. I also
    fixed two of the walls to start at 1 instead of zero. And unindented
    the latter code so it only displays once.

    Once you see what it's doing, you probably want to remove the line that
    I labelled "REMOVE ME"


    > pg.display.flip()
    >
    > def main():
    >
    > #initalize pygame
    > pg.init()
    >
    > #initalize the main screen, screen is 'pygame.Surface'
    > screen = pg.display.set_mode(((WIDTH + 1) * BLOCK_SIZE, (HEIGHT + 1) * BLOCK_SIZE))
    > screen.fill(BLACK)
    >
    > drawWalls(screen)
    >
    > myfood = food(screen, 1, 24, 1, 24)
    > myfood.draw()
    >
    > pg.display.flip()
    >
    >
    > main()
    >



    --
    DaveA
    Dave Angel, Jul 16, 2013
    #4
  5. Daniel Kersgaard

    Terry Reedy Guest

    On 7/16/2013 1:29 PM, Daniel Kersgaard wrote:
    > I'm having a little trouble, tried Googling it, but to no avail.
    > Currently, I'm working on making a snake game, however
    > I'm stuck on a simple border.


    To give a variation of the other answers, it would be easier if you drew
    the four sides more symmetrically, in something like the following order:

    top (including both top corners)
    bottom (including both bottom corners)
    left (omitting both left corners)
    right (omitting both right corners)

    Including the corners with the sides instead of the top and bottom would
    be okay. So would be including one (different) corner with each line.
    Just pick a scheme that does each one once. Using the above, if 0, 0 and
    X, Y are upper left and bottom right corners,
    and we use inclusive ranges:

    top: 0, 0 to X, 0 # includes corners
    bot: 0, Y to X, Y # includes corners
    lef: 0, 1 to 0, Y-1 # excludes corners
    rit: X, 1 to X-1, Y-1 # excludes corners

    --
    Terry Jan Reedy
    Terry Reedy, Jul 17, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AnsNum

    mpeg problem with pygame

    AnsNum, Aug 27, 2003, in forum: Python
    Replies:
    2
    Views:
    691
    Fredrik Lundh
    Aug 27, 2003
  2. Kathleen Kudzma

    Tkinter and Pygame

    Kathleen Kudzma, Sep 16, 2003, in forum: Python
    Replies:
    0
    Views:
    395
    Kathleen Kudzma
    Sep 16, 2003
  3. Blaze Bresko

    Pygame Help

    Blaze Bresko, Jul 29, 2006, in forum: Python
    Replies:
    2
    Views:
    378
    Lee Harr
    Jul 29, 2006
  4. rantingrick

    [pygame-bug] Pygame.cdrom bug

    rantingrick, Jan 30, 2011, in forum: Python
    Replies:
    1
    Views:
    290
    Benjamin Kaplan
    Jan 30, 2011
  5. aapeetnootjes

    [newbie] help with pygame-tutorial

    aapeetnootjes, Apr 9, 2012, in forum: Python
    Replies:
    2
    Views:
    390
    aapeetnootjes
    Apr 10, 2012
Loading...

Share This Page