Help with pygame

Dave Angel davea at davea.name
Tue Jul 16 20:58:40 CEST 2013


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




More information about the Python-list mailing list