Hi i'm trying to make a basic snake game. However i cannot seem to be able to display any food. I have tried numerous different ways but i just can't get anything to show :@. I simply want a red rectangle that will display in random places everytime the snake hits it. Any help would be very much appreciated.

# -*- coding: cp1252 -*-
import pygame , pygame .mixer , random, sys
from pygame.locals import *

ballimg = 'c:/Python/images/segment.png'
caption = 'Snake Game using PyGame .'
screen_size = width , height = 800 , 600
speed = [0 , 1]
black = (0, 0, 0)
applepos = (random.randint (0, 590), random.randint(0, 590))
pygame . init ()
screen = pygame . display . set_mode ( screen_size )
pygame . display . set_caption ( caption )
appleimage = Surface.fill(color, rect=20, 20, special_flags=0): return rect
appleimage.fill((255, 0, 0))
ball = pygame . image . load ( ballimg ). convert ()
ballrect = ball . get_rect ()
finished = False
paused = False

while not finished :

    for event in pygame . event . get ():

        if event . type == KEYDOWN:
            if event.key == K_DOWN:
                speed = [0 , 1]
            elif event.key == K_UP:
                speed = [0 , -1]
            elif event.key == K_LEFT:
                speed = [-1 , 0]
            elif event.key == K_RIGHT:
                speed = [1 , 0]
            elif event.key == K_ESCAPE:
                finished = True
            elif event.key == K_PAUSE:
                paused = not paused




            if event . type == pygame . QUIT :
                sys . exit ()

    pygame.time.delay (10)


    if paused == False:
        ballrect = ballrect . move ( speed )
        screen . fill ( black )        
        screen . blit (ball , ballrect )
        pygame . display . flip ()

        #check to see if its hit the wall
        
        print ballrect.left, ballrect.right
        
        if ballrect .left <0 or ballrect .right > width or ballrect .top <0 or ballrect . bottom > height :
            f=pygame.font.SysFont('Arial', 30)
            t=f.render('YOU DIED! Your score was: ', True, (255, 255, 255))
            paused = True

print "Finished"

Recommended Answers

All 2 Replies

I think you are using blit() incorrectly all my code is on another machine atm so I can't check for sure. Anyhow I'm pretty sure you can't just blit without passing in a location in the form of a tuple.

example: screen.blit(ball,(xposition,yposition)) or in other words
screen.blit(ball,(100,100))
screen.blit(ballrect,(myrandomvariable,myrandomvariable))

predator78 is right. Also, I don't know if it's the website, your IDE, or format errors from copying and pasting; most of your object methods are spaced which will throw exceptions.
I.E.:

#your code
pygame . display . flip()

#should be
pygame.display.flip()

That goes for most all of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.