Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50749 > unrolled thread

Help with pygame

Started byDaniel Kersgaard <danielkersgaard@gmail.com>
First post2013-07-16 10:29 -0700
Last post2013-07-16 20:10 -0400
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Help with pygame Daniel Kersgaard <danielkersgaard@gmail.com> - 2013-07-16 10:29 -0700
    Re: Help with pygame Chris Angelico <rosuav@gmail.com> - 2013-07-17 04:16 +1000
      Re: Help with pygame Daniel Kersgaard <danielkersgaard@gmail.com> - 2013-07-16 11:33 -0700
    Re: Help with pygame Dave Angel <davea@davea.name> - 2013-07-16 14:58 -0400
    Re: Help with pygame Terry Reedy <tjreedy@udel.edu> - 2013-07-16 20:10 -0400

#50749 — Help with pygame

FromDaniel Kersgaard <danielkersgaard@gmail.com>
Date2013-07-16 10:29 -0700
SubjectHelp with pygame
Message-ID<a18a24bf-7cbf-43ef-81a0-c13e7d812b35@googlegroups.com>
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))

    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()

[toc] | [next] | [standalone]


#50751

FromChris Angelico <rosuav@gmail.com>
Date2013-07-17 04:16 +1000
Message-ID<mailman.4778.1373998613.3114.python-list@python.org>
In reply to#50749
On Wed, Jul 17, 2013 at 3:29 AM, Daniel Kersgaard
<danielkersgaard@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#50752

FromDaniel Kersgaard <danielkersgaard@gmail.com>
Date2013-07-16 11:33 -0700
Message-ID<f66ef1f2-1226-4f2f-afb2-8ff11cd0e7c7@googlegroups.com>
In reply to#50751
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!

[toc] | [prev] | [next] | [standalone]


#50754

FromDave Angel <davea@davea.name>
Date2013-07-16 14:58 -0400
Message-ID<mailman.4780.1374001138.3114.python-list@python.org>
In reply to#50749
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

[toc] | [prev] | [next] | [standalone]


#50762

FromTerry Reedy <tjreedy@udel.edu>
Date2013-07-16 20:10 -0400
Message-ID<mailman.4785.1374019827.3114.python-list@python.org>
In reply to#50749
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web