Python simple game with pygame - limits - python-3.x

I made a simple game with python using the Pygame library but when I finished I just noticed that I forgot something. LIMITS
I am pretty new at python and I tried with
if player_pos[1] <= 600:
pygame.K_DOWN = None
*player_pos[1] is the y player's position
*600 is the display's limit
But then, the "down" key just stopped working so I just erased that lines.

You can't (or shouldn't try at least) to turn the input into None. Just use a logical test to decide if you want to move further...
if key_pressed == 'K_RIGHT': (or whatever syntax you use to catch keypress..)
if player_pos[1] < limit:
# update the player position
else:
pass

#JeffH has already suggested the a way to fix your code. I just wanted to make sure you understood why what you did caused problems and to point out the convention for constants. Not understanding and using that naming convention will make it more difficult for you to understand other code, and will complicate others trying to read your code.
pygame.K_DOWN is intended to be a defined constant, it is not an input or a control variable. You should absolutely not change it.
Python does not have true constants that enforce not being able to change them, so it is handled by convention. Variables that are intended to be constants by convention use all uppercase names, like K_DOWN and you are just not supposed to change them. As you discovered things may break or behave weirdly if you do.
In your case 'the down key stopped working' because the constant used to check it no longer contained the right value anymore. Pygame will give you the keycode for the down arrow key (the number 274 when I print it on my system) but you have assigned None to the constant that you are supposed to compare that value with to determine if it is the down arrow key, so that comparison will now fail.
That is why it stopped things working.

Related

How do I resize my Window twice with the same button?

