Match-3 game - match dependent sounds - python

I'm looking to get some help with a match-3 game I'm working on in pygame. I've loaded the images and sounds as follows and the 'gems' will be the classical elements air, earth, fire and water. How can I play the appropriate water sound file, for example, when 3 or more water sprites are matched? Don't need help with any of the game code just to create an association between the image and audio files and how to play it 'while matchedElements != []'. Thank you.
# Directions
UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'
# Space to the sides of grid
XMARGIN = int((WIDTH - ELEMENTSIZE * GRIDWIDTH) / 2)
YMARGIN = int((HEIGHT - ELEMENTSIZE * GRIDHEIGHT) / 2)
EMPTY_SPACE = -1
ROWABOVEBOARD = 'row above board'
# Colours
AIR = pygame.Color(145, 129, 129)
FIRE = pygame.Color(255, 123, 0)
WATER = pygame.Color(93, 118, 245)
EARTH = pygame.Color(22, 136, 0)
ELECTRIC = pygame.Color(22, 204, 0)
SMOKE = pygame.Color(222, 222, 222)
ICE = pygame.Color(234, 231, 255)
METAL = pygame.Color(105, 105, 105)
BLOOD = pygame.Color(222, 7, 7)
# FPS controller
fpsController = pygame.time.Clock()
def main():
global FPSCLOCK, BOARDRECTS, ELEMENTIMAGES, SOUNDS, PLAYSURF, BASICFONT
# Basic set up
pygame.init()
FPSCLOCK = pygame.time.Clock()
PLAYSURF = pygame.display.set_mode((WIDTH, HEIGHT))
BASICFONT = pygame.font.Font('freesansbold.ttf', 36)
# Load images
ELEMENTIMAGES = []
for i in range(1, NUMELEMENTS+1):
elementImage = pygame.image.load('element%s.jpg' % i)
if elementImage.get_size() != (ELEMENTSIZE, ELEMENTSIZE):
elementImage = pygame.transform.smoothscale(elementImage, (ELEMENTSIZE, ELEMENTSIZE))
ELEMENTIMAGES.append(elementImage)
# Load sounds
SOUNDS = {}
SOUNDS['bad swap'] = pygame.mixer.Sound('badswap.wav')
SOUNDS['match'] = []
for i in range(NUMMATCHSOUNDS):
SOUNDS['match'].append(pygame.mixer.Sound('elementsound%s.wav' % i))
# Rect objects for board space conversions
BOARDRECTS = []
for x in range(GRIDWIDTH):
BOARDRECTS.append([])
for y in range(GRIDHEIGHT):
r = pygame.Rect((XMARGIN + (x * ELEMENTSIZE),
YMARGIN + (y * ELEMENTSIZE),
ELEMENTSIZE, ELEMENTSIZE))
BOARDRECTS[x].append(r)
while True:
runGame()
def runGame():
# Board initialisation
gameBoard = getBlankBoard()
score = 0
fillBoardAndAnimate(gameBoard, [], score) # Drop initial elements
# Initialise new game variables
firstSelectedElement = None
lastMouseDownX = None
lastMouseDownY = None
gameIsOver = False
lastScoreDeduction = time.time()
clickContinueTextSurf = None
# Main game loop
while True:
clickedSpace = None
for event in pygame.event.get(): # Event handling
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
pygame.quit()
sys.exit()
elif event.type == KEYUP and event.key == K_BACKSPACE:
return # new game
elif event.type == MOUSEBUTTONUP:
if gameIsOver:
return # click to start new game
if event.pos == (lastMouseDownX, lastMouseDownY):
clickedSpace = checkForElementClick(event.pos)
else:
firstSelectedElement = checkForElementClick((lastMouseDownX, lastMouseDownY))
clickedSpace = checkForElementClick(event.pos)
if not firstSelectedElement or not clickedSpace:
firstSelectedElement = None
clickedSpace = None
elif event.type == MOUSEBUTTONDOWN:
lastMouseDownX, lastMouseDownY = event.pos
if clickedSpace and not firstSelectedElement:
firstSelectedElement = clickedSpace
elif clickedSpace and firstSelectedElement:
firstSwappingElement, secondSwappingElement = getSwappingElements(gameBoard, firstSelectedElement, clickedSpace)
if firstSwappingElement == None and secondSwappingElement == None:
# If both are None, elements are not adjacent
firstSelectedElement = None
continue
# Swap animation
boardCopy = getBoardCopyMinusElements(gameBoard, (firstSwappingElement, secondSwappingElement))
animateMovingElements(boardCopy, [firstSwappingElement, secondSwappingElement], [], score)
# Swap elements in the board
gameBoard[firstSwappingElement['x']][firstSwappingElement['y']] = secondSwappingElement['imageNum']
gameBoard[secondSwappingElement['x']][secondSwappingElement['y']] = firstSwappingElement['imageNum']
# See if this is a match
matchedElements = findMatchingElements(gameBoard)
if matchedElements == []:
# No match - swap back
SOUNDS['bad swap'].play()
animateMovingElements(boardCopy, [firstSwappingElement, secondSwappingElement], [], score)
gameBoard[firstSwappingElement['x']][firstSwappingElement['y']] = firstSwappingElement['imageNum']
gameBoard[secondSwappingElement['x']][secondSwappingElement['y']] = secondSwappingElement['imageNum']
else:
# A match
scoreAdd = 0
while matchedElements != []:
points = []
for elementSet in matchedElements:
scoreAdd += (10 + (len(elementSet) - 3) * 10)
for element in elementSet:
gameBoard[element[0]][element[1]] = EMPTY_SPACE
points.append({'points': scoreAdd,
'x': element[0] * ELEMENTSIZE + XMARGIN,
'y': element[1] * ELEMENTSIZE + YMARGIN})
score += scoreAdd
# Drop new elements
fillBoardAndAnimate(gameBoard, points, score)
# Check for new matches
matchedElements = findMatchingElements(gameBoard)
firstSelectedElement = None
if not canMakeMove(gameBoard):
gameIsOver = True
# Draw the board
PLAYSURF.fill(BGCOLOUR)
drawBoard(gameBoard)
if firstSelectedElement != None:
highlightSpace(firstSelectedElement['x'], firstSelectedElement['y'])
if gameIsOver:
if clickContinueTextSurf == None:
clickContinueTextSurf = BASICFONT.render('Final Score: %s (Click to continue)' % (score), 1, GAMEOVERCOLOUR, GAMEOVERBGCOLOUR)
clickContinueTextRect = clickContinueTextSurf.get_rect()
clickContinueTextRect.center = int(WIDTH / 2), int(HEIGHT / 2)
PLAYSURF.blit(clickContinueTextSurf, clickContinueTextRect)
elif score > 0 and time.time() - lastScoreDeduction > DEDUCTSPEED:
# score drops over time
score -= 1
lastScoreDeduction = time.time()
drawScore(score)
pygame.display.update()
FPSCLOCK.tick(FPS)
def getSwappingElements(board, firstXY, secondXY):
# If the elements at the (X, Y) coordinates of the two elements are adjacent,
# then their 'direction' keys are set to the appropriate direction
# value to be swapped with each other.
# Otherwise, (None, None) is returned.
firstElement = {'imageNum': board[firstXY['x']][firstXY['y']],
'x': firstXY['x'],
'y': firstXY['y']}
secondElement = {'imageNum': board[secondXY['x']][secondXY['y']],
'x': secondXY['x'],
'y': secondXY['y']}
highlightedElement = None
if firstElement['x'] == secondElement['x'] + 1 and firstElement['y'] == secondElement['y']:
firstElement['direction'] = LEFT
secondElement['direction'] = RIGHT
elif firstElement['x'] == secondElement['x'] - 1 and firstElement['y'] == secondElement['y']:
firstElement['direction'] = RIGHT
secondElement['direction'] = LEFT
elif firstElement['y'] == secondElement['y'] + 1 and firstElement['x'] == secondElement['x']:
firstElement['direction'] = UP
secondElement['direction'] = DOWN
elif firstElement['y'] == secondElement['y'] - 1 and firstElement['x'] == secondElement['x']:
firstElement['direction'] = DOWN
secondElement['direction'] = UP
else:
# These elements are not adjacent and can't be swapped.
return None, None
return firstElement, secondElement
def getBlankBoard():
# Create and return a blank board data structure.
board = []
for x in range(GRIDWIDTH):
board.append([EMPTY_SPACE] * GRIDHEIGHT)
return board
def canMakeMove(board):
# Return True if the board is in a state where a matching
# move can be made on it. Otherwise return False.
# The patterns in oneOffPatterns represent elements that are configured
# in a way where it only takes one move to make a triplet.
oneOffPatterns = (((0,1), (1,0), (2,0)),
((0,1), (1,1), (2,0)),
((0,0), (1,1), (2,0)),
((0,1), (1,0), (2,1)),
((0,0), (1,0), (2,1)),
((0,0), (1,1), (2,1)),
((0,0), (0,2), (0,3)),
((0,0), (0,1), (0,3)))
# The x and y variables iterate over each space on the board.
# If we use + to represent the currently iterated space on the
# board, then this pattern: ((0,1), (1,0), (2,0))refers to identical
# elements being set up like this:
#
# +A
# B
# C
#
# That is, element A is offset from the + by (0,1), element B is offset
# by (1,0), and element C is offset by (2,0). In this case, element A can
# be swapped to the left to form a vertical three-in-a-row triplet.
#
# There are eight possible ways for the elements to be one move
# away from forming a triple, hence oneOffPattern has 8 patterns.
for x in range(GRIDWIDTH):
for y in range(GRIDHEIGHT):
for pat in oneOffPatterns:
# check each possible pattern of "match in next move" to
# see if a possible move can be made.
if (getElementAt(board, x+pat[0][0], y+pat[0][1]) == \
getElementAt(board, x+pat[1][0], y+pat[1][1]) == \
getElementAt(board, x+pat[2][0], y+pat[2][1]) != None) or \
(getElementAt(board, x+pat[0][1], y+pat[0][0]) == \
getElementAt(board, x+pat[1][1], y+pat[1][0]) == \
getElementAt(board, x+pat[2][1], y+pat[2][0]) != None):
return True # return True the first time you find a pattern
return False
def drawMovingElement(element, progress):
# Draw an element sliding in the direction that its 'direction' key
# indicates. The progress parameter is a number from 0 (just
# starting) to 100 (slide complete).
movex = 0
movey = 0
progress *= 0.01
if element['direction'] == UP:
movey = -int(progress * ELEMENTSIZE)
elif element['direction'] == DOWN:
movey = int(progress * ELEMENTSIZE)
elif element['direction'] == RIGHT:
movex = int(progress * ELEMENTSIZE)
elif element['direction'] == LEFT:
movex = -int(progress * ELEMENTSIZE)
basex = element['x']
basey = element['y']
if basey == ROWABOVEBOARD:
basey = -1
pixelx = XMARGIN + (basex * ELEMENTSIZE)
pixely = YMARGIN + (basey * ELEMENTSIZE)
r = pygame.Rect( (pixelx + movex, pixely + movey, ELEMENTSIZE, ELEMENTSIZE) )
PLAYSURF.blit(ELEMENTIMAGES[element['imageNum']], r)
def pullDownAllElements(board):
# pulls down elements on the board to the bottom to fill in any gaps
for x in range(GRIDWIDTH):
elementsInColumn = []
for y in range(GRIDHEIGHT):
if board[x][y] != EMPTY_SPACE:
elementsInColumn.append(board[x][y])
board[x] = ([EMPTY_SPACE] * (GRIDHEIGHT - len(elementsInColumn))) + elementsInColumn
def getElementAt(board, x, y):
if x < 0 or y < 0 or x >= GRIDWIDTH or y >= GRIDHEIGHT:
return None
else:
return board[x][y]
def getDropSlots(board):
# Creates a "drop slot" for each column and fills the slot with a
# number of elements that that column is lacking. This function assumes
# that the elements have been gravity dropped already.
boardCopy = copy.deepcopy(board)
pullDownAllElements(boardCopy)
dropSlots = []
for i in range(GRIDWIDTH):
dropSlots.append([])
# count the number of empty spaces in each column on the board
for x in range(GRIDWIDTH):
for y in range(GRIDHEIGHT-1, -1, -1): # start from bottom, going up
if boardCopy[x][y] == EMPTY_SPACE:
possibleElements = list(range(len(ELEMENTIMAGES)))
for offsetX, offsetY in ((0, -1), (1, 0), (0, 1), (-1, 0)):
# Narrow down the possible elements we should put in the
# blank space so we don't end up putting an two of
# the same elements next to each other when they drop.
neighborElement = getElementAt(boardCopy, x + offsetX, y + offsetY)
if neighborElement != None and neighborElement in possibleElements:
possibleElements.remove(neighborElement)
newElement = random.choice(possibleElements)
boardCopy[x][y] = newElement
dropSlots[x].append(newElement)
return dropSlots

