snake game

snake game

Postby jack » Wed Jun 26, 2013 12:13 am

i started programming a snake game, and i just got what i thought would be enough to make a square appear on the screen, but i'm getting a blank screen with this error: Traceback (most recent call last):
File "C:\Python33\snake.py", line 16, in <module>
window_filler.fill(pygame.color("black"))
TypeError: 'module' object is not callable

here is my code:
Code: Select all
import pygame
import random
import sys
from pygame.locals import *

#set up pygame
pygame.init()

#set up window
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
window_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0)
pygame.display.set_caption("SNAKE")

window_filler = window_surface.copy()
window_filler.fill(pygame.color("black"))
window_filler.set_alpha(50)

#set up snake
snake_image = pygame.Surface((5, 5))
snake_image.fill(pygame.Color("green"))

snake = pygame.Rect(300, 100, 5, 5)
jack
 
Posts: 5
Joined: Thu Jun 13, 2013 10:29 pm

Re: snake game

Postby metulburr » Wed Jun 26, 2013 12:23 am

regarding the error:
you have pygame.color and not pygame.Color


the blank screen is because you never blit the surface to the screen. Although even if you did, you would only see it for a very short time as you have no loop

Code: Select all
import pygame

class Player:
    def __init__(self):
        self.pos = (0,0)
        self.image = pygame.Surface((5, 5))
        self.image.fill(pygame.Color("green"))
    def update(self, scr):
        scr.blit(self.image, self.pos)

class Control:
    def __init__(self):
        pygame.init()
        self.screensize = (400,400)
        self.screen = pygame.display.set_mode(self.screensize)
        pygame.display.set_caption('SNAKE')
        self.gamestate = True
        self.player = Player()
       
        self.mainloop()
       
    def update(self):
        self.player.update(self.screen)
   
    def mainloop(self):
        while self.gamestate:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.gamestate = False
            self.update()
            pygame.display.flip()

app = Control()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1407
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: snake game

Postby Mekire » Wed Jun 26, 2013 12:30 am

You need a game loop that includes an event loop to get any pygame off the ground.

This is about the bare minimum to get your program running:
Code: Select all
import random,sys,os
import pygame as pg

#set up pg
pg.init()

#set up window
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
pg.display.set_caption("SNAKE")
SCREEN = pg.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
Clock = pg.time.Clock()

snake_image = pg.Surface((5, 5)).convert()
snake_image.fill(pg.Color("green"))
snake_rect = pg.Rect(300, 100, 5, 5)

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    SCREEN.blit(snake_image,snake_rect)
    pg.display.update()
    Clock.tick(60)
pg.quit();sys.exit()

If you don't know how to use function I strongly suggest you cover this prior to trying to start game programming. Ideally you should be very comfortable with classes as well. I have some sample code posted here if you would like to take a look:
http://code.google.com/p/my-pygame-samples/downloads/list

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: snake game

Postby metulburr » Wed Jun 26, 2013 12:34 am

@Mekire
Do you have a github with repos of all your games?
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1407
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: snake game

Postby Mekire » Wed Jun 26, 2013 12:37 am

@Metul: For the more recent projects if you go to the Source tab on the google code page, you can clone the repo or browse the code. For the older ones I haven't added yet.

Repo for the sample code is here:
http://code.google.com/p/my-pygame-samples/source/browse/

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: W3C [Linkcheck] and 4 guests