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


Groups > comp.lang.python > #50749

Help with pygame

Newsgroups comp.lang.python
Date 2013-07-16 10:29 -0700
Message-ID <a18a24bf-7cbf-43ef-81a0-c13e7d812b35@googlegroups.com> (permalink)
Subject Help with pygame
From Daniel Kersgaard <danielkersgaard@gmail.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextNext 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