r/inventwithpython • • Oct 25 '16

[Pygame] Debugging Pygame Help?

1 Upvotes

Hi guys, I just learned about virtual environments. To avoid running into problems further down the road, I scrapped my modules before they got out of hand and am now working with virtual environments, a 'pygame' env for everything pygame, with Pygame re-installed, naturally. But now it's acting up.

Take as an example the memorypuzzle.py from http://inventwithpython.com/pygame/downloads/ Most of it works, but it doesn't reveal the second square when you click it. I already checked the 'firstSelection' part, that works. The square also does get set to 'True' in the 'revealedBoxes' List. And it does show (and keeps showing) both squares once you've found a matching pair.

tl;dr -- I don't get any error messages and the game code as such seems to be correct (it's the sample code, and it worked fine before), so I have no idea where to even start debugging? Help?

Many thanks in advance!

UPDATE: so now it doesn't reveal any squares anymore.

I suspected sth must be wrong with event handling, so I added the following lines to the code:

myevents = pygame.event.get()

      if len(myevents) != 0:

          print(myevents)

(I put it into the main Game Loop, inside the "while True", just before "for event in pygame.event.get()"...)

The output that generates is this. Note that I clicked on a different square each time, so I don't know why the position doesn't keep changing:

[[<Event(4-MouseMotion {'buttons': (0, 0, 0), 'rel': (229, 375), 'pos': (229, 375)})>, <Event(5-MouseButtonDown {'pos': (229, 375), 'button': 1})>, <Event(6-MouseButtonUp {'pos': (229, 375), 'button': 1})>, <Event(4-MouseMotion {'buttons': (0, 0, 0), 'rel': (-139, -326), 'pos': (90, 49)})>, <Event(5-MouseButtonDown {'pos': (90, 49), 'button': 1})>, <Event(6-MouseButtonUp {'pos': (90, 49), 'button': 1})>]
[<Event(5-MouseButtonDown {'pos': (90, 49), 'button': 1})>]
[<Event(6-MouseButtonUp {'pos': (90, 49), 'button': 1})>]
[<Event(5-MouseButtonDown {'pos': (90, 49), 'button': 1})>]
[<Event(6-MouseButtonUp {'pos': (90, 49), 'button': 1})>]
[<Event(5-MouseButtonDown {'pos': (90, 49), 'button': 1})>]

(last line gets repeates as often as I keep clicking)

Any help would be very much appreciated. Thanks!


r/inventwithpython • • Feb 19 '14

Unable to open IDLE but can run the python.exe file just fine

1 Upvotes

error msg reads IDLE's subprocess didn't make connection. Can I just use the exe file or is it important to have access to the IDLE gui?


r/inventwithpython • • Oct 28 '16

Making a simple widget GUI program. Using Tkinter need help understanding what to do. [General Python Question]

3 Upvotes

http://pastebin.com/ATERBWiL

I am rather new to OOP, and building GUI's from it. So I am hoping someone can explain to me what I am doing wrong. I think when I input a number to the object inch it is not an integer and therefore not accepted by the flow control operators. But I don't know how to fix this problem. - Lucas


r/inventwithpython • • Sep 23 '15

regex version of strip() from automate the boring stuff ch. 7

3 Upvotes

I'm trying to figure out the right regex to create my own version Python's strip() function. Below is my code:

import re

def regexStrip(string, c):
    regex = '([' + c + ']*)(.*)([' + c + ']*$)'
    strip = re.compile(regex)
    print strip.search(string).group(2)

My function seems to strip the preceding part but not the part that follows. When I run regexStrip('eeeestripee', 'e'), for example, the output is 'stripee'. Thanks in advance.


r/inventwithpython • • Feb 21 '14

I know Python 2.X.X programs can be turned into .exe files that other people can play, but can you also do that with Python 3.X.X?

2 Upvotes

I looked it up and it seemed like you couldn't, but it was slightly old information so I wanted to double check. I am currently learning from the book, but typing everything in Python 2.7.6 by changing a couple things around, but is that necessary? I want to be able to write programs good enough to be Greenlighted on Steam someday, and to do that I'll need to write in code that can be executed by any normal computer (i.e. without downloading python and pygame).


r/inventwithpython • • Apr 12 '16

I've downloaded and installed Pygame from four different websites now, but each time I try to import it IDLE tells me there is no module named 'pygame'.

2 Upvotes

I have Python 3.5.1 (32 bit) installed, and installed above mentioned downloads in the same directory. But I'm a noob, so I might very well have overlooked something.

I also tried the method that this user who had the same problem links to, but it didn't work for me either. In the same thread, somebody also tried to explain it, but used all kinds of terms that I didn't understand as a noob.

Any help would be appreciated.


r/inventwithpython • • Sep 28 '15

Problem with a project from Automate The Boring Stuff

4 Upvotes

Edit: Code has now been fixed.

I've created the randomQuizGenerator from Auomate the Boring stuff chapter 8 (https://automatetheboringstuff.com/chapter8/). It should create 70 papers, 35 quiz sheets, 35 answer sheets.

It does that, but they're largely blank and I can't seem to figure where I am going wrong?

Any help would be much appreciated. My code is in the link below;

https://github.com/Millsy88/AutomateTheBoringStuff/blob/master/RandomQuizGenerator.py

Thanks!


r/inventwithpython • • Jan 18 '15

Small question about Chapter 6? Regarding functions! :)