This is just a partial answer to demonstrate what I suggested in the comments. I'm not sure how your gameBoard actually looks like, so you have to adjust the code as needed.
I use a board which is just filled with strings in this example (you could also use constants WATER = 1, FIRE = 2, etc. or an enum). The sounds are in a dictionary with the elements as the keys. If you have a match, figure out which element it is, then use it to get the associated sound out of the dict and play it.
ELEMENT_SOUNDS = {
'water': WATER_SOUND,
'fire': FIRE_SOUND,
}
board = [
['water', 'fire', 'water'],
['fire', 'water', 'fire'],
['water', 'water', 'water'],
]
if match:
# Figure out the kind of the matching element 'water' in this case.
element_kind = 'water'
ELEMENT_SOUNDS[element_kind].play()

Related

I'm having a problem with determining the intersection of two lines in this python code

I tried a math formula from analytic geometry but I didn't get the desired results.
As you can see in the code, the user draw two lines and a green circle(point of intersection) appear.
I have tried to fix the problem for hours but I failed. Sometimes the point doesn't appear or appear but in the wrong position.
Source of the formula I used in the code
Docs of pygame the library I used
#!/usr/bin/env python
from pygame import *
from time import sleep
init();
win = display.set_mode((500,500));
lines = []
cords = []
preview = False
xi = -100
yi = -100
def drawlines(flines):
for fcords in flines:
draw.line(win,(0,0,0),fcords[0],fcords[1],4)
while True:
win.fill((255,255,255))
for ev in event.get():
if ev.type == QUIT:
quit();
exit();
elif ev.type == MOUSEBUTTONDOWN:
if ev.button == 1:
preview = True
a = mouse.get_pos()
cords.append(mouse.get_pos())
elif ev.type == MOUSEBUTTONUP:
if ev.button == 1:
cords.append(mouse.get_pos())
lines.append((cords))
cords = []
preview = False
######################################## THIS BROKEN PART #################################
if len(lines) == 2:
#line 1
points_line1 = lines[0]
point1_line1 = points_line1[0]
point2_line1 = points_line1[1]
line1_vector = (point2_line1[0]-point1_line1[0],point2_line1[1]-point1_line1[1])
a_line1 = line1_vector[1]
b_line1 = -line1_vector[0]
c_line1 = -((a_line1*point1_line1[0]) + (b_line1*point1_line1[1]))
#line2
points_line2 = lines[1]
point1_line2 = points_line2[0]
point2_line2 = points_line2[1]
line2_vector = (point2_line2[0]-point1_line2[0],point2_line2[1]-point1_line2[1])
a_line2 = line2_vector[1]
b_line2 = -line2_vector[0]
c_line2 = -((a_line2*point1_line2[0]) + (b_line2*point1_line2[1]))
if (a_line2 != 0 and b_line2 != 0):
if (a_line1 / a_line2) != ( b_line1/b_line2):
#intersection between line1 and line2
yi = ((((a_line1*c_line2) / a_line2) + c_line1) / -b_line1)
xi = (((-b_line1*yi)-c_line1) / a_line1)
###########################################################################################
elif preview:
draw.line(win,(0,0,0),a,mouse.get_pos(),4)
drawlines(lines)
draw.circle(win,(0,200,0),(int(xi),int(yi)),10)
display.flip()
It's hard to dig through your code. Especially c_line1 = -((a_line1*point1_line1[0]) + (b_line1*point1_line1[1])) seems to be odd, because it calculates the Dot product of vector and a point.
Therefore I don't know exactly what's wrong with your code, but I know how to intersect lines. See Problem with calculating line intersections and Collision and Intersection - Line and line.
Write functions that can add, subtract, scale vectors and rotate vectors by 90°. Write a function which calculates the Dot product:
def add(a, b):
return a[0]+b[0], a[1]+b[1]
def sub(a, b):
return a[0]-b[0], a[1]-b[1]
def rot90(v):
return -v[1], v[0]
def mul(v, s):
return v[0]*s, v[1]*s
def dot(a, b):
return a[0]*b[0] + a[1]*b[1]
Calculate the vector from the beginning of the lines to the end of the lines and the normal vectors to the lines. The normal vector is obtained by rotating the line vector by 90°:
#line 1
point1_line1, point2_line1 = lines[0]
line1_vector = sub(point2_line1, point1_line1)
line1_norml = rot90(line1_vector)
#line2
point1_line2, point2_line2 = lines[1]
line2_vector = sub(point2_line2, point1_line2)
line2_norml = rot90(line2_vector)
Calculate the intersection of the line segments. Make sure the lines are not parallel and that the intersection point is on the line segments:
# vector from start point of line 2 to start point of line 1
l2p1_l1p1 = sub(point1_line1, point1_line2)
# intersection
d = dot(line2_vector, line1_norml)
if d != 0: # prallel lines
t = dot(l2p1_l1p1, line1_norml) / d
u = dot(l2p1_l1p1, line2_norml) / d
if 0 <= t <= 1 and 0 <= u <= 1: # intersection on line segments
xi, yi = add(point1_line2, mul(line2_vector, t))
Minimal example:
from pygame import *
init()
win = display.set_mode((500,500))
lines = []
cords = []
preview = False
xi, yi = -100, -100
def drawlines(flines):
for fcords in flines:
draw.line(win,(0,0,0),fcords[0],fcords[1],4)
def add(a, b):
return a[0]+b[0], a[1]+b[1]
def sub(a, b):
return a[0]-b[0], a[1]-b[1]
def rot90(v):
return -v[1], v[0]
def mul(v, s):
return v[0]*s, v[1]*s
def dot(a, b):
return a[0]*b[0] + a[1]*b[1]
run = True
while run:
for ev in event.get():
if ev.type == QUIT:
run = False
elif ev.type == MOUSEBUTTONDOWN:
if ev.button == 1:
preview = True
a = ev.pos
cords.append(a)
elif ev.type == MOUSEBUTTONUP:
if ev.button == 1:
cords.append(ev.pos)
lines.append((cords))
cords = []
preview = False
intersections = []
for i, line1 in enumerate(lines):
for line2 in lines[i+1:]:
#line 1
point1_line1, point2_line1 = line1
line1_vector = sub(point2_line1, point1_line1)
line1_norml = rot90(line1_vector)
#line2
point1_line2, point2_line2 = line2
line2_vector = sub(point2_line2, point1_line2)
line2_norml = rot90(line2_vector)
# vector from start point of line 2 to start point of line 1
l2p1_l1p1 = sub(point1_line1, point1_line2)
# intersection
d = dot(line2_vector, line1_norml)
if d != 0: # prallel lines
t = dot(l2p1_l1p1, line1_norml) / d
u = dot(l2p1_l1p1, line2_norml) / d
if 0 <= t <= 1 and 0 <= u <= 1: # intersection on line segments
xi, yi = add(point1_line2, mul(line2_vector, t))
intersections.append((xi, yi))
win.fill((255,255,255))
if len(cords) % 2 == 1:
draw.line(win,(0,0,0), a, mouse.get_pos(), 4)
drawlines(lines)
for p in intersections:
draw.circle(win, (0,200,0), (round(p[0]), round(p[1])), 10)
display.flip()
quit()
exit()

