Python Forum
[PyGame] I found a way to generate sprites without .blit. Is it effecient?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] I found a way to generate sprites without .blit. Is it effecient?
#13
I did try to put everything into one loop, but I haven't been able to get the thing to run (when I press enter at the start screen, it just freezes and I have no clue why). That's why the version I actually play uses a separate loop for each screen. For some reason, that's the only way I could find to do it.

Here's that version if anyone's interested:

import random
import pygame
pygame.init()

screenWidth=500
screenHeight=500

mainWindow=pygame.display.set_mode((screenWidth,screenHeight))
pygame.display.set_caption("PySnake")

#snake's graphics
x=screenWidth/2
y=screenHeight/2
snakeSize=50
move="down"
length=0
path=[]
#food's graphics
posx=0
posy=0
#fps
fps=60
fpsClock=pygame.time.Clock()
#game states
win=True
state='start'
#sound effects
loseGameSound=pygame.mixer.Sound('pysnake_lose_game.wav')
eatSound=pygame.mixer.Sound('pysnake_eat.wav')
bleep=pygame.mixer.Sound('pysnake_bleep.wav')

font=pygame.font.SysFont('arialblack',20)



def frandxy():
    posx=snakeSize*(random.randint(0,screenWidth/snakeSize-1))
    posy=snakeSize*(random.randint(0,screenWidth/snakeSize-1))
    if (posx,posy) in path or (posx,posy)==(x,y):
            frandxy()

def closeGame():
    pygame.quit()
    quit()

