Uncaught TypeError: Cannot read properties of undefined (reading 'add') - Wont add - typeerror

Can anyone help me, I have been working on a project and cannot for the life of me work out why some of these work, and the lines such as 'queens' doesnt add or remove the classes...
I am attempting to make it so it verifies each line, checks if the current card is (for example) the ace of hearts, 'aceh', if so it add the current class, after this it then removes it once it is added to the prevcard var.
At the same time of adding it to the prevcard var, it adds another random card, lets say 'kingc' to the current card var... However it doesn't add the class to some of the vars and doesn't remove from others.
//CARD VERIFYING
if (currentcard == 'aceh'){aceh.classList.add('current')};
if (prevcard == 'aceh'){aceh.classList.add('prev')};
if (currentcard == 'aced'){aced.classList.add('current')};
if (prevcard == 'aced'){aced.classList.add('prev')};
if (currentcard == 'acec'){acec.classList.add('current')};
if (prevcard == 'acec'){acec.classList.add('prev')};
if (currentcard == 'aces'){aces.classList.add('current')};
if (prevcard == 'aces'){aces.classList.add('prev')};
if (currentcard == 'twoh'){twoh.classList.add('current')};
if (prevcard == 'twoh'){twoh.classList.add('prev')};
if (currentcard == 'twod'){twod.classList.add('current')};
if (prevcard == 'twod'){twod.classList.add('prev')};
if (currentcard == 'twoc'){twoc.classList.add('current')};
if (prevcard == 'twoc'){twoc.classList.add('prev')};
if (currentcard == 'twos'){twos.classList.add('current')};
if (prevcard == 'twos'){twos.classList.add('prev')};
if (currentcard == 'threeh'){threeh.classList.add('current')};
if (prevcard == 'threeh'){threeh.classList.add('prev')};
if (currentcard == 'threed'){threed.classList.add('current')};
if (prevcard == 'threed'){threed.classList.add('prev')};
if (currentcard == 'threec'){threec.classList.add('current')};
if (prevcard == 'threec'){threec.classList.add('prev')};
if (currentcard == 'threes'){threes.classList.add('current')};
if (prevcard == 'threes'){threes.classList.add('prev')};
if (currentcard == 'fourh'){fourh.classList.add('current')};
if (prevcard == 'fourh'){fourh.classList.add('prev')};
if (currentcard == 'fourd'){four.classList.add('current')};
if (prevcard == 'fourd'){four.classList.add('prev')};
if (currentcard == 'fourc'){four.classList.add('current')};
if (prevcard == 'fourc'){four.classList.add('prev')};
if (currentcard == 'fours'){four.classList.add('current')};
if (prevcard == 'fours'){fours.classList.add('prev')};
if (currentcard == 'fiveh'){fiveh.classList.add('current')};
if (prevcard == 'fiveh'){fiveh.classList.add('prev')};
if (currentcard == 'fived'){fived.classList.add('current')};
if (prevcard == 'fived'){fived.classList.add('prev')};
if (currentcard == 'fivec'){fivec.classList.add('current')};
if (prevcard == 'fivec'){fivec.classList.add('prev')};
if (currentcard == 'fives'){fives.classList.add('current')};
if (prevcard == 'fives'){fives.classList.add('prev')};
if (currentcard == 'sixh'){sixh.classList.add('current')};
if (prevcard == 'sixh'){sixh.classList.add('prev')};
if (currentcard == 'sixd'){sixd.classList.add('current')};
if (prevcard == 'sixd'){sixd.classList.add('prev')};
if (currentcard == 'sixc'){sixc.classList.add('current')};
if (prevcard == 'sixc'){sixc.classList.add('prev')};
if (currentcard == 'sixs'){sixs.classList.add('current')};
if (prevcard == 'sixs'){sixs.classList.add('prev')};
if (currentcard == 'sevenh'){sevenh.classList.add('current')};
if (prevcard == 'sevenh'){sevenh.classList.add('prev')};
if (currentcard == 'sevend'){sevend.classList.add('current')};
if (prevcard == 'sevend'){sevend.classList.add('prev')};
if (currentcard == 'sevenc'){sevenc.classList.add('current')};
if (prevcard == 'sevenc'){sevenc.classList.add('prev')};
if (currentcard == 'sevens'){sevens.classList.add('current')};
if (prevcard == 'sevens'){sevens.classList.add('prev')};
if (currentcard == 'eighth'){eighth.classList.add('current')};
if (prevcard == 'eighth'){eighth.classList.add('prev')};
if (currentcard == 'eightd'){eightd.classList.add('current')};
if (prevcard == 'eightd'){eightd.classList.add('prev')};
if (currentcard == 'eightc'){eightc.classList.add('current')};
if (prevcard == 'eightc'){eightc.classList.add('prev')};
if (currentcard == 'eights'){eights.classList.add('current')};
if (prevcard == 'eights'){eights.classList.add('prev')};
if (currentcard == 'nineh'){nineh.classList.add('current')};
if (prevcard == 'nineh'){nineh.classList.add('prev')};
if (currentcard == 'nined'){nined.classList.add('current')};
if (prevcard == 'nined'){nined.classList.add('prev')};
if (currentcard == 'ninec'){ninec.classList.add('current')};
if (prevcard == 'ninec'){ninec.classList.add('prev')};
if (currentcard == 'nines'){nines.classList.add('current')};
if (prevcard == 'nines'){nines.classList.add('prev')};
if (currentcard == 'tenh'){tenh.classList.add('current')};
if (prevcard == 'tenh'){tenh.classList.add('prev')};
if (currentcard == 'tend'){tend.classList.add('current')};
if (prevcard == 'tend'){tend.classList.add('prev')};
if (currentcard == 'tenc'){tenc.classList.add('current')};
if (prevcard == 'tenc'){tenc.classList.add('prev')};
if (currentcard == 'tens'){tens.classList.add('current')};
if (prevcard == 'tens'){tens.classList.add('prev')};
if (currentcard == 'jackh'){jackh.classList.add('current')};
if (prevcard == 'jackh'){jackh.classList.add('prev')};
if (currentcard == 'jackd'){jackd.classList.add('current')};
if (prevcard == 'jackd'){jackd.classList.add('prev')};
if (currentcard == 'jackc'){jackc.classList.add('current')};
if (prevcard == 'jackc'){jackc.classList.add('prev')};
if (currentcard == 'jacks'){jacks.classList.add('current')};
if (prevcard == 'jacks'){jacks.classList.add('prev')};
if (currentcard == 'queenh'){queenh.classList.add('current')};
if (prevcard == 'queenh'){queenh.classList.add('prev')};
if (currentcard == 'queend'){queend.classList.add('current')};
if (prevcard == 'queend'){queend.classList.add('prev')};
if (currentcard == 'queenc'){queenc.classList.add('current')};
if (prevcard == 'queenc'){queenc.classList.add('prev')};
if (currentcard == 'queens'){queens.classList.add('current')};
if (prevcard == 'queens'){queens.classList.add('prev')};
if (currentcard == 'kingh'){kingh.classList.add('current')};
if (prevcard == 'kingh'){kingh.classList.add('prev')};
if (currentcard == 'kingd'){kingd.classList.add('current')};
if (prevcard == 'kingd'){kingd.classList.add('prev')};
if (currentcard == 'kingc'){kingc.classList.add('current')};
if (prevcard == 'kingc'){kingc.classList.add('prev')};
if (currentcard == 'kings'){kings.classList.add('current')};
if (prevcard == 'kings'){kings.classList.add('prev')};