Trouble with minimax algorithm in tic tac toe game with adjustable game size

I am trying to create a tic tac toe game with an adjustable game size and a computer that uses the minimax algorithm. The game sizes can only be odd numbers, to make sure that diagonal wins are always possible. The game runs with no errors, but I know that the minimax algorithm isn't working 100% correct because I can still beat the computer. I've looked extensively over my code and cannot find where the algorithm is going wrong. Here is my code:
Main.py
import TicTacToe
import Minimax
if (__name__ == "__main__"):
t = TicTacToe.ttt(3)
m = Minimax.Minimax(3, t)
while (t.winner == None):
if (t.turn == 1):
playerInputI = int(input("Input row: "))
playerInputJ = int(input("Input column: "))
bestIndex = (playerInputI, playerInputJ)
else:
winner, bestIndex = m.minimax(t.grid, (-1, -1), 15, -1)
t.winner = None
t.findWinner(bestIndex, t.grid)
t.updateGameGrid(bestIndex)
print(t.grid)
print(t.grid)
Minimax.py
class Minimax:
def __init__(self, gs, t):
self.gridSize = gs
self.ttt = t
def minimax(self, state, currIndex, depth, turn):
if (currIndex[0] != -1 and currIndex[1] != -1):
winner = self.ttt.findWinner(currIndex, state)
if (winner == -1):
return winner - depth, currIndex
elif (winner == -1):
return winner + depth, currIndex
elif (winner == 0):
return 0, currIndex
if (depth==0 and winner==None):
return 0, currIndex
evalLimit = -turn * 1000
bestIndex = None
for i in range(self.gridSize):
for j in range(self.gridSize):
if (state[i][j] == 0):
state[i][j] = turn
eval, newIndex = self.minimax(state, (i, j), depth-1, -turn)
state[i][j] = 0
if (turn > 0 and eval > evalLimit):
bestIndex = newIndex
evalLimit = eval
elif (turn < 0 and eval < evalLimit):
bestIndex = newIndex
evalLimit = eval
return evalLimit, bestIndex
Tictactoe.py
from random import randint
class ttt:
def __init__(self, size):
self.gridSize = size
self.grid = self.createGrid()
# If using minimax algorithm, user is maximizer(1) and computer is minimizer(-1)
# If single player, then user is 1, computer is -1
# If multiplayer, user1 is 1, user2 = -1
self.turn = 1
self.winner = None
def createGrid(self):
grid = []
for i in range(self.gridSize):
grid.append([])
for j in range(self.gridSize):
grid[i].append(0)
# grid = [[-1, 1, 0], [0, -1, 0], [0, 0, 0]]
return grid
def updateGameGrid(self, index):
if (self.grid[index[0]][index[1]] != 0):
return
self.grid[index[0]][index[1]] = self.turn
winner = self.findWinner(index, self.grid)
self.turn = -self.turn
def randomIndex(self):
x = randint(0, self.gridSize-1)
y = randint(0, self.gridSize-1)
while (self.grid[x][y] != 0):
x = randint(0, self.gridSize-1)
y = randint(0, self.gridSize-1)
return (x, y)
def findWinner(self, index, grid):
# Row
found = True
for j in range(self.gridSize-1):
if (grid[index[0]][j] != grid[index[0]][j+1] or grid[index[0]][j] == 0):
found = False
break
if (found):
self.winner = self.turn
return self.turn
# Column
found = True
for i in range(self.gridSize-1):
if (grid[i][index[1]] != grid[i+1][index[1]] or grid[i][index[1]] == 0):
found = False
break
if (found):
self.winner = self.turn
return self.turn
# Top Left to Bottom Right Diagonal
if (index[0] == index[1]):
found = True
for i in range(self.gridSize-1):
if (grid[i][i] != grid[i+1][i+1] or grid[i][i] == 0):
found = False
break
if (found):
self.winner = self.turn
return self.turn
# Top Right to Bottom Left Diagonal
if (index[0] + index[1] == self.gridSize-1):
found = True
for i in range(self.gridSize-1):
if (grid[self.gridSize-i-1][i] != grid[self.gridSize-i-2][i+1] or grid[self.gridSize-i-1][i] == 0):
found = False
break
if (found):
self.winner = self.turn
return self.turn
tie = True
for i in range(self.gridSize):
for j in range(self.gridSize):
if (grid[i][j] == 0):
tie = False
if (tie):
self.winner = 0
return 0
return None
The grid is represented as a 2d array, with each element being either a -1 for O, 1 for X and 0 for nothing. The player is 1 and the computer is -1. Who's turn it is is represented as a -1 or 1, corresponding to O or X. If anyone is able to find where the error in my code is that would be a great.
I see two troubles :
a) the two following conditions are the same in minimax
if (winner == -1):
return winner - depth, currIndex
elif (winner == -1):
return winner + depth, currIndex
b) When you return bestIndex, you actually return the winning move, the one that completes a line or a row or a diagonal, at one of the leaves of the game tree. What you really want is the next move to play. Write instead bestIndex = (i, j) in the condition if eval > evalLimit
I didn't check for everything but that is a good start. Run your code at depth=1 or 2 with many printf inside the minimax function, and look at the different moves, with the corresponding score, if they look correct or not.

