Python Forum
[PyGame] Block Game - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code sharing (https://python-forum.io/forum-5.html)
+--- Thread: [PyGame] Block Game (/thread-10645.html)



[PyGame] Block Game - Zman350x - May-29-2018

A simple game that lets you draw with blocks. Made in a few hours to test/first use pickle (actually cPickle). I know the code is messy, don't worry about that, I made this in a rush. Tell me what you think.

NOTES

Needs 3 images(found below code)
Made on Python 2.7.13, unsure if it works on 3
Need to have pygame to run

#Controls

print '-------------------------\nControls\n\nMove Cursor: Arrow Keys\nDraw/Erase: Space Bar\nClear Screen(White): W\nClear Screen(Black): B\n-------------------------'

#imports

import pygame, cPickle as pic, os

if os.path.exists('WorldList.pickle'):
    WorldOpen = open('WorldList.pickle','rb')
    WorldList = pic.load(WorldOpen)
else:
    WorldList = [0] * 750
    WorldPickle = open('WorldList.pickle','wb')
    pic.dump(WorldList,WorldPickle,2)
    WorldPickle.close()
BC = pygame.image.load("BlackCursor.png")
WC = pygame.image.load("WhiteCursor.png")
Icon = pygame.image.load("icon.png")

#colors

white = 255,255,255
black = 0,0,0

#base info

pygame.init()
screenWidth = 600
screenHeight = 500
screen = pygame.display.set_mode((screenWidth,screenHeight))
pygame.display.set_caption('Block Game')
pygame.display.set_icon(Icon)
block = pygame.Surface((20,20))
block.fill(black)
cursor = pygame.Surface((20,20))
cursor.fill(black)
cursor.set_alpha(0)
cX = 300
cY = 240
cXc = 0
cYc = 0
clock = pygame.time.Clock()
FPS = 5
valIndex = 375
space = False

#Game Loop

while True:

    #Event Check

    for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    WorldPickle = open('WorldList.pickle','wb')
                    pic.dump(WorldList,WorldPickle,2)
                    WorldPickle.close()
                    pygame.quit()
                    quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LEFT:
                        cXc -= 20
                    if event.key == pygame.K_RIGHT:
                        cXc += 20
                    if event.key == pygame.K_UP:
                        cYc = - 20
                    if event.key == pygame.K_DOWN:
                        cYc = 20
                    if event.key == pygame.K_SPACE:
                        if WorldList[valIndex] == 1:
                            WorldList[valIndex] = 0
                        else:
                            WorldList[valIndex] = 1
                        space = True
                    if event.key == pygame.K_w:
                        cWindex = 0
                        for i in WorldList:
                            WorldList[cWindex] = 0
                            cWindex += 1
                    if event.key == pygame.K_b:
                        cBindex = 0
                        for i in WorldList:
                            WorldList[cBindex] = 1
                            cBindex += 1
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                        cXc = 0
                    if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                        cYc = 0
                    if event.key == pygame.K_SPACE:
                        space = False

    #Movement

    if space == True and valIndex != paval:
        if WorldList[valIndex] == 1:
            WorldList[valIndex] = 0
        else:
            WorldList[valIndex] = 1
    x = 0
    y = 0

    indexID = 0
    cX += cXc
    cY += cYc
    if cX < 0:
        cX = 0
    if cX >= screenWidth:
        cX = 580
    if cY < 0:
        cY = 0
    if cY >= screenHeight:
        cY = 480

    screen.fill(white)
    paval = valIndex

    for blockID in WorldList:
        if x == 600:
            x = 0
            y += 20
        if blockID == 1:
            screen.blit(block,(x,y))
        if cX == x and cY == y:
            valIndex = indexID
        x += 20
        indexID+=1

    #Cursor

    screen.blit(cursor,(cX,cY))
    if WorldList[valIndex] == 1:
        screen.blit(WC,(cX,cY))
    else:
        screen.blit(BC,(cX,cY))

    #Display Update

    pygame.display.update()
    clock.tick(FPS)
Here are the Images
[Image: b5Lfxd]
[Image: cURyiJ]
[Image: fTShAy]


RE: [PyGame] Block Game - fierygaming - May-29-2018

Pretty cool


RE: [PyGame] Block Game - Zman350x - May-29-2018

Thanks

Oh, and I forgot to mention this, I'm open to new ideas for features to add to the program.


RE: [PyGame] Block Game - fierygaming - May-30-2018

(May-29-2018, 09:36 PM)Zman350x Wrote: Thanks

Oh, and I forgot to mention this, I'm open to new ideas for features to add to the program.

It would be cool if you added in some pre-loads for the player so they could get an idea of what to do