Help with pygame


D

Daniel Kersgaard

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

Advertisements

C

Chris Angelico

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
 
D

Daniel Kersgaard

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!
 
D

Dave Angel

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"
 
Ad

Advertisements

T

Terry Reedy

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top