How do I call out a class object?

I just currently finished making the game 'snake' as a practice to learn how to program, as I am new to programming for about 3 months.
Although the game is completed and runs the way I intended, I want to try to simplify my code and reduce the amount of lines as much as possible, and possibly make the script tidier as the current majority of my codes are cluster in the while loop.
Until now I haven't touched upon class objects, and I want everything in the while loop to go into individual classes that get called out from the while loop to reduce the amount of lines in it.
off-topic: by reading through the script, how else can I improve it to be run more efficiently, including simplifying some code as I may have over-complicated it?
I looked up how class object is used from w3school and other programming tutorials, but I still don't fully understand it as it only shows examples in using print. I did play around and experimented with class object examples and attempted to call them without using print, but I lack the knowledge of how to use them properly.
from graphics import *
from threading import Timer
import keyboard, random, time
# configurations
width = 400
gridHeight = width
height = 470
timer = False
game = True
score = 0
bonus = 0
x = 70
y = 30
radius = 10
length = radius * 2
playerLength = 3
poisonLength = playerLength
i = 0
k = 0
pointRadius = 5
points = False
cherryPoints = False
key = "Right"
countDown = 0
# set coordinations
cX = 90
cY = 30
coordX = [10]
coordY = [10]
while coordX[len(coordX)-1] != width-10:
cX+=20
coordX.append(cX)
while coordY[len(coordY)-1] != 390:
cY+=20
coordY.append(cY)
randomX = random.choice(coordX)
randomY = random.choice(coordY)
cherryRandomX = random.choice(coordX)
cherryRandomY = random.choice(coordY)
poisonRandomX = random.choice(coordX)
poisonRandomY = random.choice(coordY)
# window set up
win = GraphWin("SNAKE", width, height, autoflush = False)
win.setBackground(color_rgb(15,15,15))
# grid
lineX = 20
while lineX < width:
gridX = Line(Point(lineX,0),Point(lineX,gridHeight))
gridX.setOutline(color_rgb(25,25,25))
gridX.draw(win)
lineX += 20
lineY = 20
while lineY <= gridHeight:
gridX = Line(Point(0,lineY),Point(width,lineY))
gridX.setOutline(color_rgb(25,25,25))
gridX.draw(win)
lineY += 20
# snake banner
UI = Rectangle(Point(0,400),Point(width,height))
UI.setFill(color_rgb(102,51,0))
UI.setOutline(color_rgb(102,51,0))
UI.draw(win)
snakeTitle = Text(Point(width/2,420),"SNAKE")
snakeTitle.setTextColor("green")
snakeTitle.setSize(20)
snakeTitle.draw(win)
scoreTitle = Text(Point(320,424),"SCORE")
scoreTitle.setTextColor("white")
scoreTitle.setSize(10)
scoreTitle.draw(win)
scoreUI = Text(Point(320,435),score)
scoreUI.setTextColor("white")
scoreUI.setSize(10)
scoreUI.draw(win)
# make player
player = {}
player[0] = Rectangle(Point(x-20-radius,y-radius), Point(x-20+radius, y+radius))
player[1] = Rectangle(Point(x-40-radius,y-radius), Point(x-40+radius, y+radius))
player[2] = Rectangle(Point(x-60-radius,y-radius), Point(x-60+radius, y+radius))
# make poison
poison = {}
def main():
global timer, scoreUI, score, bonus, playerLength, poisonLength, x, y, points, cherryPoints, randomX, randomY, cherryRandomX, cherryRandomY, poisonRandomX, poisonRandomY, key, countDown, k, game
while(game==True):
# score update
scoreUI.undraw()
scoreUI = Text(Point(320,435),score)
scoreUI.setTextColor("white")
scoreUI.setSize(10)
scoreUI.draw(win)
# generating new body blocks
if len(player) < playerLength:
i+=1
player[i] = player[i-1].clone()
# body following player
player[0].undraw()
for i in range(1,len(player)):
player[len(player)-i].undraw()
player[len(player)-i] = player[len(player)-i-1].clone()
player[len(player)-i].draw(win)
# update player's head coordinate
player[0] = Rectangle(Point(x-radius,y-radius), Point(x+radius,y+radius))
player[0].setFill("green")
player[0].setWidth(2)
player[0].draw(win)
# player movement
if keyboard.is_pressed("Up") and key != "Down":
key = "Up"
elif keyboard.is_pressed("Left") and key != "Right":
key = "Left"
elif keyboard.is_pressed("Down") and key != "Up":
key = "Down"
elif keyboard.is_pressed("Right") and key != "Left":
key = "Right"
if key == "Up":
y -= length
elif key == "Left":
x -= length
elif key == "Down":
y += length
elif key == "Right":
x += length
# point
if points == False: # generates new point when eaten
point = Rectangle(Point(randomX-pointRadius,randomY-pointRadius),Point(randomX+pointRadius,randomY+pointRadius))
point.setFill("white")
point.setWidth(2)
point.draw(win)
points = True
if player[0].getCenter().getX() == point.getCenter().getX() and player[0].getCenter().getY() == point.getCenter().getY(): # when player eats the point
point.undraw()
playerLength += 1
poisonLength += 1
score += 200+bonus
randomX = random.choice(coordX)
randomY = random.choice(coordY)
for i in range(len(player)):
if (point.getCenter().getX() == player[i].getCenter().getX() and point.getCenter().getY() == player[i].getCenter().getY()) or (cherryPoints == True and cherryPoint.getCenter().getX() == point.getCenter().getX() and cherryPoint.getCenter().getY() == point.getCenter().getY()): # regenerate x and y coordinate if they share the same coordinate as player and cherry
randomX = random.choice(coordX)
randomY = random.choice(coordY)
for i in range(len(poison)): # regenerate x and y coordinate if point shares the same coordinate to other array of poisons
if point.getCenter().getX() == poison[i].getCenter().getX() and point.getCenter().getY() == poison[i].getCenter().getY():
cherryRandomX = random.choice(coordX)
cherryRandomY = random.choice(coordY)
points = False
# cherry
if countDown == 150:
countDown = 0
if cherryPoints == False: # generates new cherry from countdown
cherryPoint = Rectangle(Point(cherryRandomX-pointRadius,cherryRandomY-pointRadius),Point(cherryRandomX+pointRadius,cherryRandomY+pointRadius))
cherryPoint.setFill(color_rgb(213,0,50))
cherryPoint.setWidth(2)
cherryPoint.draw(win)
cherryPoints = True
if cherryPoints == True:
for i in range(2, 6): # cherry blinks between countdown 40 to 100
if countDown == 20*i:
cherryPoint.undraw()
elif countDown == 10+(20*i):
cherryPoint.draw(win)
if countDown >= 100: # when countdown becomes 100, remove cherry and reset count down
cherryPoints = False
countDown = 0
cherryRandomX = random.choice(coordX)
cherryRandomY = random.choice(coordY)
if cherryPoints==True and player[0].getCenter().getX() == cherryPoint.getCenter().getX() and player[0].getCenter().getY() == cherryPoint.getCenter().getY(): # when player eats the cherry
cherryPoint.undraw()
score += 500
cherryRandomX = random.choice(coordX)
cherryRandomY = random.choice(coordY)
for i in range(len(player)):
if (cherryPoint.getCenter().getX() == player[i].getCenter().getX() and cherryPoint.getCenter().getY() == player[i].getCenter().getY()) or (cherryPoint.getCenter().getX() == point.getCenter().getX() and cherryPoint.getCenter().getY() == point.getCenter().getY()): # regenerate x and y coordinate if they share the same coordinate as player and point
cherryRandomX = random.choice(coordX)
cherryRandomY = random.choice(coordY)
for i in range(len(poison)): # regenerate x and y coordinate if cherry shares the same coordinate to other array of poisons
if cherryPoint.getCenter().getX() == poison[i].getCenter().getX() and cherryPoint.getCenter().getY() == poison[i].getCenter().getY():
cherryRandomX = random.choice(coordX)
cherryRandomY = random.choice(coordY)
cherryPoints = False
# poison
if poisonLength % 5 == 0: # generates a poison block each time the player size reaches the multiple of 5
poison[k] = Rectangle(Point(poisonRandomX-pointRadius,poisonRandomY-pointRadius),Point(poisonRandomX+pointRadius,poisonRandomY+pointRadius))
poison[k].setFill("green")
poison[k].setWidth(2)
poison[k].draw(win)
poisonRandomX = random.choice(coordX)
poisonRandomY = random.choice(coordY)
for i in range(len(player)):
if (poison[k].getCenter().getX() == player[i].getCenter().getX() and poison[k].getCenter().getY() == player[i].getCenter().getY()) or (poison[k].getCenter().getX() == point.getCenter().getX() and poison[k].getCenter().getY() == point.getCenter().getY()) or (cherryPoints==True and poison[k].getCenter().getX() == cherryPoint.getCenter().getX() and poison[k].getCenter().getY() == cherryPoint.getCenter().getY()): # regenerate x and y coordinate if they share the same coordinate as player and point and cherry
poisonRandomX = random.choice(coordX)
poisonRandomY = random.choice(coordY)
for i in range(len(poison)):
if poison[k].getCenter().getX() == poison[i].getCenter().getX() and poison[k].getCenter().getY() == poison[i].getCenter().getY(): # regenerate x and y coordinate if new poison shares the same coordinate to other array of poisons
poisonRandomX = random.choice(coordX)
poisonRandomY = random.choice(coordY)
bonus+=50
k+=1
poisonLength+=1
# game over requirements
for i in range(len(poison)): # if player touches poison
if player[0].getCenter().getX() == poison[i].getCenter().getX() and player[0].getCenter().getY() == poison[i].getCenter().getY():
game = False
for i in range(2, len(player)): # if player touches its own body or reach out of window
if (player[0].getCenter().getX() == player[i].getCenter().getX() and player[0].getCenter().getY() == player[i].getCenter().getY()) or x < 0 or x > width or y < 0 or y > gridHeight:
game = False
# FPS
update(10)
countDown += 1
# GAME OVER
gameOver = Text(Point(width/2,200), "GAME OVER")
gameOver.setTextColor("red")
gameOver.setSize(30)
gameOver.draw(win)
update()
time.sleep(2)
win.close()
main()
Ideally the result should replace each code in the while loop with individual classes outside of the function to reduce the amount of lines in the main() function and make the script easier to read.
Classes are essentially just bundles of code that contain various attributes and methods.
A Snake class might have a list of coordinates for each section of the body (the first is the head).
class Snake:
def __init__(self, x, y):
self.positions = [(x, y)]
def get_head(self):
return self.positions[0]
def move_forward(self):
self.positions.pop()
self.positions.insert(0, self.get_head()[1] + 1)
def move_backward(self):
self.positions.pop()
self.positions.insert(0, self.get_head()[1] - 1)
...
And so on. Classes, at this level, let you think of objects as concrete entities, distinct from each other but easily manipulated.