Related

Anylogic, parameter variation error during iteration

I am trying to run a parameter variation experiment with 3 iterations and 100 replications in Anylogic. I use the same 100 seeds {10, 20, 30, ..., 1000} for every replication using
root.getDefaultRandomGenerator().setSeed((long) listOfSeeds.get(getCurrentReplication()));
in "before simulation run" java action field in parameter variation properties. I am varying one parameter. For retrieving data after each run I inserted following code in "after simulation run" section
replication = getCurrentReplication();
double totalMins = 0;
double totalLoading = 0;
double totalUnloading = 0;
int i = 0;
while(i < root.pickTime.size()){
double x = root.pickTime.getX(i);
if(i+7 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2) && x == root.pickTime.getX(i+3) && x == root.pickTime.getX(i+4) && x == root.pickTime.getX(i+5) && x == root.pickTime.getX(i+6) && x == root.pickTime.getX(i+7)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2) + root.pickTime.getY(i+3) +root.pickTime.getY(i+4) +root.pickTime.getY(i+5) +root.pickTime.getY(i+6) +root.pickTime.getY(i+7);
pickTimePV.add(x, y);
i += 8;
continue;}
if(i+6 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2) && x == root.pickTime.getX(i+3) && x == root.pickTime.getX(i+4) && x == root.pickTime.getX(i+5) && x == root.pickTime.getX(i+6)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2) + root.pickTime.getY(i+3) +root.pickTime.getY(i+4) +root.pickTime.getY(i+5) +root.pickTime.getY(i+6);
pickTimePV.add(x, y);
i += 7;
continue;}
if(i+5 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2) && x == root.pickTime.getX(i+3) && x == root.pickTime.getX(i+4) && x == root.pickTime.getX(i+5)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2) + root.pickTime.getY(i+3) +root.pickTime.getY(i+4) +root.pickTime.getY(i+5);
pickTimePV.add(x, y);
i += 6;
continue;}
if(i+4 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2) && x == root.pickTime.getX(i+3) && x == root.pickTime.getX(i+4)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2) + root.pickTime.getY(i+3) +root.pickTime.getY(i+4);
pickTimePV.add(x, y);
i += 5;
continue;}
if(i+3 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2) && x == root.pickTime.getX(i+3)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2) + root.pickTime.getY(i+3);
pickTimePV.add(x, y);
i += 4;
continue;}
if(i+2 < root.pickTime.size() && x == root.pickTime.getX(i+1) && x == root.pickTime.getX(i+2)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1) + root.pickTime.getY(i+2);
pickTimePV.add(x, y);
i += 3;
continue;}
if(i+1 < root.pickTime.size() && x == root.pickTime.getX(i+1)){
double y = root.pickTime.getY(i) + root.pickTime.getY(i+1);
pickTimePV.add(x, y);
i += 2;
continue;}
else{
pickTimePV.add(x, root.pickTime.getY(i));
i++;}}
excelFile.writeDataSet(pickTimePV, 1, 1, 1 + iteration);
pickTimePV.reset();
iteration +=3;
kommiUtilization.add(getCurrentIteration(), root.kommiPool.utilization());
aushilfeUtilization.add(getCurrentIteration(), root.aushilfePop.get(0).timeInState(ResourceUsageState.USAGE_BUSY, MINUTE) / (root.kommissionierer.get(0).timeInState(ResourceUsageState.USAGE_BUSY, MINUTE)/root.kommiPool.utilization()));
ordersInQueue.add(getCurrentIteration(), root.orderQueue.size());
for(int j = 0; j < root.loadingTime.size(); j++){
totalLoading += root.loadingTime.getY(j);}
loadingTimePV.add(getCurrentIteration(), totalLoading/root.totalLoadTrucks);
for(int k = 0; k < root.unloadingTime.size(); k++){
totalUnloading += root.unloadingTime.getY(k);}
unloadingTimePV.add(getCurrentIteration(), totalUnloading/root.totalUnloadTrucks);
for(int t = 0; t < root.pickTime.size(); t++){
totalMins += root.pickTime.getY(t);}
totalPickTime.add(getCurrentIteration(), totalMins);
totalLoadTrucksPV.add(getCurrentIteration(), root.totalLoadTrucks);
totalUnloadTrucksPV.add(getCurrentIteration(), root.totalUnloadTrucks);
After replication No. 61 (183 simulation runs) the system throws the error:
Error in the model during iteration 3
java.lang.RuntimeException: Error in the model during iteration 3
at com.anylogic.engine.ExperimentParamVariation$b.i(Unknown Source)
at com.anylogic.engine.n$b.run(Unknown Source)
Before the error occurs, the last simulation run finishes succesfully (visibile using traceln()). I checked all possible parameter configurations and seed numbers where the error could be thrown with simulation experiments, but everything works fine for simulation experiments. So I guess the error has to do something with the parameter variation experiment setup.
Do you have any ideas how to fix the problem?
Thanks in advance.