frandxy()
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            closeGame()

    key=pygame.key.get_pressed()
    if key[pygame.K_ESCAPE]:
        closeGame()

    if state=='start':
        titleFont=pygame.font.SysFont('arialblack',50)
        title=titleFont.render('PySnake',0,(0,255,0))
        enterPrompt=font.render('Press Enter to Start',0,(0,255,0))

        mainWindow.blit(title,(screenWidth/2-title.get_width()/2,screenHeight/2-snakeSize*2))
        mainWindow.blit(enterPrompt,(screenWidth/2-enterPrompt.get_width()/2,screenHeight/2))

        for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    closeGame()

        key=pygame.key.get_pressed()
        if key[pygame.K_RETURN]:
            bleep.play()
            state='game'
    if state=='game':
        for event in pygame.event.get():
                if event.type==pygame.QUIT:
                    closeGame()

        key=pygame.key.get_pressed()
        if key[pygame.K_UP] and move != "down":
            move="up"
        if key[pygame.K_w] and move !="down":
            move="up"
        if key[pygame.K_DOWN] and move != "up":
            move="down"
        if key[pygame.K_s] and move != "up":
            move="down"
        if key[pygame.K_RIGHT] and move != "left":
            move="right"
        if key[pygame.K_d] and move != "left":
            move="right"
        if key[pygame.K_LEFT] and move != "right":
            move="left"
        if key[pygame.K_a] and move != "right":
            move="left"
        if key[pygame.K_ESCAPE]:
            closeGame()
        if key[pygame.K_e]:
            closeGame()

            path.insert(0,(x,y))
            #move snake
            if move=="down" and y<=screenHeight-snakeSize:
                y+=snakeSize
            if move=="down" and y>=screenHeight:
                y=0
            if move=="up" and y>=0:
                y-=snakeSize
            if move=="up" and y<0:
                y=screenHeight-snakeSize
            if move=="right" and x<=screenWidth-snakeSize:
                x+=snakeSize
            if move=="right" and x>=screenWidth:
                x=0
            if move=="left" and x>=0:
                x-=snakeSize
            if move=="left" and x<0:
                x=screenWidth-snakeSize

            if x==posx and y==posy:
                length+=1
                eatSound.play()
                frandxy()
            if (x,y) in path:
                win=False
                state='end'
            if length>=((screenWidth/snakeSize)*(screenHeight/snakeSize))-1:
                state='end'

            mainWindow.fill((0,0,0))
            #draw snake
            fps=6
            if length>0:
                for i in range(length):
                    pygame.draw.rect(mainWindow, (0,255,0),(path[i][0],path[i][1], snakeSize,snakeSize))
            pygame.draw.rect(mainWindow, (0,255,0),(x, y, snakeSize, snakeSize))
            #draw food
            pygame.draw.rect(mainWindow, (255,0,0),(posx,posy, snakeSize, snakeSize))
            fps=60
            if len(path)>length:
                path=path[:(length)]
    if state=='end':
        key=pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
            closeGame()
        if key[pygame.K_RETURN]:
            bleep.play()
            state='playAgain'
        if win:
            mainWindow.fill((0,255,0))
            textTop=font.render('Your Snake Filled the Screen!',0,(0,0,0))
            textBottom=font.render(('Congratulations!'),0,(0,0,0))
        if win==False:
            textTop=font.render('GAME OVER',0,(0,0,0))
            textBottom=font.render(('Your Length Was: '+str(length)),0,(0,0,0))

        mainWindow.fill((0,0,0))
        pygame.draw.rect(mainWindow,(0,255,0),(screenWidth/2-snakeSize*3,\
                                               screenHeight/2-snakeSize,\
                                               snakeSize*6,snakeSize*2))
        mainWindow.blit(textTop, (screenWidth/2-textTop.get_width()/2,screenHeight/2-snakeSize))
        mainWindow.blit(textBottom, (screenWidth/2-textBottom.get_width()/2,screenHeight/2))

    if state=='playAgain':
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                closeGame()

        key=pygame.key.get_pressed()
        if key[pygame.K_ESCAPE]:
            bleep.play()
            closeGame()
        if key[pygame.K_y]:
            bleep.play()
            win=True
            x=screenWidth/2
            y=screenHeight/2
            length=0
            move="down"
            path=[]
        if key[pygame.K_n]:
            closeGame()
        if win==False:
            mainWindow.fill((0,0,0))
        if win==True:
            mainWindow.fill((0,255,0))
        pygame.draw.rect(mainWindow,(0,255,0),(screenWidth/2-snakeSize*3,\
                                               screenHeight/2-snakeSize,\
                                               snakeSize*6,snakeSize*2))
        font=pygame.font.SysFont('arialblack',20)
        textTop=font.render('Play Again?',0,(0,0,0))
        textBottom=font.render(('(y)es/(n)o'),0,(0,0,0))

        mainWindow.blit(textTop, (screenWidth/2-textTop.get_width()/2,screenHeight/2-snakeSize))
        mainWindow.blit(textBottom, (screenWidth/2-textBottom.get_width()/2,screenHeight/2))

    pygame.display.update()
    fpsClock.tick(fps)

And yeah, having a quit() function may be kinda silly, but it feels a lot more elegant than just letting the game crash (which is what mine used to do when you lost).
Reply


Messages In This Thread
RE: I found a way to generate sprites without .blit. Is it effecient? - by xBlackHeartx - Dec-06-2019, 02:53 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame, sprites, and rects menator01 12 7,206 Dec-07-2023, 02:37 AM
Last Post: Benixon
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 3,400 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  [PyGame] pygame blit() method syafiq14 1 6,774 Oct-30-2020, 04:46 PM
Last Post: Russ_CW
  index error when using lists to blit rotated images codefun45 7 5,309 Sep-03-2020, 11:11 PM
Last Post: codefun45
  moving image with blit rwahdan 2 4,052 Jul-10-2019, 06:24 PM
Last Post: nilamo
  [PyGame] Having 4 players(Sprites) all being able to jump ElijahCastle 5 5,290 May-07-2019, 05:04 PM
Last Post: SheeppOSU
  Sprites and Actor error ajlconsulting 6 12,489 Jan-30-2019, 12:50 AM
Last Post: metulburr
  draw not showing all sprites ethanstrominger 0 3,192 Jan-25-2019, 10:10 PM
Last Post: ethanstrominger
  error with survace.blit joemcsk1 3 5,903 Aug-06-2018, 12:23 AM
Last Post: metulburr
  [PyGame] move randomly sprites reutB 4 9,819 Mar-29-2017, 01:12 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020