TypeError: 'NoneType' object is not an iterator Rdkit VS code - visual-studio-code

I'm trying to filter molecules from database 'part1' via Lipinski,Ghose and RUle of 3 filter but I keep getting error.
So, after I ran this script, I get an error 'object is not an iterator' at the line where 'progressbar' is mentioned twice. I'm new and would appreciate help.
Below is my script for filtering database of molecules.
from rdkit import Chem
from rdkit.Chem import Descriptors
import progressbar
if __name__ == '__main__':
molecules = Chem.SDMolSupplier('chemspidersdf/part1.sdf')
results = {
"Lipinski Rule of 5": 0,
"Ghose Filter": 0,
"Rule of 3 Filter": 0,
}
print ("Molecule Database Length: " + str(len(molecules)))
for i in progressbar.ProgressBar(range(len(molecules))):
molecule = molecules[i]
if molecule:
lipinski = False
rule_of_3 = False
ghose_filter = False
molecular_weight = Descriptors.ExactMolWt(molecule)
logp = Descriptors.MolLogP(molecule)
h_bond_donor = Descriptors.NumHDonors(molecule)
h_bond_acceptors = Descriptors.NumHAcceptors(molecule)
rotatable_bonds = Descriptors.NumRotatableBonds(molecule)
number_of_atoms = Chem.rdchem.Mol.GetNumAtoms(molecule)
molar_refractivity = Chem.Crippen.MolMR(molecule)
# Lipinski
if molecular_weight <= 500 and logp <= 5 and h_bond_donor <= 5 and h_bond_acceptors <= 5 and rotatable_bonds <= 5:
lipinski = True
results["Lipinski Rule of 5"] += 1
# Ghose Filter
if molecular_weight >= 160 and molecular_weight <= 480 and logp >= 0.4 and logp <= 5.6 and number_of_atoms >= 20 and number_of_atoms <= 70 and molar_refractivity >= 40 and molar_refractivity <= 130:
ghose_filter = True
results["Ghose Filter"] += 1
# Rule of 3
if molecular_weight <= 300 and logp <= 3 and h_bond_donor <= 3 and h_bond_acceptors <= 3 and rotatable_bonds <= 3:
rule_of_3 = True
results["Rule of 3 Filter"] += 1
print (results)

I assume you are using progressbar2. Looks like progressbar should be lowercase:
mols = Chem.SDMolSupplier('path/to/my/mols.sdf')
n_mols = len(mols)
for x in progressbar.progressbar(range(n_mols)):
# do stuff...
It also works like this:
mols = Chem.SDMolSupplier('path/to/my/mols.sdf')
for mol in progressbar.progressbar(mols):
if mol is None:
continue
# do stuff...

Related

Why is TIC-80 giving me the attempt to index a nil value error?