local Variable 'beta' referenced before assignment

My program uses the minimax-algorithm to choose the most otimal move against the human player in tic-tac-toe. (this is working)
After that I tried to implement the alpha-beta-pruning to optimize the time the algorithm needs in order to get the optimal move. (this doesnt work)
I know why the Error is raised, but I dont get why the Error is raised by 'alpha' and 'beta' and not by 'board' or 'minX' or 'minY' too.
I think the Variables I declared should all be global variables, should they?
Error:
UnboundLocalError: local Variable 'beta' referenced before assignment
#Global Variables
values = [0,1,2]
minX = 0
minY = 0
alpha = -999
beta = 999
global alpha
global beta
board = [["-" for x in range(3)] for y in range(3)]
#ausgabe
def ausgabe():
for zeile in range(3):
for spalte in range(3):
print("|"+str(board[zeile][spalte]),end="")
print("|")
#auswertung
#liefert 0, wenn O gewonnen hat
#liefert 2, wenn X gewonnen hat
#liefert 1, wenn es unentschieden ausgeht
#liefert -1 wenn noch nicht klar
def auswertung():
for i in ("X", "O"):
for x in range(3):
if board[x][0] == i and board[x][1] == i and board[x][2] == i:
return i
for y in range(3):
if board[0][y] == i and board[1][y] == i and board[2][y] == i:
return i
if board[0][0] == i and board[1][1] == i and board[2][2] == i:
return i
if board[0][2] == i and board[1][1] == i and board[2][0] == i:
return i
for zeile in range(3):
for spalte in range(3):
if board[zeile][spalte] == "-":
return -1
return 1
#max
def max():
temp = auswertung()
if temp != -1:
if temp == "X":
return 2
elif temp == "O":
return 0
else:
return temp
maximalwert = -999
for x in range(3):
for y in range(3):
if board[x][y] == "-":
board[x][y] = "X"
temp = alpha
alpha = beta
beta = temp
if alpha < beta:
break
wert = min()
board[x][y] = "-"
if wert > maximalwert:
maximalwert = wert
return maximalwert
#min
def min():
temp = auswertung()
if temp != -1:
if temp == "X":
return 2
elif temp == "O":
return 0
else:
return temp
minimalwert = 999
for x in range(3):
for y in range(3):
if board[x][y] == "-":
board[x][y] = "O"
temp = beta
beta = alpha
alpha = temp
if alpha > beta:
break
wert = max()
board[x][y] = "-"
if wert < minimalwert:
minimalwert = wert
return minimalwert
#wo befindet sich das min
def minWo():
temp = auswertung()
if temp != -1:
if temp == "X":
return 2
elif temp == "O":
return 0
else:
return temp
global minX
global minY
minimalwert = 999
for x in range(3):
for y in range(3):
if board[x][y] == "-":
board[x][y] = "O"
temp = beta
beta = alpha
alpha = temp
if alpha > beta:
break
wert = max()
board[x][y] = "-"
if wert < minimalwert:
minX = x
minY = y
minimalwert = wert
return minimalwert
def user_input():
while True:
try:
number1 = int(input("number1: "))
number2 = int(input("number2: "))
if number1 in values and number2 in values:
if board[number1][number2] == "-":
return number1, number2
else:
print("Invalid Input!\ttry again")
except ValueError:
print("Invalid Input!\ttry again")
def game_still_going():
winner = auswertung()
if winner == "X" or winner == "O":
print("Player "+winner+" won the game")
return False
elif winner == 1:
print("It's a draw")
return False
return True
def play_game():
while game_still_going():
number1,number2 = user_input()
board[number1][number2] = "X"
minWo()
board[minX][minY] = "O"
ausgabe()
play_game()
You should set alpha respective beta to -999 and 999 instead of maximalwert, that should work. Look at the pseudo code for minimax.