So if i try to resize my Window with the same button back and forth just the one on the bottom will work but it wont go back to '384x470' as if the Window wasnt '500x500'
def sizeWindow():
if root.geometry('500x500'):
root.geometry('384x470')
else:
root.geometry('500x500')
buttonWinSize.configure(text="<<")```
You seem to misunderstand several things here. Tkinter has relatively cool feature that allows you to use some methods for different things. Either you get or you set with the same method. To get you leave the interface empty (e.g. root.geometry()) or you set with the required argument (e.g. root.geometry('500x500+0+0')). You should be aware that you get the current value(s) in the same format as you set them.
Therefore you retrieve a geometry string by root.geometry() and this has the form of width x height +x +y.
However, not every time this feature is the right tool to achieve the overall goal. In your case you want to check for the width and height. Tkinter provides another feature for exactly this task. It lets you ask for the width and height via winfo_width() and winfo_height().
In addition you should know that every function in python needs to return something and if nothing is specified None is returned by this function. Since you use the setter variant of root.geometry('500x500') you receive None and None equals to False in python, therefore your else block will always be executed instead of your if block.
Suggested change:
def sizeWindow():
if root.winfo_width() == 500 and root.winfo_height() == 500:
root.geometry('384x470')
else:
root.geometry('500x500')
Your if-statement is wrong. You are actually setting the size with your first root.geometry('500x500'). This does (AFAIK) not return a boolean. So you need to check the actual current dimensions:
if root.winfo_width() == 500 and root.winfo_height() == 500:
root.geometry('384x470')
else:
root.geometry('500x500')
buttonWinSize.configure(text="<<")

kivy switch_to screen on if statement doesn't work

I could REALLY use your help with this one.
I'm trying to make a sort-of voice command operated menu for a toddler's learning app and kivy is giving me a headache
all of my screens are correctly defined and load as intended if the buttons are pressed but the voice commands, even though they register correctly and carry over their variables as intended they don't seem to have the desired effect when asked to act upon ScreenManager when the if statement is fulfilled
def on_enter(self):
....
Command.start()
Command.introMenu()
......
if Command.sel == "shapes":
ScreenManager().switch_to = "shapes"
elif Command.sel == "colours":
ScreenManager().switch_to = "colours"
......
else:
pass
the variable Command.sel is captured from a dependency, defined as a string and carried correctly as far as I can tell from the variables view in debugging
yet even though everything seems to be in order (in fact no error messages appear at all) the desired screen is not called when the if condition is met
what am I doing wrong here???
full code here
(please ignore the Greek bits in the code... it's just strings, imagine it's any other language for that matter...)
thank you!
issue resolved
correct command was
self.parent.current = "your_screen_name"
answer (eventually) found here

How can I make PyCharm break when a variable takes a certain value?

I have a big dictionary and some of the elements occasionally end up with illegal values. I want to figure out where the illegal values are coming from. PyCharm should constantly monitor the values of my dictionary, and the moment any of them take the illegal value, it should break and let me inspect the state of the program.
I know I can do this by just creating a getter/setter for my dictionary instead of accessing it directly, and then break inside the setter with an appropriate condition.
Is there a way to do it without modifying my code?
I'm not sure if this answers your question but you can set a breakpoint on the line of code you want to break at, right click on that break point once it is set and then apply a condition.
An example of such a condition could be:
x > 5
Once you are at the stage in your loop/code where this condition is true i.e. when x = 6 then it will break and you can inspect all the current values/ status of your code.
Hope this helps

python one-line input referenced twice

How can I reference an input twice in a one line code?
Ex:
my_word=input()
print("hey" if my_word==my_word else "bye")
You are only referencing it once right now, so this is easy:
print("hey" if input().isdigit() else "bye")
Though you could argue that this line of code does too much, and may be difficult to maintain. Breaking it into two lines makes maintenance easier, and for example it also allows you to set a breakpoint on the print line and inspect the value in my_word if you wanted to.
For academic reasons, here is one possible solution to evaluating an expression once but using it multiple times in one statement: list comprehension. (This is a terrible, terrible idea, and you shouldn't do this. I mean it.)
[print(i if i.isdigit() else "bye") for i in (input(),)]

How to number floats in LaTeX consistently?

I have a LaTeX document where I'd like the numbering of floats (tables and figures) to be in one numeric sequence from 1 to x rather than two sequences according to their type. I'm not using lists of figures or tables either and do not need to.
My documentclass is report and typically my floats have captions like this:
\caption{Breakdown of visualisations created.}
\label{tab:Visualisation_By_Types}
A quick way to do it is to put \addtocounter{table}{1} after each figure, and \addtocounter{figure}{1} after each table.
It's not pretty, and on a longer document you'd probably want to either include that in your style sheet or template, or go with cristobalito's solution of linking the counters.
The differences between the figure and table environments are very minor -- little more than them using different counters, and being maintained in separate sequences.
That is, there's nothing stopping you putting your {tabular} environments in a {figure}, or your graphics in a {table}, which would mean that they'd end up in the same sequence. The problem with this case (as Joseph Wright notes) is that you'd have to adjust the \caption, so that doesn't work perfectly.
Try the following, in the preamble:
\makeatletter
\newcounter{unisequence}
\def\ucaption{%
\ifx\#captype\#undefined
\#latex#error{\noexpand\ucaption outside float}\#ehd
\expandafter\#gobble
\else
\refstepcounter{unisequence}% <-- the only change from default \caption
\expandafter\#firstofone
\fi
{\#dblarg{\#caption\#captype}}%
}
\def\thetable{\#arabic\c#unisequence}
\def\thefigure{\#arabic\c#unisequence}
\makeatother
Then use \ucaption in your tables and figures, instead of \caption (change the name ad lib). If you want to use this same sequence in other environments (say, listings?), then define \the<foo> the same way.
My earlier attempt at this is in fact completely broken, as the OP spotted: the getting-the-lof-wrong is, instead of being trivial and only fiddly to fix, absolutely fundamental (ho, hum).
(For the afficionados, it comes about because \advance commands are processed in TeX's gut, but the content of the .lof, .lot, and .aux files is fixed in TeX's mouth, at expansion time, thus what was written to the files was whatever random value \#tempcnta had at the point \caption was called, ignoring the \advance calculations, which were then dutifully written to the file, and then ignored. Doh: how long have I know this but never internalised it!?)
Dutiful retention of earlier attempt (on the grounds that it may be instructively wrong):
No problem: try putting the following in the preamble:
\makeatletter
\def\tableandfigurenum{\#tempcnta=0
\advance\#tempcnta\c#figure
\advance\#tempcnta\c#table
\#arabic\#tempcnta}
\let\thetable\tableandfigurenum
\let\thefigure\tableandfigurenum
\makeatother
...and then use the {table} and {figure} environments as normal. The captions will have the correct 'Table/Figure' text, but they'll share a single numbering sequence.
Note that this example gets the numbers wrong in the listoffigures/listoftables, but (a) you say you don't care about that, (b) it's fixable, though probably mildly fiddly, and (c) life is hard!
I can't remember the syntax, but you're essentially looking for counters. Have a look here, under the custom floats section. Assign the counters for both tables and figures to the same thing and it should work.
I'd just use one type of float (let's say 'figure'), then use the caption package to remove the automatically added "Figure" text from the caption and deal with it by hand.

Resources