Python Forum
[PyGame] Block Game
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Block Game
#1
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]
Reply
#2
Pretty cool
Reply
#3
Thanks

Oh, and I forgot to mention this, I'm open to new ideas for features to add to the program.
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame clicker game CrazyMakes 2 13,166 May-26-2020, 07:41 PM
Last Post: CrazyMakes
  Snake Game in Python with Pygame Bjdamaster 2 7,999 Aug-13-2018, 01:11 PM
Last Post: metulburr
  Flappy Bird Game [Pygame] georgecoopers 2 4,562 Apr-30-2017, 07:59 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

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