Conditional Replacement in Keras Backend

Target:
Hey i want to build a custom metrics method, where different specific classes are allowed do be confused with specific (because their idea in some conditions in nearly the same).
How it could be reached:
I got 25 classes, where the following confusions should be allowed
(1 can be 2,3,4) ; (2 can be 3,4) ; (3 can be 4) ;(15 can be 16,17,18);(16 can be 17) ; (17 can be 18) ; (6 can be 7,8 ) ; (7 can be 8 ); (19 can be 20,21 ) ; (20 can be 21 );(10 can be 11 );(13 can be 14 );(22 can be 23 ); (24 can be 14 );
quite much huh... therefore i put it into a mask:
mask = mask =(y_true!=y_pred)&(((y_true==1)&((y_pred == 2)| (y_pred == 3)| (y_pred == 4))) | ((y_true==2) & ((y_pred == 3)|(y_pred == 4))) | ((y_true==3)&((y_pred == 4)))| ((y_true==15)&((y_pred == 16) | (y_pred == 17)| (y_pred == 18))) | ((y_true==16) & ((y_pred == 17)|(y_pred == 18))) | ((y_true==17)&((y_pred == 18))) | ((y_true==6) & ((y_pred == 7)|(y_pred == 8)))|((y_true==7)&((y_pred == 8)))| ((y_true==19) & ((y_pred == 20)|(y_pred == 21)))|((y_true==20)&((y_pred == 21)))|((y_true==10)&((y_pred == 11)))|((y_true==13)&((y_pred == 14)))|((y_true==22)&((y_pred == 23)))|((y_true==24)&((y_pred == 14))) )
I was hoping to find a function (equivalent to np.where) to conditional replace the values in the "y_pred" which fit the condition, by the "y_true" tensors values at the same index.
I thought tf.where_v2() could work (sadly, i wasnt able to check if it works because tf.equal() and tf.reduce_all() only have limited abilities to work in a metrics function (or am I wrong?)
the following creates no debug errors
def profit_metrics(y_true, y_pred):
mask =(y_true!=y_pred)&(((y_true==1)&((y_pred == 2)| (y_pred == 3)| (y_pred == 4))) | ((y_true==2) & ((y_pred == 3)|(y_pred == 4))) | ((y_true==3)&((y_pred == 4)))| ((y_true==15)&((y_pred == 16) | (y_pred == 17)| (y_pred == 18))) | ((y_true==16) & ((y_pred == 17)|(y_pred == 18))) | ((y_true==17)&((y_pred == 18))) | ((y_true==6) & ((y_pred == 7)|(y_pred == 8)))|((y_true==7)&((y_pred == 8)))| ((y_true==19) & ((y_pred == 20)|(y_pred == 21)))|((y_true==20)&((y_pred == 21)))|((y_true==10)&((y_pred == 11)))|((y_true==13)&((y_pred == 14)))|((y_true==22)&((y_pred == 23)))|((y_true==24)&((y_pred == 14))) )
new_pred = tf.where_v2(mask,y_true,y_pred)
return sparse_categorical_accuracy(y_true, new_pred)
(...)
model.compile(...,metrics=['accuracy',profit_metrics,])
but the thing is... it returns exactly the same accuracy value as the standard accuracy method!
I tried to check if both tensors are still the same, but sadly all i tried didnt work out, maybe because i need to use only Keras backend functions?
def profit_metrics(y_true, y_pred):
mask =(y_true!=y_pred)&(((y_true==1)&((y_pred == 2)| (y_pred == 3)| (y_pred == 4))) | ((y_true==2) & ((y_pred == 3)|(y_pred == 4))) | ((y_true==3)&((y_pred == 4)))| ((y_true==15)&((y_pred == 16) | (y_pred == 17)| (y_pred == 18))) | ((y_true==16) & ((y_pred == 17)|(y_pred == 18))) | ((y_true==17)&((y_pred == 18))) | ((y_true==6) & ((y_pred == 7)|(y_pred == 8)))|((y_true==7)&((y_pred == 8)))| ((y_true==19) & ((y_pred == 20)|(y_pred == 21)))|((y_true==20)&((y_pred == 21)))|((y_true==10)&((y_pred == 11)))|((y_true==13)&((y_pred == 14)))|((y_true==22)&((y_pred == 23)))|((y_true==24)&((y_pred == 14))) )
new_pred = tf.where_v2(mask,y_true,y_pred)
bool_tensor = tf.equal(new_pred,y_pred)
same = tf.math.reduce_all(bool_tensor)
session = tf.keras.backend.get_session(op_input_list=())
K.set_session(same)
if session.run(same)==True:
return sparse_categorical_accuracy(y_true, new_pred)#K.mean(K.equal(K.argmax(y_true, axis=-1), K.argmax(new_pred, axis=-1)))
else:
return tf.zeros([16, 25], tf.int32)
(both tensors have shape [16, 25])
something similar to where_v2 from keras.backend
or at least a method to show if my code works somehow(?) would be great
But in the end:
All i want is to make the conditional confusion for the mentioned conditions possible in my custom accuracy metrics and i would be extremely glad for anyones helpful idea (thank you) :'/

No module named 'Models', python 3.5.2

Got this Arkanoid clone code from internet however even after installing pygame binary still doesn' t want to work ("ImportError: No module named 'Models'"). I am using Spyder Python 3.5 and i cloned all repo from there https://github.com/Killavus/PyGame-Arkanoid
import pygame
import sys
import math
import random
from pygame.locals import *
import GameModule
from GameModule.Models import *
from GameModule.Constants import *
class Game:
def playSound( self, fileName ):
sound = pygame.mixer.Sound(fileName)
sound.play()
def __init__(self):
pygame.init()
pygame.mixer.init(11025)
self.actualState = states["START"]
self.display = pygame.display.set_mode( gfx["screen"] )
pygame.display.set_caption( "Arkanoid v" + version )
# 4 basic objects, which define the game
## Painter - drawing actual situation on screen
## Mapper - loading the map
## Status - holding stance of the game: points, lifes, actual powerups etc
## Objects - holding actual game's objects: balls, bricks, pad etc
self.objects = GameModule.Objects()
self.mapper = GameModule.Mapper( self.objects )
self.status = Status()
self.painter = GameModule.Painter( self.display, self.objects, self.status )
self.lastState = None
self.fps = pygame.time.Clock()
self.newLevel()
def loop(self):
self.fps.tick( gfx["framerate"] )
for event in pygame.event.get():
self.handleGlobalEvents( event )
self.handleEvents( event )
self.handleKeyboard()
self.updateState()
if self.actualState != states["GAMEOVER"]:
self.painter.draw()
else:
self.display.fill( (0,0,0) )
bigFont = pygame.font.SysFont( "sans-serif", 32 )
gameOver = bigFont.render( "GAME OVER!", True, (255,255,255) )
smallFont = pygame.font.SysFont( "sans-serif", 18 )
points = smallFont.render( "Punktow: " + str(self.status.points), True, (255,255,255) )
self.display.blit( gameOver, (gfx["screen"][0]/2 - gameOver.get_size()[0]/2, gfx["screen"][1]/2 - gameOver.get_size()[1]/2) )
self.display.blit( points, (gfx["screen"][0]/2 - points.get_size()[0]/2, gfx["screen"][1]/2 - gameOver.get_size()[1]/2 - 40) )
pygame.display.update()
def handleEvents( self, event ):
if self.actualState != states["PAUSE"]:
if event.type == KEYDOWN:
if event.key == K_p:
self.lastState = self.actualState
self.actualState = states["PAUSE"]
self.painter.paused = True
else:
if event.type == KEYDOWN:
if event.key == K_p:
self.actualState = self.lastState
self.painter.paused = False
if self.actualState == states["START"]:
if event.type == KEYDOWN:
if event.key == K_SPACE:
self.actualState = states["PROGRESS"]
(self.objects.balls())[0].speedChange( int( math.copysign( 5., float(self.objects.pad().lastMove) ) ), -5 )
def handleGlobalEvents( self, event ):
if event.type == QUIT:
sys.exit()
def handleKeyboard(self):
pressed = pygame.key.get_pressed()
if self.actualState == states["PROGRESS"] or self.actualState == states["START"]:
if pressed[K_LEFT]:
if self.objects.pad().move(-10) and self.actualState == states["START"]:
(self.objects.balls())[0].move(-10,0)
if pressed[K_RIGHT]:
if self.objects.pad().move(10) and self.actualState == states["START"]:
(self.objects.balls())[0].move(10,0)
def updateState(self):
if self.actualState == states["PAUSE"]:
return
if self.actualState == states["PROGRESS"]:
self.objects.updatePowerups()
for ball in self.objects.balls():
speed = ball.speed()
ball.move( speed[0], speed[1] )
if ball.collideWithWall():
pos = ball.pos()
if pos[0] == 0 or pos[0] >= gfx["screen"][0] - ball.image.get_size()[0] - 1:
ball.xInvert()
if pos[1] == 0:
ball.yInvert()
if pos[1] >= gfx["screen"][1] - ball.image.get_size()[1] - 1:
self.status.lives -= 1
self.actualState = states["START"]
self.playSound( "Resources/snd/chord.wav" )
while len(self.objects.balls()) != 1:
self.objects.removeBall((self.objects.balls())[0])
for powerup in self.objects.powerups():
self.objects.deletePowerup(powerup)
self.objects.balls()[0].reset()
self.objects.pad().reset()
for obj in self.objects.grid():
if ball.collision(obj):
self.status.points += 100
if obj.realx <= ball.position[0] and obj.realx + gfx["grid"][0] >= ball.position[0]:
ball.yInvert()
if obj.realy <= ball.position[1] and obj.realy + gfx["grid"][1] >= ball.position[1]:
ball.xInvert()
self.handleBrickCollision(obj)
if ball.collision(self.objects.pad()):
ball.yInvert()
if self.objects.pad().position[1] <= ball.position[1] and self.objects.pad().position[1] + gfx["grid"][1] >= ball.position[1]:
ball.xInvert()
for powerup in self.objects.powerups():
pad = self.objects.pad()
if powerup.collision(pad):
self.objects.deletePowerup(powerup)
self.generatePowerup()
if powerup.y == gfx["screen"][1] - 1 - powerup.image.get_size()[1]:
self.objects.deletePowerup(powerup)
if self.status.lives == 0:
self.actualState = states["GAMEOVER"]
if self.objects.grid() == []:
self.status.level += 1
if len(levels) == self.status.level:
self.actualStatus = states["GAMEOVER"]
else:
self.newLevel()
for modifier in self.status.modifiers:
modifier[1] -= 1
if modifier[1] == 0:
if modifier[0] == "big_pad":
self.objects.pad().setWidth(3)
del modifier
def generatePowerup(self):
r = random.randint(1,3)
# 3 possible powerups:
# 1 - +1 life
# 2 - for 30 sec the pad is larger
# 3 - additional ball
print (r)
if r == 1:
self.status.lives += 1
elif r == 2:
self.status.modifiers.append( [ "big_pad", gfx["framerate"] * 30 ] )
self.objects.pad().setWidth(5)
elif r == 3:
ball = Ball()
ball.position[0] = self.objects.pad().position[0] + (gfx["grid"][0]*self.objects.pad().gridWidth/2)
ball.speedChange( int( math.copysign( 5., float(self.objects.pad().lastMove) ) ), -5 )
self.objects.addBall(ball)
def handleBrickCollision(self,obj):
brickType = obj.getType()
if brickType == "simple":
self.objects.setGrid( obj.x, obj.y, None )
rand = random.randint( 0, 9 )
if rand == 9: # 1/10 chance for powerup
self.objects.spawnPowerup( obj.x, obj.y )
elif brickType == "solid":
self.objects.setGrid( obj.x, obj.y, SimpleBrick(obj.x,obj.y) )
elif brickType == "ghost":
newType = random.randint( 0, 1 )
if newType == 0:
self.objects.setGrid( obj.x, obj.y, SimpleBrick(obj.x,obj.y) )
elif newType == 1:
self.objects.setGrid( obj.x, obj.y, SolidBrick(obj.x,obj.y) )
self.playSound( "Resources/snd/ding.wav" )
def newLevel(self):
self.playSound( "Resources/snd/tada.wav" )
self.mapper.load( self.status.level )
self.objects.pad().reset()
while len(self.objects.balls()) != 1:
self.objects.removeBall((self.objects.balls())[0])
self.objects.balls()[0].reset()
self.actualState = states["START"]
class Status:
def __init__(self):
self.level = 0
self.points = 0
self.lives = startState["lives"]
self.modifiers = []
game = Game()
while True:
game.loop()

How to write a loop with an "or" condition

What would be the best way to write this statement in Java.
continue loop while either x or y equals true
I have tried these and they fail. I am not sure of the proper way to construct this statement.
while (x || y == true)
while (x | y == true)
while (y == true) || (x == true)
while (y == true) | (x == true)
Thanks in advance.
Alright, so in Java, the best way would be
while(x || y)
If x and y are Booleans. If you are testing conditions,
while(x == 5 || y == 3)
while( x == true || y == true ){
//do loopy stuff
}
This may be different for some languages, but for C based syntax languages, this should work