Text Size
Text Size
Exit Full Screen
Python Mini
Show
Source Code
View Source Code in Full Screen
Open in New Tab
from processing import * from random import randint xs = [75,125,175,225,275,325,375,425] speed = [2,1,5] board = [ [1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1], [1,1,1,1,1,1,1,1], ] class piece(): inst = [] idnum = 0 def __init__(self,x,y,look): self.x = x self.y = y self.look = look self.idnum = piece.idnum piece.idnum += 1 piece.inst.append(self) mous = {'p':0,'h':None,'ph':None,'snap':0,'sticky':0,'capt':0} curs = {'p':0,'h':None,'ph':None,'x':25,'y':225,'f':0} def setup(): global b1, w1, b2, w2, b3, w3, b4, w4, b5, w5, b6, w6, b7, w7, cursor, cursorsel, cursorslow, cursorselslow, cursorfast, cursorselfast print('a ------------------------- remove board squares') print('q,w,e,r,t,y,u ------------- spawn white pieces') print('1,2,3,4,5,6,7 ------------- spawn black pieces') print('enter --------------------- toggle activate cursor') print('shift --------------------- cycle cursor speed') print('up, down, left, right ----- control cursor') print('^^^^ ADVANCED CONTROLS ^^^^') size(500,600) for i in xs: piece(i,125,11) for i in xs: piece(i,375,21) piece(175,75,12);piece(325,75,12) piece(175,425,22);piece(325,425,22) piece(125,75,13);piece(375,75,13) piece(125,425,23);piece(375,425,23) piece(75,75,14);piece(425,75,14) piece(75,425,24);piece(425,425,24) piece(275,75,15) piece(275,425,25) piece(225,75,16) piece(225,425,26) b1 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-1.png') w1 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-2.png') b2 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-3.png') w2 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-4.png') b3 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-5.png') w3 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-6.png') b4 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-7.png') w4 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-8.png') b5 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-9.png') w5 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-10.png') b6 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-11.png') w6 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-12.png') b7 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-13.png') w7 = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-14.png') cursor = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-15.png') cursorslow = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-16.png') cursorsel = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-17.png') cursorselslow = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-18.png') cursorfast = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-19.png') cursorselfast = loadImage('https://oyohub.s3.amazonaws.com/spriteeditor/projects/59ff7bf1ca292c38749df130/chess-20.png') def mousePressed(): mous['p'] = 1 def mouseReleased(): mous['p'] = 0 def keyPressed(): if keyboard.key == '1': piece(mouse.x,mouse.y,11) if keyboard.key == 'q': piece(mouse.x,mouse.y,21) if keyboard.key == '3': piece(mouse.x,mouse.y,12) if keyboard.key == 'e': piece(mouse.x,mouse.y,22) if keyboard.key == '2': piece(mouse.x,mouse.y,13) if keyboard.key == 'w': piece(mouse.x,mouse.y,23) if keyboard.key == '4': piece(mouse.x,mouse.y,14) if keyboard.key == 'r': piece(mouse.x,mouse.y,24) if keyboard.key == '6': piece(mouse.x,mouse.y,15) if keyboard.key == 'y': piece(mouse.x,mouse.y,25) if keyboard.key == '5': piece(mouse.x,mouse.y,16) if keyboard.key == 't': piece(mouse.x,mouse.y,26) if keyboard.key == '7': piece(mouse.x,mouse.y,17) if keyboard.key == 'u': piece(mouse.x,mouse.y,27) if keyboard.key == ' ': for i in piece.inst: if curs['x'] >= i.x - 25 and curs['x'] <= i.x + 25 and curs['y'] <= i.y + 25 and curs['y'] >= i.y - 25 and curs['p'] > 0: piece.inst.remove(i) curs['p'] = 40 if mouse.px >= i.x - 25 and mouse.px <= i.x + 25 and mouse.py <= i.y + 25 and mouse.py >= i.y - 25 and mous['p'] > 0: try: piece.inst.remove(i) except: pass if curs['p'] == 40: curs['p'] = 0 if keyboard.key == 'a': if mouse.y >= 50 and mouse.y < 450 and mouse.x >= 50 and mouse.x <= 450: board[mouse.y/50-1][mouse.x/50-1] = (board[mouse.y/50-1][mouse.x/50-1] + 1) % 2 if keyboard.keyCode == UP: curs['y'] -= 5 * speed[curs['f']] if keyboard.keyCode == DOWN: curs['y'] += 5 * speed[curs['f']] if keyboard.keyCode == LEFT: curs['x'] -= 5 * speed[curs['f']] if keyboard.keyCode == RIGHT: curs['x'] += 5 * speed[curs['f']] if keyboard.keyCode == SHIFT: curs['f'] = (curs['f'] + 1) % 3 if keyboard.keyCode == ENTER: if curs['p'] > 0: curs['p'] = 0 else: curs['p'] = 1 def checkPiece(a): if a == 11: return b1 elif a == 21: return w1 elif a == 12: return b2 elif a == 22: return w2 elif a == 13: return b3 elif a == 23: return w3 elif a == 14: return b4 elif a == 24: return w4 elif a == 15: return b5 elif a == 25: return w5 elif a == 16: return b6 elif a == 26: return w6 elif a == 17: return b7 elif a == 27: return w7 def cursorState(): if curs['p'] > 0: if curs['f'] == 0: return cursorsel elif curs['f'] == 2: return cursorselfast else: return cursorselslow else: if curs['f'] == 0: return cursor elif curs['f'] == 2: return cursorfast else: return cursorslow def draw(): background(220,160,80) if mous['p'] > 0: mous['p'] += 1 if mous['p'] > 30: #hold number limit mous['p'] = 30 if curs['p'] > 0: curs['p'] += 1 if curs['p'] > 30: #hold number limit curs['p'] = 30 rectMode(CENTER,CENTER) imageMode(CENTER,CENTER) #make board for j in range(8): for i in range(8): if (j + i) % 2 == 1: fill(100) else: fill(150) if board[j][i] == 1: rect(xs[i],xs[j],50,50) fill(0,255,0) #piece layering for i in piece.inst: if mous['h'] == i.idnum: if i != piece.inst[-1]: piece.inst.append(piece.inst.pop(piece.inst.index(i))) if curs['h'] == i.idnum: if i != piece.inst[-1]: piece.inst.append(piece.inst.pop(piece.inst.index(i))) #handle holding pieces mous['ph'] = mous['h'] curs['ph'] = curs['h'] if mous['p'] == 0: mous['h'] = None if curs['p'] == 0: curs['h'] = None for i in piece.inst: if mouse.px >= i.x - 25 and mouse.px <= i.x + 25 and mouse.py <= i.y + 25 and mouse.py >= i.y - 25 and mous['p'] > 0: if i.idnum == mous['h'] or mous['h'] == None or mous['sticky'] == 1: i.x += mouse.x - mouse.px i.y += mouse.y - mouse.py if mous['h'] == None: mous['h'] = i.idnum if curs['x'] >= i.x - 25 and curs['x'] <= i.x + 25 and curs['y'] <= i.y + 25 and curs['y'] >= i.y - 25 and curs['p'] > 0: if i.idnum == curs['h'] or curs['h'] == None or mous['sticky'] == 1: i.x = curs['x'] i.y = curs['y'] if curs['h'] == None: curs['h'] = i.idnum image(checkPiece(i.look),i.x,i.y,50,50) #snapping if mous['h'] != i.idnum and curs['h'] != i.idnum and mous['snap'] == 1: i.y = int(i.y/50)*50+25 i.x = int(i.x/50)*50+25 #buttons if mous['snap'] == 1: fill(130) else: fill(200) rect(100,525,100,50) fill(0) textAlign(CENTER,CENTER) textSize(15) text('Snap to Grid',100,525) if mouse.x >= 50 and mouse.x <= 150 and mouse.y >= 500 and mouse.y <= 550 and mous['p'] == 2: mous['snap'] = (mous['snap'] + 1) % 2 if curs['x'] >= 50 and curs['x'] <= 150 and curs['y'] >= 500 and curs['y'] <= 550 and curs['p'] == 2: mous['snap'] = (mous['snap'] + 1) % 2 if mous['sticky'] == 1: fill(130) else: fill(200) rect(250,525,100,50) fill(0) textAlign(CENTER,CENTER) textSize(15) text('Sticky Pieces',250,525) if mouse.x >= 200 and mouse.x <= 300 and mouse.y >= 500 and mouse.y <= 550 and mous['p'] == 2: mous['sticky'] = (mous['sticky'] + 1) % 2 if curs['x'] >= 200 and curs['x'] <= 300 and curs['y'] >= 500 and curs['y'] <= 550 and curs['p'] == 2: mous['sticky'] = (mous['sticky'] + 1) % 2 if mous['capt'] == 1: fill(130) else: fill(200) rect(400,525,100,50) fill(0) textAlign(CENTER,CENTER) textSize(15) text('Capturing',400,525) if mouse.x >= 350 and mouse.x <= 450 and mouse.y >= 500 and mouse.y <= 550 and mous['p'] == 2: mous['capt'] = (mous['capt'] + 1) % 2 if curs['x'] >= 350 and curs['x'] <= 450 and curs['y'] >= 500 and curs['y'] <= 550 and curs['p'] == 2: mous['capt'] = (mous['capt'] + 1) % 2 #capturing for i in piece.inst: if i.idnum == mous['ph'] and i.idnum != mous['h'] and mous['capt'] == 1: for j in piece.inst: if i.idnum != j.idnum: if j.x >= i.x - 25 and j.x <= i.x + 25 and j.y >= i.y - 25 and j.y <= i.y + 25: piece.inst.remove(j) if i.idnum == curs['ph'] and i.idnum != curs['h'] and mous['capt'] == 1: for j in piece.inst: if i.idnum != j.idnum: if j.x >= i.x - 25 and j.x <= i.x + 25 and j.y >= i.y - 25 and j.y <= i.y + 25: piece.inst.remove(j) #cursor image(cursorState(),curs['x'],curs['y'],50,50) run()
stickychess.py
( around 286 lines python code )
Published By:
luyfru
Published on
2024-06-06T23:48:00Z
Python Mini
- an
OYOclass
application,
own your own class today
.
Run
Result
×
Error message shows here