Unexpected 0 appearing in a list despite the code seemingly preventing a zero from being added to the list

I'm working on a tile based game in Python. For some reason, my function that finds the distance (in tile spaces) from a specified tile and the closest water tile, keeps returning 0 if the specified tile isn't the top left tile or adjacent to it. The function should normally only return 0 if the specified tile itself is a water tile. I tested the top left tile even when it was water and it still returns 0 in that case like its supposed to, and of course works properly with any other edits to the tiles around it. Like I said however, any tile that is tested which isn't adjacent to the top left tile never works no matter what the circumstance, it always returns 0.
The reason 0 shouldn't ever show up if I test a non-water tile is that the function appends the distances of a tile being tested relative to all water tiles to a list and then finds the minimum of that list and returns it. Since a non-water tile won't be in the list of water tiles, there should never be a case where is measures the distance between the specified tile and itself which would result in a 0. I did some self testing and confirmed that the water tiles list does indeed only contain the coordinates of water tiles, and the specified tile is of course specified in the function so there can't be error there.
Below is my code. It was originally written in codeskulptor and used the random module and simplegui module, but since this question doesn't involve either of those i stripped them away. I also cut out all the code which isn't relevant to this problem so don't worry about trying to point out other potential problems I might run into trying to do what I do. I also deeply apologize for the terrible naming conventions. I adapted the code to work in Pycharm so that more people might be able to diagnose it. Thanks so much.
Edit: It's come to my attention that I'm talking as though there is physical space being produced by this code, when there actually isn't in this version. I'll give you some reference points:
-tiles[0] would be at the top left
-tiles[1] would be to the right of tiles[0]
-tiles[80] would be right under tiles[0] because each row is 80 tiles
Furthermore, the function in question is "distance_from_water" near the top.
def distance(tile1, tile2):
# returns the distance of two tiles
return abs((tile2.x - tile1.x) + (tile2.y - tile1.y))
def distance_from_water(tile, index):
# cycles through every water tile and adds the distances
# of them relative to a specified tile to a list, then returns
# the lowest distance
water_tiles = []
water_tile_proximities = []
for element in range(0, len(index)):
if index[element].iswater == True:
water_tiles.append(element)
for element in water_tiles:
water_tile_proximities.append(distance(index[tile], index[element]))
lowest_distance = min(water_tile_proximities)
return lowest_distance
canvaswidth = 1280
canvasheight = 800
tile_size = 16
tile_slots = int((canvaswidth / tile_size) * (canvasheight / tile_size))
tile_slot_locations = [[(tile_size / 2), (tile_size / 2)]]
for element in range(0, (tile_slots - 1)):
# finds how many discrete locations for tiles there are based on the canvas size
if tile_slot_locations[element][0] > (canvaswidth - (tile_size / 2)):
tile_slot_locations[element][0] = (tile_size / 2)
tile_slot_locations[element][1] += tile_size
tile_slot_locations.append([((tile_slot_locations[element][0]) + tile_size), tile_slot_locations[element][1]])
tiles = []
colors = ["blue", "green", "darkgreen", "grey", "khaki", "brown"]
class Tile(object):
list_of_all_tiles = []
def __init__(self, location, color):
self.location = location
self.color = color
self.dimensions = ((location[0] + (tile_size / 2), location[1] + (tile_size / 2)),
(location[0] + (tile_size / 2), location[1] - (tile_size / 2)),
(location[0] - (tile_size / 2), location[1] - (tile_size / 2)),
(location[0] - (tile_size / 2), location[1] + (tile_size / 2)),
(location[0] + (tile_size / 2), location[1] + (tile_size / 2)))
self.x = (location[0] - (tile_size / 2)) / tile_size
self.y = (location[1] - (tile_size / 2)) / tile_size
Tile.list_of_all_tiles.append(self)
# determine the type
if color == "blue":
self.iswater = True
self.island = False
self.isforest = False
self.ismountain = False
self.issand = False
self.isinn = False
if color == "green":
self.iswater = False
self.island = True
self.isforest = False
self.ismountain = False
self.issand = False
self.isinn = False
if color == "darkgreen":
self.iswater = False
self.island = False
self.isforest = True
self.ismountain = False
self.issand = False
self.isinn = False
if color == "grey":
self.iswater = False
self.island = False
self.isforest = False
self.ismountain = True
self.issand = False
self.isinn = False
if color == "khaki":
self.iswater = False
self.island = False
self.isforest = False
self.ismountain = False
self.issand = True
self.isinn = False
if color == "brown":
self.iswater = False
self.island = False
self.isforest = False
self.ismountain = False
self.issand = False
self.isinn = True
for element in range(0, len(tile_slot_locations)):
# cycles through and assigns the Tile class
# using every tile slot location and saves in "tiles" list
tile = Tile(tile_slot_locations[element], colors[0])
tiles.append(tile)
tiles[120].island = True
tiles[120].iswater = False
tiles[1].island = True
tiles[1].iswater = False
tiles[80].island = True
tiles[80].iswater = False
tiles[81].island = True
tiles[81].iswater = False
tiles[3].island = True
tiles[3].iswater = False
print(distance_from_water(3, tiles))
Your distance() function is fundamentally broken - it's quite capable of returning a zero distance for tiles that are arbitrarily far apart, if their X difference is the negative of their Y difference. The expression should be:
abs(tile2.x - tile1.x) + abs(tile2.y - tile1.y)
instead of:
abs((tile2.x - tile1.x) + (tile2.y - tile1.y))
Unfortunately the distance is not your only problem. a very similar bug is
tile_slots = int((canvaswidth / tile_size) * (canvasheight / tile_size))
where you most probably want
tile_slots = int(canvaswidth / tile_size) * int(canvasheight / tile_size)
which in python3 you write
tile_slots = (canvaswidth // tile_size) * (canvasheight // tile_size)
some more hints
while i do not think your tile representation is ideal you can shorten the determine type block massively
# determine the type
self.iswater = color == "blue"
self.island = color == "green"
self.isforest = color == "darkgreen"
self.ismountain = color == "grey"
self.issand = color == "khaki"
self.isinn = color == "brown"

Can Someone Explain What Just Happened (Python) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I was working on my AI pathfinding(which you don't need to understand),
for some reason, my On[3] list was expanding when I did this in the shell:
tempList.append([On[1]-1])
(After the program messed up.) WHY?
The program didn't crash, but that isn't my question.
A screenshot(Ignore the extra prints, I was trying to narrow down the code that was causing it.)
On[1] was my Y coordinates.
The code in question is at # Find Path
(Under the bottom section.)
My code(Over 200 lines long. :/)
# Setup Python ----------------------------------------------- #
import pygame, sys, random, time, webbrowser, os
from datetime import datetime
# Version ---------------------------------------------------- #
Version = '1.0'
# Setup pygame/window ---------------------------------------- #
x = 100
y = 100
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
mainClock = pygame.time.Clock()
from pygame.locals import *
pygame.init()
pygame.display.set_caption('Pathfinding '+(Version)+'')
WINDOWWIDTH = 200
WINDOWHEIGHT = 200
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT),pygame.NOFRAME)
# Font ------------------------------------------------------- #
basicFont = pygame.font.SysFont(None, 20)
# Images ----------------------------------------------------- #
# Audio ------------------------------------------------------ #
# Colors ----------------------------------------------------- #
WHITE = (255,255,255)
BLACK = (0,0,0)
GRAY3 = (105,105,105)
GRAY = (195,195,195)
GRAY2 = (127,127,127)
RED = (255,0,0)
GREEN = (0,255,0)
BLUE = (0,0,255)
GOLD = (255,215,0)
PURPLE = (115,0,242)
# Variables -------------------------------------------------- #
Map = ['0000000000',
'0300000000',
'0000000000',
'0200000000',
'0000000000',
'0000000000',
'0000000000',
'0000000000',
'0000000000',
'0000000000']
Column = 0
Row = 0
Nodes = {}
for whatevs in Map:
for whatevs2 in Map[Row]:
Nodes[''+(str(Column))+','+(str(Row))+''] = [Column,Row,int(whatevs2)]
if whatevs2 == '3':
On = [Column,Row,[[Column,Row]]]
Column += 1
if Column == 10:
Column = 0
Row += 1
Open = {}
Closed = {}
# Rects ------------------------------------------------------ #
# Defenitions ------------------------------------------------ #
def Distance(Location,End):
if Location != []:
if int(Location[0]) < End[0]:
Dist = End[0] - int(Location[0])
else:
Dist = int(Location[0]) - End[0]
if int(Location[1]) < End[1]:
Dist2 = End[1] - int(Location[1])
else:
Dist2 = int(Location[1]) - End[1]
Dist += Dist2
if Location[2] == 1:
return 100000
elif Location[2] == 2:
return 0
else:
return Dist
else:
return 100000
# FPS -------------------------------------------------------- #
FPS = 80
TrueFPSCount = 0
TrueFPS = 0
fpsOn = False
PrevNow = 0
# Text -------------------------------------------------------- #
def drawText(text, font, color, surface, x, y):
textobj = font.render(text, 1, color)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
screen.blit(textobj, textrect)
# Loop ------------------------------------------------------- #
while True:
# Black Screen ------------------------------------------- #
screen.fill(BLACK)
# Show Nodes --------------------------------------------- #
for Node in Nodes:
Rect = pygame.Rect(Nodes[Node][0]*20,Nodes[Node][1]*20,20,20)
if Nodes[Node][2] == 0:
pygame.draw.rect(screen,WHITE,Rect)
elif Nodes[Node][2] == 1:
pygame.draw.rect(screen,BLUE,Rect)
elif Nodes[Node][2] == 2:
pygame.draw.rect(screen,GREEN,Rect)
End = [Nodes[Node][0],Nodes[Node][1]]
else:
pygame.draw.rect(screen,RED,Rect)
for Node in Closed:
Rect = pygame.Rect(Closed[Node][0]*20,Closed[Node][1]*20,20,20)
pygame.draw.rect(screen,(0,100,200),Rect)
Rect2 = pygame.Rect(On[0]*20,On[1]*20,20,20)
pygame.draw.rect(screen,(0,200,100),Rect2)
if [On[0],On[1]] == End:
print('Completed.')
print(On[2])
input()
time.sleep(3)
pygame.quit()
sys.exit()
# Find Path ---------------------------------------------- #
Top = []
Bottom = []
Right = []
Left = []
Closed[''+(str(On[0]))+','+(str(On[1]))+''] = [On[0],On[1]]
try:
Top.append(Nodes[''+(str(On[0]))+','+(str(On[1]-1))+''][0])
Top.append(Nodes[''+(str(On[0]))+','+(str(On[1]-1))+''][1])
Top.append(Nodes[''+(str(On[0]))+','+(str(On[1]-1))+''][2])
tempList = []
tempList = On[2]
print(On)
tempList.append([On[0],On[1]-1])
print(On)
Top.append(tempList)
for item in Closed:
if Top != []:
if item == ''+(str(Top[0]))+','+(str(Top[1]))+'':
Top = []
except NameError:
pass
except KeyError:
pass
try:
Bottom.append(Nodes[''+(str(On[0]))+','+(str(On[1]+1))+''][0])
Bottom.append(Nodes[''+(str(On[0]))+','+(str(On[1]+1))+''][1])
Bottom.append(Nodes[''+(str(On[0]))+','+(str(On[1]+1))+''][2])
tempList = []
tempList = On[2]
print('?')
print(On)
tempList.append([On[0],On[1]+1])
print(On)
print()
Bottom.append(tempList)
print('On')
print(On)
for item in Closed:
if Bottom != []:
if item == ''+(str(Bottom[0]))+','+(str(Bottom[1]))+'':
Bottom = []
except NameError:
pass
except KeyError:
pass
try:
Right.append(Nodes[''+(str(On[0]+1))+','+(str(On[1]))+''][0])
Right.append(Nodes[''+(str(On[0]+1))+','+(str(On[1]))+''][1])
Right.append(Nodes[''+(str(On[0]+1))+','+(str(On[1]))+''][2])
tempList = []
tempList = On[2]
tempList.append([On[0]+1,On[1]])
Right.append(tempList)
for item in Closed:
if Right != []:
if item == ''+(str(Right[0]))+','+(str(Right[1]))+'':
Right = []
except NameError:
pass
except KeyError:
pass
try:
Left.append(Nodes[''+(str(On[0]-1))+','+(str(On[1]))+''][0])
Left.append(Nodes[''+(str(On[0]-1))+','+(str(On[1]))+''][1])
Left.append(Nodes[''+(str(On[0]-1))+','+(str(On[1]))+''][2])
tempList = []
tempList = On[2]
tempList.append([On[0]-1,On[1]])
Left.append(tempList)
for item in Closed:
if Left != []:
if item == ''+(str(Left[0]))+','+(str(Left[1]))+'':
Left = []
except NameError:
pass
except KeyError:
pass
if Top != []:
Open[''+(str(Top[0]))+','+(str(Top[1]))+''] = [Distance(Top,End),Top[0],Top[1],Top[3]]
if Bottom != []:
print(':D')
print(On)
Open[''+(str(Bottom[0]))+','+(str(Bottom[1]))+''] = [Distance(Bottom,End),Bottom[0],Bottom[1],Bottom[3]]
if Right != []:
Open[''+(str(Right[0]))+','+(str(Right[1]))+''] = [Distance(Right,End),Right[0],Right[1],Right[3]]
if Left != []:
Open[''+(str(Left[0]))+','+(str(Left[1]))+''] = [Distance(Left,End),Left[0],Left[1],Left[3]]
Lowest = [0,0]
LowestNum = 100000
try:
del Open[''+(str(On[0]))+','+(str(On[1]))+'']
except KeyError:
pass
for Place in Open:
if Open[Place][0] < LowestNum:
LowestNum = Open[Place][0]
Lowest = [Open[Place][1],Open[Place][2],Open[Place][3]]
On = Lowest
# FPS ---------------------------------------------------- #
NewSec = False
TrueFPSCount += 1
now = datetime.now()
now = now.second
if PrevNow != now:
PrevNow = now
NewSec = True
TrueFPS = TrueFPSCount
TrueFPSCount = 0
TrueFPS = str(TrueFPS)
# Buttons ------------------------------------------------ #
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == ord('x'):
if fpsOn == True:
fpsOn = False
elif fpsOn == False:
fpsOn = True
# Update ------------------------------------------------- #
if fpsOn == True:
drawText('FPS:'+(TrueFPS)+'', basicFont, WHITE, screen, 500,12)
pygame.display.update()
mainClock.tick(10)
I don't use classes or sprites. :P
Thanks for taking a look!
Because your On[-1] and tempList are the same object. And this is how it works with same objects:
>>> x=[1,2,3]
>>> y=x
>>> y.append(10)
>>> x
[1, 2, 3, 10]
>>> y
[1, 2, 3, 10]
>>>

Categories