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


Groups > comp.lang.python > #50754

Re: Help with pygame

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'finished,': 0.07; 'sys': 0.07; '#define': 0.09; 'blue': 0.09; 'constructor': 0.09; 'currently,': 0.09; 'happens.': 0.09; 'latter': 0.09; 'main()': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'width': 0.09; 'subject:Help': 0.11; 'def': 0.12; 'random': 0.14; '(0,': 0.16; '0))': 0.16; '24,': 0.16; 'appreciated!': 0.16; 'doing,': 0.16; 'food:': 0.16; 'googling': 0.16; 'labelled': 0.16; 'main():': 0.16; 'once.': 0.16; 'pygame': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'screen,': 0.16; 'zero.': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'code,': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'why.': 0.24; "i've": 0.25; 'daniel': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'fixed': 0.29; 'moved': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'program,': 0.31; 'code': 0.31; 'easier': 0.31; 'lines': 0.31; "skip:' 10": 0.31; '"remove': 0.31; 'end,': 0.31; 'horizontal': 0.31; 'piece': 0.31; 'class': 0.32; 'probably': 0.32; 'run': 0.32; 'screen': 0.34; 'skip:_ 10': 0.34; 'subject:with': 0.35; 'but': 0.35; 'there': 0.35; 'height': 0.36; 'wrong': 0.37; 'two': 0.37; 'area': 0.37; 'displays': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'short': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'remove': 0.60; 'black': 0.61; 'color': 0.61; 'mentioned': 0.61; 'providing': 0.61; 'simply': 0.61; 'simple': 0.61; 'making': 0.63; 'wall': 0.65; 'here': 0.66; 'bottom': 0.67; 'below.': 0.71; 'food': 0.72; 'therefore': 0.72; 'increasing': 0.74; 'game,': 0.84; 'missing.': 0.84; 'snake': 0.84; 'surface': 0.84; 'walls': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re: Help with pygame
Date Tue, 16 Jul 2013 14:58:40 -0400
References <a18a24bf-7cbf-43ef-81a0-c13e7d812b35@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 174.32.174.30
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130623 Thunderbird/17.0.7
In-Reply-To <a18a24bf-7cbf-43ef-81a0-c13e7d812b35@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4780.1374001138.3114.python-list@python.org> (permalink)
Lines 127
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1374001138 news.xs4all.nl 15970 [2001:888:2000:d::a6]:54104
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50754

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web