So I am building a game for a game jam over at itch.io, and I'm using this really neat Fantasy Console called TIC-80, found at tic80.com. My issue is that while I understand what the error message is and what it means, I don't understand as to why it's giving me this error.
Error Message (In the TIC-80 console):
>[string "-- title: The Clone
Wars..."]:144: attempt to index a nil
value (field '?')
stack traceback:
[string "-- title: The Clone
Wars..."]:144: in function 'TIC'
The Code in question:
-- title: The Clone Wars
-- author: DinoNuggies
-- desc: Help the jedi destroy the clones!
-- script: lua
--Functions
function sign(n) return n>0 and 1 or n<0 and -1 or 0 end
function lerp(a,b,t) return (1-t)*a + t*b end
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
--End of Functions
--Variables
player = {
spr = 256,
sprMod = 0,
x = 1,
y = 1,
vx = 0,
vy = 0,
dirX = 0,
dirY = 0,
flip = 0,
shoot = false,
}
gun = {
t = 0,
spr = 258,
sprMod = 0,
x = 0,
y = 0,
modX = 0,
modY = 0,
flip = 0,
rot = 0,
}
tile = {
r0 = 0,
r1 = 0,
l0 = 0,
l1 = 0,
u0 = 0,
u1 = 0,
d0 = 0,
d1 = 0,
m = 0,
}
m = {
x = 0,
y = 0,
left = false,
right = false,
middle = false,
}
cam = {
activate = true,
x = 120,
y = 64,
}
--Bullet Class & Functions
bulletMod = 0
bullet = {}
function bullet:new()
local this = {
spr = 260,
x = player.x,
y = player.y,
vx = 0,
vy = 0,
mx = m.x - (player.x + cam.x),
my = m.y - (player.y + cam.y),
t = 0,
}
return this
end
function bullet:remove()
local this = {
spr = nil,
x = nil,
y = nil,
vx = nil,
vy = nil,
mx = nil,
my = nil,
t = nil,
}
return this
end
--End of Variables
function TIC()
cls()
--Camera
if cam.activate then
cam.x = math.min(120, lerp(cam.x, 120-player.x, 0.05))
cam.y = math.min(64, lerp(cam.y, 64-player.y, 0.05))
ccx = cam.x / 8 + (cam.x % 8 == 0 and 1 or 0)
ccy = cam.y / 8 + (cam.y % 8 == 0 and 1 or 0)
end
map(15 - ccx, 8 - ccy, 31, 18, (cam.x % 8) - 8, (cam.y % 8) - 8, 0)
--End of Camera
--Gun Physics
m.x, m.y, m.left, m.middle, m.right = mouse()
gun.x = player.x + cam.x
gun.y = player.y + cam.y
bullets = tablelength(bullet) - 2
flick = (time() // 16) % 16 == 0
if m.left then m.leftp = flick else m.leftp = false end
--Gun Display
if m.x > player.x + cam.x + 4 then
gun.flip = 0
gun.modX = 4
else
gun.flip = 1
gun.modX = -4
end
--Gun Firing
if m.leftp == true then
bullet[bullets] = bullet:new()
if m.x > player.x + cam.x + 4 then
bullet[bullets].vx = 8
else
bullet[bullets].vx = -8
end
end
--End of Gun Physics
--Bullet Physics
if bullets > 0 then
for i=0,bullets-1 do
bullet[i].x = bullet[i].x + bullet[i].vx --LINE 144, THE ONE IN QUESTION
bullet[i].y = bullet[i].y + bullet[i].vy
spr(bullet[i].spr, bullet[i].x + cam.x, bullet[i].y + cam.y, 0, 1)
bullet[i].t = bullet[i].t + 1
if bullet[i].t > 16 then
bullet[i] = bullet:remove()
bullet[i] = nil
end
end
end
bullets = tablelength(bullet) - 2
--End of Bullet Physics
--Drawing
--Sprites
spr(player.spr + player.sprMod, player.x + cam.x, player.y + cam.y, 0, 1, player.flip)
spr(gun.spr + gun.sprMod, gun.x + gun.modX, gun.y + gun.modY, 0, 1, gun.flip, gun.rot)
--Debug
print("Debug:", 1, 1, 11)
print("X: " .. (player.x // 8) + 15 .. " Y: " .. (player.y // 8) + 9, 1, 8, 11)
print("BLTs: " .. bullets .. " ", 1, 16, 11)
--End of Drawing
--Player Movement
player.x = player.x + player.vx
player.y = player.y + player.vy
if key(1) and player.x > -120 then
player.vx = -1
player.sprMod = 1
player.dirX = 0
elseif key(4) then
player.vx = 1
player.sprMod = 0
player.dirX = 1
else
player.vx = 0
end
if key(23) and player.y > -64 then
player.vy = -1
player.dirY = 0
elseif key(19) then
player.vy = 1
player.dirY = 1
else
player.vy = 0
end
if btnp(0) then player.y = player.y - 1
elseif btnp(1) then player.y = player.y + 1
elseif btnp(2) then player.x = player.x - 1
elseif btnp(3) then player.x = player.x + 1
end
--End of Movement
--Player Collision
tile.r0 = mget(((player.x + 7) // 8) + 15, ((player.y - 9) // 8) + 9)
tile.r1 = mget(((player.x + 7) // 8) + 15, ((player.y - 2) // 8) + 9)
tile.l0 = mget(((player.x - 2) // 8) + 15, ((player.y - 9) // 8) + 9)
tile.l1 = mget(((player.x - 2) // 8) + 15, ((player.y - 2) // 8) + 9)
tile.u0 = mget(((player.x - 1) // 8) + 15, ((player.y - 10) // 8) + 9)
tile.u1 = mget(((player.x + 6) // 8) + 15, ((player.y - 10) // 8) + 9)
tile.d0 = mget(((player.x - 1) // 8) + 15, ((player.y - 1) // 8) + 9)
tile.d1 = mget(((player.x + 6) // 8) + 15, ((player.y - 1) // 8) + 9)
if player.dirX == 1 then
if fget(tile.r0, 0) or fget(tile.r1, 0) then
player.vx = 0
end
elseif player.dirX == 0 then
if fget(tile.l0, 0) or fget(tile.l1, 0) then
player.vx = 0
end
end
if player.dirY == 0 then
if fget(tile.u0, 0) or fget(tile.u1, 0) then
player.vy = 0
end
elseif player.dirY == 1 then
if fget(tile.d0, 0) or fget(tile.d1, 0) then
player.vy = 0
end
end
--End of Player Collision
--Misc
if keyp(3) then
if cam.activate then
cam.activate = false
else
cam.activate = true
end
end
--End of Misc
end
--DON'T WORRY ABOUT ANYTHING PAST THIS POINT, IT'S ONLY SPRITE AND TILE DEFINITIONS FOR TIC-80 VISUALS
-- <TILES>
-- 000:6666666666666666666666666666666666666666666666666666666666666666
-- 001:6666666666566566666666656666666665665666666665666566666666666666
-- 002:6668666666898566666866656666666665665866686689868986686668666666
-- 003:6656556666555556555556555555556555555555665555556556555666655566
-- 016:dddddddeddddddefddeeeeffddeeeeffddeeeeffddeeeeffdeffffffefffffff
-- 017:6226622661266126222222221121112161266126612661265126512665266525
-- 018:6228622661298126222222221121112161266126612681268126512668266525
-- 019:6226522661255126222222221121112151255125612551255126512665255525
-- </TILES>
-- <SPRITES>
-- 000:00cccc000cccccd00cbbbbb0ccbbcbbdcccfefcd0dddddd000ceec0000c00c00
-- 001:00cccc000cccccd00bbbbbd0cbbcbbcdccfefccd0dddddd000ceec0000c00c00
-- 002:000000000000000000000000000eeeed000efff0000ef0000000000000000000
-- 003:0000ed00000eef0000eef0000eef000000eef000000ee0000000000000000000
-- 004:0000000000000000000000000aaaaaa00aaaaaa0000000000000000000000000
-- </SPRITES>
-- <MAP>
-- 000:010101010101010101010101010101010101010101010101010101010101010000000000000000000000101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 001:010000000000000000000000000000101000101000001000000010101001010000000000100000000000101010000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 002:010001000100010000000000000010000000100010001000100010001001010000000010100000000000101010000000000000001010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 003:010001000100000000000000000000101000100010001000100010100001010000000010000000000000101000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 004:010001010100010000000000000000001000100010001000100010000001010000000010000000000000101010000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 005:010001000100010000000000000010100000001010000010100010000001010000000010000000000000101010000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 006:010000000000000000000000000101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 007:010000000000000000000000000101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 008:010000000000000000000000000101301010000000000000000000000000000000000000101010000000101000000000000000000010101000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 009:010000000000000000000000000101101010100000000000000000000000000000001010101000000000101000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 010:010000000000000000000000000101101010000000000001111101000000001111111121211111110000101000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 011:010000000000000000000000000101000000000000001000000001000000000000001020101000000000101000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 012:010000000000000000000000000101000000000000011111111101010001010000101010000000000010101000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 013:010000000000000000000000000101000000000010101000000001011101010000101000000000000010100000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 014:010000000000000000000000000101000000000010101010000000000000010000000000000000000010100000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 015:010000000000000000000000000101000000000000000000000000000000010000000000000000001010000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 016:010101010101010101010101010101000000000000000000000000000000010000000000000000001010000000000000001010000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 017:010101010101010101010101010101000000000000000000000000000000000000000000000000101010000000000010102000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- 018:000000000000000000000000000101000000000000000000000010100000000000000000000000101000000000001010000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 019:000000000000000000001010000101000000000000000000000000100000000000000000000010101000001111211110000000001010000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 020:000000001000000000001000000101000000000000000000000000101000000000000000001010101000000010101011111100000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 021:000000000000000000001010100101101010000000000000000000000000000000000000101000000000001010200000000011110000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 022:000000000000000000000000000101001010100000000000000000000000000000000010101000000010101010100000000000001111000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 023:000000000000000000000000000101000000000000000000000000000000000000001010100000000010101010000000000000000000110000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 024:000000000000000000000000000101000000000000000000000000000000000000001010100000001020201000000000000000100000001100102001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 025:000000000000000000000000000101000000000000000000000000000000000000101010000000001020100000000000000000100000000011212101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 026:000000101000000000000000000101000000000000000000000000000000001010100010000000001010100000000010000000100010100010101001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 027:000000101010000000000000100101000000000000000010101010101010101010000010000000001010000000000000101010000000001010000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 028:000000100010000000000000100101000000000000000000000000001010101000101010000000001010000010101010000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 029:000000001010000000000000000101000000000000000000000000000010100000000000000000001010000000000000000000100010000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 030:000000001010000000000000000101100000000000000000000000000000000000000000000000001000000000001000000000100000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 031:000000001010000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 032:000000000000000000000000000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 033:000000000000000000000000000101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- 135:010101010101010101010101010101010101010101010101010101010101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
-- </MAP>
-- <WAVES>
-- 000:00000000ffffffff00000000ffffffff
-- 001:0123456789abcdeffedcba9876543210
-- 002:0123456789abcdef0123456789abcdef
-- </WAVES>
-- <SFX>
-- 000:020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200304000000000
-- 001:020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200307000000000
-- 002:02000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020002000200020030b000000000
-- </SFX>
-- <FLAGS>
-- 000:00000000000000000000000000000000101010100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
-- </FLAGS>
-- <PALETTE>
-- 000:1a1c2c5d2810814428ca9581ffcd7575ae442495482571793c55c22c9dfae20000000000f4f4f494b0c2566c86333c57
-- </PALETTE>
What triggers the error, is after the player fires the second bullet. After firing one, the bullet is created, displayed, and despawned on queue, but after the second bullet starts existing, it stops and gives me the error. I've switched things around quite a bit, and it seems like every time I reference to one of the bullet objects values when more then one exists, it sends me the error, which is what I don't understand, as i thought I had already solved that problem with the for loops.
So if you noticed anything right off the bat that doesn't look quite right, let me know, and if you don't know anything about TIC-80 or what the API does, I'm sure the TIC-80 website can explain it way better then me.
If you want to run the game, to see the issue in action and mess around with the code, download TIC-80 from the website and run this file:
https://drive.google.com/file/d/18ti0NboNNN9Yog6l_n73usF_eX_86EN4/view?usp=sharing
Let's take a look at your bullet handling
First call to TIC:
bullets is 0. We add one bullet into bullet[0]
Second call:
bullets is 1. We add one bullet into bullet[1].
Now, as bullets > 0 we enter the bullet physics if statement.
We do some calculations to that bullet and increment bullet[0].t
The following calls we do the same. We add a bullet and process all but the new bullet as we did above.
When a bullet's t field becomes > 16 we remove it from bullet.
So first we remove the oldest bullet bullet[0]. But in the following call we again start our loop at i = 0. so bullet[i] is nil and hence indexing it in bullet[i].x causes the observed error.
Side note:
This makes no sense.
function bullet:remove()
local this = {
spr = nil,
x = nil,
y = nil,
vx = nil,
vy = nil,
mx = nil,
my = nil,
t = nil,
}
return this
end
A table with all nil values is just an empty table. So simply return an empty table.
On top of that you don't need that function as it does nothing useful.
bullet[i] = bullet:remove()
bullet[i] = nil
The first line is non necessary. You don't have to assign an emtpy table and then nil. Just assign nil.
If you'd just keep the bullets in the array part of the table you wouldn't need your own tablelength function and then subtract 2 btw.
Then you could also use table.remove to remove bullets without creating unexpected gaps in your bullet list.

Pygame PiTFT touch events - main loop sits waiting until touches are detected

I am using a raspberry pi and PiTFT to measure sensors and display the results.
Using the tips from https://stackoverflow.com/a/54986161 all is working fine but one problem:
the main loop sits waiting until the lcd is touched.
I removed all non trivial parts from the program just to test the main loop.
This piece of code is printing the state after touch and release.
As long as the lcd is being touched the main loop keeps printing, releasing the lcd stops the loop. How can I rewrite the loop so it's keeps running even when there is no keypress?
#!/usr/bin/python3
import subprocess, pygame, evdev, select
pygame.init()
surfaceSize = (320, 240)
##
# Everything that follows is for handling the touchscreen touch events via evdev
##
# Used to map touch event from the screen hardware to the pygame surface pixels.
# (Those values have been found empirically, but I'm working on a simple interactive calibration tool
tftOrig = (3750, 180)
tftEnd = (150, 3750)
tftDelta = (tftEnd [0] - tftOrig [0], tftEnd [1] - tftOrig [1])
tftAbsDelta = (abs(tftEnd [0] - tftOrig [0]), abs(tftEnd [1] - tftOrig [1]))
# We use evdev to read events from our touchscreen
# (The device must exist and be properly installed for this to work)
touch = evdev.InputDevice('/dev/input/touchscreen')
# We make sure the events from the touchscreen will be handled only by this program
# (so the mouse pointer won't move on X when we touch the TFT screen)
touch.grab()
# Prints some info on how evdev sees our input device
print(touch)
# Even more info for curious people
#print(touch.capabilities())
# Here we convert the evdev "hardware" touch coordinates into pygame surface pixel coordinates
def getPixelsFromCoordinates(coords):
# TODO check divide by 0!
if tftDelta [0] < 0:
x = float(tftAbsDelta [0] - coords [0] + tftEnd [0]) / float(tftAbsDelta [0]) * float(surfaceSize [0])
else:
x = float(coords [0] - tftOrig [0]) / float(tftAbsDelta [0]) * float(surfaceSize [0])
if tftDelta [1] < 0:
y = float(tftAbsDelta [1] - coords [1] + tftEnd [1]) / float(tftAbsDelta [1]) * float(surfaceSize [1])
else:
y = float(coords [1] - tftOrig [1]) / float(tftAbsDelta [1]) * float(surfaceSize [1])
return (int(x), int(y))
# Was useful to see what pieces I would need from the evdev events
def printEvent(event):
print(evdev.categorize(event))
print("Value: {0}".format(event.value))
print("Type: {0}".format(event.type))
print("Code: {0}".format(event.code))
while True:
r,w,x = select.select([touch], [], [])
for event in touch.read():
if event.type == evdev.ecodes.EV_ABS:
if event.code == 1:
X = event.value
elif event.code == 0:
Y = event.value
elif event.type == evdev.ecodes.EV_KEY:
if event.code == 330 and event.value == 1:
printEvent(event)
p = getPixelsFromCoordinates((X, Y))
if 12 <= p[0] <= 72 and 210 <= p[1] <= 230:
num = 0
if 92 <= p[0] <= 154 and 210 <= p[1] <= 230:
num = 1
if 173 <= p[0] <= 234 and 210 <= p[1] <= 230:
num = 2
if 255 <= p[0] <= 317 and 210 <= p[1] <= 230:
num = 3
if 290 <= p[0] <= 320 and 0 <= p[1] <= 30:
pygame.quit()
quit()
print(num)
pygame.quit()
quit()
Solved!
With the comments from #sloth and #Kingsley I rewrote the code to:
r,w,x = select.select([touch], [], [], 0)
if ( touch in r ):
for event in touch.read():
if event.type == evdev.ecodes.EV_ABS:
if event.code == 1:
X = event.value
elif event.code == 0:
Y = event.value
elif event.type == evdev.ecodes.EV_KEY:
if event.code == 330 and event.value == 1:
printEvent(event)
Now the loop runs even if there is no input, and input is working ok also.

I am creating Snake Game in python3.6.5 using PyGame. Here is the initial code and it is running well on PyCharm. But I am getting problems in VSCode

import pygame
pygame.init()
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
gameDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Snake')
gameExit = False
lead_x = 300
lead_y = 300
lead_x_change = 0
lead_y_change = 0
clock = pygame.time.Clock()
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x_change = -10
lead_y_change = 0
elif event.key == pygame.K_RIGHT:
lead_x_change = 10
lead_y_change = 0
elif event.key == pygame.K_UP:
lead_y_change = -10
lead_x_change = 0
elif event.key == pygame.K_DOWN:
lead_y_change = 10
lead_x_change = 0
if lead_x >= 750 or lead_y >= 599 or lead_y < 0 or lead_x < 0:
gameExit = True
lead_x += lead_x_change
lead_y += lead_y_change
gameDisplay.fill(white)
pygame.draw.rect(gameDisplay, black, [lead_x, lead_y, 10, 10])
pygame.display.update()
clock.tick(15)
pygame.quit()
quit()
I am getting following problems in VSCode:
E1101:Module 'pygame' has no 'init' member
E1101:Module 'pygame' has no 'K_RIGHT' member
E1101:Module 'pygame' has no 'QUIT' member
E1101:Module 'pygame' has no 'KEYDOWN' member
E1101:Module 'pygame' has no 'K_LEFT' member
E1101:Module 'pygame' has no 'quit' member
E1101:Module 'pygame' has no 'K_DOWN' member
E1101:Module 'pygame' has no 'K_UP' member
Why VScode is showing these problems while PyCharm is showing no errors and problems?
Those messages are generated by pylint. Pylint disables loading C extensions (Pygame in this case) by default for security reasons. To enable it add this to your settings:
"python.linting.pylintArgs": [
"--extension-pkg-whitelist=pygame"
]
If you have other settings right, this should be enough.

Curve mouse movement

I'm trying to make the mouse cursor move from one point to another. Not in a straight line but in a curve.
Here is an illustrative video. Here is code that moves mouse based on the locations:
Func smoothmove($coords, $j, $firstms);make mouse move to the note
Dim $currcoord[3]
Dim $diff[3]
Dim $pixelsptms[3]
If $j > 1 Then
If Mod($coords[$j - 1][7][1], 2) = 0 Then
$currcoord[1] = $coords[$j - 1][1][1]
$currcoord[2] = $coords[$j - 1][2][1]
Else
$currcoord[1] = $coords[$j - 1][1][$coords[$j - 1][1][0]]
$currcoord[2] = $coords[$j - 1][2][$coords[$j - 1][2][0]]
EndIf
Else
$currcoord[1] = MouseGetPos(0)
$currcoord[2] = MouseGetPos(1)
EndIf
If $coords[$j][1][1] = $currcoord[1] Then
If $coords[$j][2][1] = $currcoord[2] Then Return 1
EndIf
If $coords[$j][3][1] <= $firstms Then Return 1
$diff[1] = $coords[$j][1][1] - $currcoord[1]
$diff[2] = $coords[$j][2][1] - $currcoord[2]
$pixelsptms[1] = ($diff[1] / ($coords[$j][3][1] - $firstms)) * 12
$pixelsptms[2] = ($diff[2] / ($coords[$j][3][1] - $firstms)) * 12
Dim $ready[5]
$ready[1] = 2
$ready[2] = 3
$ready[3] = 2
$ready[4] = 3
$firstms += 12
$count = 0
While 1
DllCall($osumap[0], 'int', 'ReadProcessMemory', 'int', $osumap[1], 'int', $address[2], 'ptr', $bufferptr, 'int', $buffersize, 'int', '')
$ms = DllStructGetData($buffer, 1)
If $ms >= $coords[$j][3][1] Then Return 1
If $pixelsptms[1] < 0 Then
If $currcoord[1] <= $coords[$j][1][1] Then
$currcoord[1] = $coords[$j][1][1]
$ready[3] = 1
$pixelsptms[1] = 0
EndIf
Else
If $currcoord[1] >= $coords[$j][1][1] Then
$currcoord[1] = $coords[$j][1][1]
$ready[3] = 1
$pixelsptms[1] = 0
EndIf
EndIf
If $pixelsptms[2] < 0 Then
If $currcoord[2] <= $coords[$j][2][1] Then
$currcoord[2] = $coords[$j][2][1]
$ready[4] = 1
$pixelsptms[2] = 0
EndIf
Else
If $currcoord[2] >= $coords[$j][2][1] Then
$currcoord[2] = $coords[$j][2][1]
$ready[4] = 1
$pixelsptms[2] = 0
EndIf
EndIf
If $ready[3] = $ready[4] Then Return -1
If $ms >= $firstms Then
$currcoord[1] += $pixelsptms[1]
$currcoord[2] += $pixelsptms[2]
$firstms += 12
MouseMove($currcoord[1], $currcoord[2], 0)
EndIf
WEnd
EndFunc
Maybe this gives you some ideas.
#include <math.au3>
HotKeySet("{ESC}", "esc")
Global Const $PI = 3.1415926535897932384626433832795
Dim $t
Dim $expr
For $theta = 0 To 924 * $PI ;ALSO MAKE THE LOOP GO LONGER
$t = _Radian($theta)
$expr = Exp(Cos($t)) - 2 * Cos(4 * $t) - Sin($t / 12) ^ 5
$x = 400 + 100 * Cos($t) * $expr
$y = 400 + 100 * Sin($t) * $expr
ConsoleWrite($x & "," & $y & #LF) ;Use for debugging with SciTE
MouseMove($x, $y, 1)
Next
Func esc()
Exit 0
EndFunc ;==>esc

TypeError: get_stats() takes exactly 1 argument (0 given)

Trying a dnd character creator: keep getting a:TypeError: get_stats() takes exactly 1 argument (0 given). I dont know how to give the shell what it wants.
Heres the code:
import random
class Character(object):
def __init__(self,name):
self.name = raw_input('name')
# Attributes for Pathfinder
def get_stats(self):
stats = []
count = 0
while count < 6:
roll = []
roll = [random.randint(1,6) for x in range(4)]
roll = sorted(roll)
del(roll[0])
sum = 0
for x in roll:
sum += x
stats.append(sum)
count += 1
print stats
return stats
stats = get_stats()
ste = stats[0]
con = stats[1]
dex = stats[2]
inte = stats[3]
wis = stats[4]
cha = stats[5]
#Modifiers
stemod = (ste - 10)/2
dexmod = (dex - 10)/2
conmod = (con - 10)/2
intemod = (inte - 10)/2
wismod = (wis - 10)/2
chamod = (cha - 10)/2
#Bios
#Sex
sex = random.randint(1,2)
if sex == 1:
sex = "male"
else:
sex = "female"
#Races and size
#list_of_races
x = random.randint(1,7)
if x == 1:
race = 'drawf'
size = 'med'
con += 2
wis += 2
cha -= 2
elif x == 2:
race = 'elf'
size = 'med'
dex += 2
inte += 2
con -= 2
elif x == 3:
race = 'human'
size = 'med'
bonus = random.randint(1,6)
if bonus == 1:
ste += 2
elif bonus == 2:
con += 2
elif bonus == 3:
dex += 2
elif bonus == 4:
inte += 2
elif bonus == 5:
wis += 2
elif bonus == 6:
cha += 2
elif x == 4:
race = 'halfling'
size = 'small'
dex += 2
cha += 2
ste -= 2
elif x == 5:
race = 'gnome'
size = 'small'
con += 2
cha += 2
ste -= 2
elif x == 6:
race = 'half-elf'
size = 'med'
bonus = random.randint(1,6)
if bonus == 1:
ste += 2
elif bonus == 2:
con += 2
elif bonus == 3:
dex += 2
elif bonus == 4:
inte += 2
elif bonus == 5:
wis += 2
elif bonus == 6:
cha += 2
elif x == 7:
race = 'half-orc'
size = 'med'
bonus = random.randint(1,6)
if bonus == 1:
ste += 2
elif bonus == 2:
con += 2
elif bonus == 3:
dex += 2
elif bonus == 4:
inte += 2
elif bonus == 5:
wis += 2
elif bonus == 6:
cha += 2
#Size pelenties
#Classes
# List of classes(Fighter,Ranger,Wizard,Cleric)
c = random.randint(1,4)
playclass = ''
while playclass == '':
if c == 1 and ste > 10:
playclass = 'Fighter'
hp = 10 + conmod
bab = 1
fort_save = 2 + conmod
reflex_save = 0 + dexmod
will_save = 0 + wismod
elif c == 2:
playclass = 'Ranger'
hp = 10 + conmod
bab = 1
fort_save = 2 + conmod
reflex_save = 2 + dexmod
will_save = 0 + wismod
elif c == 3 and inte > 10:
playclass = 'Wizard'
hp = 6 + conmod
bab = 0
fort_save = 0 + conmod
reflex_save = 0 + dexmod
will_save = 2 + wismod
elif c == 4 and wis > 10:
playclass = 'Cleric'
hp = 8 + conmod
bab = 0
fort_save = 2 + conmod
reflex_save = 0 + dexmod
will_save = 2 + wismod
else:
c = random.randint(1,3)
#AC
ac = 10 + dexmod
if size == 'small':
ac += 1
print sex
print size +" "+race
print 'strenght : ' + str(ste)
print 'constitution : ' + str(con)
print 'dexterity : ' + str(dex)
print 'intelligence : ' + str(inte)
print 'wisdom : ' + str(wis)
print 'charisma : ' + str(cha)
print playclass
print 'HP:' + str(hp)
print 'AC:' + str(ac)
print 'BAB:' + str(bab)
print 'Fort:' + str(fort_save)
print 'Will:' + str(will_save)
print 'Reflex:' + str(reflex_save)
In your get_stats() function, you have put self. But to fill the parantheses in the line:
stats = get_stats()
You are supposed to put None in your parantheses to prevent the error. So the line will look like:
stats = get_stats(None)
Why did add None? This means that the amount of stuff in the parantheses other than self is 0 or None. I hope this helps you!