3 Upvotes

Hi all! Can someone help me understand what "chosenCave" is referring to in Chapter 6 (the dragon realm game). Its used as an argument here: "def checkCave(chosenCave):" but I have no idea where "chosenCave" is defined in the code before. I am used to seeing a variable defined before using it as an argument in a function... but I can't see where this is done before. I have tried reading the summary but I really don't get it still D:. Link to the chapter is here: http://inventwithpython.com/chapter6.html

I'm new to reddit and python, so sorry if I've done anything nooby without realising!


r/inventwithpython • • Apr 02 '17

How to install PYPERCLIP? [hacking]

1 Upvotes

Hey there.

I've been have trouble finding out how to install the pyperclip module on windows. Any help would be much appreciated.

I am using the Python 3.6.1


r/inventwithpython • • Jan 07 '15

Has anyone figured out how to display .png images?

2 Upvotes

I have a top and bottom png image. The top image is mostly transparent (alpha) so I should be able to see most of the bottom image, yet I see black in those areas instead (as if the top image image transparency isn't working).

Any pointers how to go about this?


r/inventwithpython • • Oct 27 '16

Affine Cipher Hacker (ch. 16)

1 Upvotes

One of my students is participating in the national cipher challenge (UK) and has been working through the Hacking book at the same time. He knows that challenge 2B is an Affine Cipher and wanted to use his program to find the solution. The Affine sample ciphertext can be hacked by the program (alan turing quote), but it fails when trying challenge 2B https://www.cipherchallenge.org/challenges/challenge-2/

Two other online tools were able to hack it (a= 5, b = 11): http://rumkin.com/tools/cipher/affine.php http://www.dcode.fr/affine-cipher

We've tried many variations to no avail.

What are we missing? Any advice?


r/inventwithpython • • Jan 25 '18

[automate] (Chapter 8) trouble with the open() function

4 Upvotes

[Now solved]

I cannot seem to work the open() function.

I created a file in my home directory called 'hello2.txt' (I made several, but they keep not working so the name has evolved). The file just has the text "Hello world!" in it as recommended in the book.

When I run the suggested text (with my attempt to adapt it for linux) in idle:

              .>>> helloFile = open('/home/chris/hello2.txt')

I get:

Traceback (most recent call last):

File "<pyshell#7>", line 1, in <module>

helloFile=open('/home/chris/hello2.txt')

FileNotFoundError: [Errno 2] No such file or directory: '/home/chris/hello2.txt'

So I tried:

.>>> helloFile=open('~/home/chris/hello2.txt')

with the thought that maybe for linux one needs the leading ~, but that gave the same result.

When I run the terminal and look at the directory with the 'ls' command, I see the file.

I can open the file with a text editor no problem. I can open it from the shell using the same path as attempted above... for example, if I run

$pico ~/home/chris/hello2.txt

from the terminal's Linux command line I can edit the program in that editor.

I thought that maybe it was a typo in the book to not have the "os." in front of open, so I tried

helloFile=os.open('/home/chris/hello2.txt')

and I get this error:

Traceback (most recent call last): File "<pyshell#6>", line 1, in <module> helloFile=os.open('/home/chris/hello2.txt') TypeError: Required argument 'flags' (pos 2) not found

So I thought maybe I needed the 'r' after the file (as a flag) and ran:

.>>>helloFile=os.open('/home/chris/hello2.txt', 'r')

...but this gives me an error about an expected integer.

I have tried finding someone else who has the same problem, but I cannot seem to ask the question well enough to find anything on stack overflow or reddit.

I tried many things including restarting (in case the python shell did not update it's index of my directory) -- and I did import os at the beginning of each python shell session. (Restarting made no noticeable impact on this problem.)

I have also tried making other .txt files and putting them in other directories and that also does not work. I don't need sudo to open the file from the Linux command line, so I don't think that is creating a problem for me.

Clearly I am making some mistake but I cannot out what it is and I would love some help.

(I am running python 3.5.2 on a linux mint machine if any of that matters.)


r/inventwithpython • • Mar 02 '14

Chapter 9 - Hangman

3 Upvotes

I'm going through and adding comments to the code so that I can make sure I understand every part of it before I move on.

this is the function that I'm having problems with specifically the for loop that replaces blanks with correctly guessed letters.

what does the i stand for in the line "for i in range(len(secretWord)):

I looked at the chapter multiple times and it eludes me.

Any help would be appreciated.

def displayBoard(HANGMANPICS, missedLetters, correctLetters, secretWord): #this function displays the game board to the player. len(missedLetters) will look at the length of the #missedLetters variable and add it as an element so the interpreter knows which picture to display to the player #from HANGMANPICS. HANGMANPICS is all caps because it is a constant variable, which does not change. #the for loop prints a _ for every letter in the secret word chosen in the function getRandomWord (line 66) with a #space between them. #the blanks variable makes _ the length of the word chosen for the variable secretWord #the next for loop will go through each letter in secretWord and replace the _ with the actual guessed letter. print (HANGMANPICS[len(missedLetters)]) print ()

print('Missed letters:', end=' ')
for letter in missedLetters:
    print(letter, end=' ')
print ()

blanks = '_' * len(secretWord)

for i in range(len(secretWord)):  #replaces blanks with correctly guessed letters

    if secretWord[i] in correctLetters:
        blanks = blanks[:i] + secretWord[i] + blanks[i+1:]

for letter in blanks: #show the secret word with spaces in between each letter.
    print(letter, end=' ')
print()

r/inventwithpython • • May 09 '13

I get an error like "NameError: name 'john' is not defined" for the first program.

4 Upvotes

This is caused by using Python 2 instead of Python 3. The input() function does something different in Python 2 than it does in Python 3.


r/inventwithpython • • Mar 12 '15

Link in 3rd edition does not work.

3 Upvotes

r/inventwithpython • • May 07 '17

Hack for the Sea 2017 :: Come to Gloucester, MA and participate in our maritime hackathon!

Thumbnail
hackforthesea.com
2 Upvotes

r/inventwithpython • • May 10 '13

RSA help

3 Upvotes

Can anyone give me some guidance as to how to leverage the code in the RSA program to decode the message given below? The problem is from the picoCTF competition that finished earlier this week.

Thanks in advance!

p = 9648423029010515676590551740010426534945737639235739800643989352039852507298491399561035009163427050370107570733633350911691280297777160200625281665378483

q = 11874843837980297032092405848653656852760910154543380907650040190704283358909208578251063047732443992230647903887510065547947313543299303261986053486569407

e = 65537

c = 83208298995174604174773590298203639360540024871256126892889661345742403314929861939100492666605647316646576486526217457006376842280869728581726746401583705899941768214138742259689334840735633553053887641847651173776251820293087212885670180367406807406765923638973161375817392737747832762751690104423869019034

Use RSA to find the secret message


r/inventwithpython • • May 10 '13

Programs that got cut from "Hacking Ciphers": The Null Cipher and Null Hacker.

Thumbnail inventwithpython.com
3 Upvotes

r/inventwithpython • • May 09 '17

[Automate] Having trouble with printing formatted table

1 Upvotes

I'm working on the project for chapter 6 of the "Automate the Boring Stuff" (table printer) and I'm having a hard time figuring out how to properly setup my loops to go through the ljust, rjust, and center formatting. I already have code that prints the table in a 'grid" found here but I feel like it's kind of bulky code for what it does.

Can anyone help point me in the right direction to print this properly and maybe clean up my code?


r/inventwithpython • • Dec 10 '16

Looking to add keyboad shortcuts to a program using Python automation?

10 Upvotes

Hi, apologies if this is the wrong place for this, but I'm looking for a little bit of a push about where to start... I have a web based system that I have to use for work, and one major pain is flicking between the different modes, accessed via buttons at the top of the screen. Having a keyboard shortcut for these would make my work a lot quicker to do.

So, I'm a fairly competent programmer in general and have been using Python for 2 or 3 years. My idea would be to have some Python code that listens for the key combination, then instantly simulate movign the mouse to the correct position, click on the icon, then move the mouse back to where it was before. Does that sound feasible? I'm looking at perhaps autopy for the mouse/clicking part.

However, and this is perhaps my inexperience with this - can the Python program listen for keyboard input even though the focus will be on the browser running the web app?

Any further hints or ideas before I disappear down the rabbit hole of getting this working? Thanks in advance :-) (oh, I'm using Windows btw).


r/inventwithpython • • May 10 '13

When will the .mobi version of the book be complete?

4 Upvotes

Sometime soon. Currently I have the books as Word documents and I need a way to convert them to .mobi or .epub formats. Currently the easiest way to do this seems to save the Word docs as HTML, and then use the Kindle publishing software to create epubs from the HTML, though this results in really messy-looking epubs.

If anyone has suggestions for a different workflow please email me at [email protected]


r/inventwithpython • • Dec 03 '16

Automate the Boring stuff Flashcards

4 Upvotes

Does anyone know of flashcards for Automate the Boring Stuff?

I am mostly interested in flashcards that have the functions and methods used in each chapter.


r/inventwithpython • • Feb 23 '18

Sonar.py question, function will never return false?

2 Upvotes

Hi there,

I'm confused about a particular bit of code in the 'Invent Your Own Computer Games' book. Chapter 13 (Sonar game), the text makes reference to the function makeMove returning False if the player makes an invalid move. As near as I can tell though, the function would never return False. Am I missing something? I've included the code below and at this pastebin: https://pastebin.com/ChmKEvdk

def makeMove(board, chests, x, y):
    # Change the board data structure with a sonar device character. Remove treasure chests from the chests list as they are found.
    # Return False if this is an invalid move.
    # Otherwise, return the string of the result of this move.
    smallestDistance = 100 # Any chest will be closer than 100.
    for cx, cy in chests: # For 'value 1' and 'value 2' in each index of the list
        # If each index didn't have two values, this wouldn't work.
        # This isn't a nested loop or anything.
        distance = math.sqrt((cx - x) * (cx - x) + (cy - y) * (cy - y))

        if distance < smallestDistance: # We want the closest treasure chest.
            smallestDistance = distance

    smallestDistance = round(smallestDistance)

    if smallestDistance == 0:
        # xy is directly on a treasure chest!
        chests.remove([x,y])
        return 'You have found a sunken treasure chest!'
    else:
        if smallestDistance < 10:
            board[x][y] = str(smallestDistance)
            return 'Treasure detected at a distance of %s from the sonar device' % (smallestDistance)
        else:
            board[x][y] = 'X'
            return 'Sonar did not detect anything. All treasure chests out of range.'

r/inventwithpython • • May 09 '13

When importing Pygame, I get "ImportError: DLL load failed: The specified module could not be found."

3 Upvotes

This is caused by using Pygame with a 64-bit version of Python. Currently Pygame only works on 32-bit versions of Python.


r/inventwithpython • • May 09 '13

Errors when copying the code? Use the online diff tool to see the difference between your code and the code in the book.

Thumbnail inventwithpython.com
4 Upvotes