PyGame snake game not running in PyCharm - python

I am trying to run a python file for a snake game in PyCharm (Community Edition 2020.2). I am using Python 3.8. When I try to run the file, the Python application tries to open, but is never able to to start the game. I feel like there is something wrong with my interpreter. I used the "which python3" command to figure out where my Python3 was installed and am using that location for my interpreter, but again, the application doesn't open upon running. I have attached a screenshot of my interpreter. Any thoughts on what might be wrong would be great

PyCharm automatically creates a virtual env for your work. That might be the root of your problem. You could look up about that to resolve your problem. I am a bit less familar with virtual-envs. Someone else could give you detailed info but it might be the gist.

Related

Unable to use turtle in Git Bash

I have only just begun to learn to code in Python. I have a bit of experience in HTML, but that's about it.
I am current taking the Intro to Programming nanodegree from Udacity.
The lessons I am working on is introducing students to using interactive mode via command line to test bits of code. The problem is that I have to use the python -i command to get into interactive mode, and I am unable to scroll through the history. The bigger problem, however, is that running things like the turtle module are spotty.
Following the instructions in the lesson, I am trying to run the following code:
import turtle
amy = turtle.Turtle()
amy.forward(100)
amy.left(90)
amy.forward(100)
etc...
What ends up happening is I am able to import the turtle, assign it the name "amy", but then when I try to give it movement commands, nothing happens. the >>> disappears and Git Bash stops responding. I have to close the terminal and start a new terminal everything.
Also, being unable to use the arrow keys to cycle through command history is frustrating and retyping the code over and over again any time a mistake is time consuming.
Are there any better ways to go about using the CLI to fulfill the requirements of the course? (I didn't say which requirements, because I'm not entirely sure to what extent the course will require me to utilize the CLI on upcoming projects.)
This problem has frustrated me so much and I am not able to get any useful information from the "mentors" on the Udacity forums, so I have basically stopped progressing through the course.
Any help is much appreciated.
One possible alternative would be to use, instead of a git bash shell, a true Linux session through WSL2 if you are using a recent enough Windows version.
That way, you would benefit from a regular shell, in which history would be available through arrow keys to cycle through.
I'm doing the same course and had the same problem. Found a solution at https://granatguitar.com/why-can't-i-run-python-in-git-bash-on-windows/
I tested it first from the $ prompt and it seemed to work OK, so then I set it up in .bash_profile.
I didn't have a .bash_profile file so I created one in ~ and edited it as granat suggested.
I added the line alias pythoni="winpty python" and saved the file. (I used pythoni rather than python because, well, it's python interactive.)
Now, when I enter pythoni at the $ prompt it starts python in interactive mode....and it works! All the problems with hanging and not moving the turtles have gone.

How can I open pygame windows outside of PyCharm?

I made a game in pygame. It works perfectly fine when run from pycharm. However, when I want to run it from another location, say command prompt or just windows explorer, all that I get is a black screen that briefly pops up before closing.
I have had the same problems when writing code that uses selenium or other imports. I'm quite sure that the imports are the problem. Code without imports works just fine, regardless from where it's run.
I'm sorry if this is a stupid question with an obvious answer, I'm still a beginner.
Thank you in advance, noble people of StackOverflow.
Try to run this command in cmd (outside the environment):
pip install pygame
Mine works fine and, yes, it is a problem related to the imports.

Python in VSCode: How do you run a previous command?

I'm kind of new to software development. Outside of VSCode, I can open up a terminal (let's say PowerShell), run python in it, type in a command (like 2+2), be able to click the up arrow key to find my previous command so that I can run it again.
If I run PowerShell in VSCode and do the same thing, nothing happens when I click the up arrow where I would expect my previous command to be cycled.
Is this a problem with my Python or VSCode? I've been looking for a solution for this but haven't found many useful topics on this.
This was going to just be a comment but its too long. Sorry it isn't more informative than it is. This is kind of an odd problem because VSCode isn't a true IDE. It doesn't have its own shell and just hijacks your powershell or bash terminal, depending on which OS you are using. You should be able to use your up and down arrows just like you can in powershell. I have tested it on my own VSCode installation and it works fine for me. If it's a problem, it's not with python, since VSCode will interact with the terminal the same way no matter which language you are using it for, so its probably with VSCode or your terminal. I have heard of others having issues with up arrow autocomplete in bash, so if you are connecting to a bash terminal that could be it, but I've never heard of it glitching in powershell. I'd say check which terminal you are using, see if the problem persists when you change terminals, and try reinstalling VSCode if it does. Past that, I don't know what to tell you.
I found a work-around. For me, neither git bash, nor PowerShell allowed up/down arrows for history switching within a python shell. So here it goes.
Ctr-Shift-P opens VSCode commands
Python: Create Terminal does not actuallt start python, but it does launch powershell in a mode that will enable us to succeed
py starts python shell with working up/down arrows!

How to create desktop python game application in Ubuntu 16.04?

I have written a python game using pygame module. It runs fine from terminal.
I have a game directory (let's call it myGame) which contain all the resources(images and sounds) and all the python scripts which use those.
When I run the game through terminal (let's say python3 mainPythonModule.py), the game runs fine.
I tried to create a game.desktop file. But when I click it, a black window appears and crashes almost instantly.
I am using Ubuntu 16.04 LTS. And my game is written in python3.5 and uses pygame module.
My game.desktop file looks like this
I don't want the solution answered for this question because I still have to type in terminal.
I would like to create a desktop applcation, such that by clicking on the icon, the game would start.
Just like when we download any game, after installation, we just click on the game icon and it runs, I want something like that. How can I do that?
Edit :
I don't think this question is a possible duplicate of this question.
I want to know how to develop a desktop application in Ubuntu 16.04, such that by clicking on some icon, the game would start.
Final Edit :
First of all, I would like to thank everyone who responded. I really am sorry for wasting your precious time.
The issue is resolved. The problem was all the resource (images and sounds) were loaded via relative path and not through absolute path. Now the game.desktop file runs fine.
I think what are you trying to do is some kind of launcher.
Here you have my repo, it contains a Tkinter GUI with a PyGame call on a button.
Feel free to study/use it as you want, look at the file desktop_application.py and if you don`t care about the coin part you can just skip that and take what you need.
https://github.com/FilippoLeone/PyGameLauncher
And you can compile everything as an executable with PyInstaller.
Install PyInstaller from PyPI:
pip install pyinstaller
Go to your program’s directory and run:
pyinstaller yourprogram.py

IDLE 3.4.3 crashes when running module

I've recently started to use Python, and I don't know much about programming.
I've installed Python 3.4.3 in my Mac OS X Yosemite, and it worked fine for a few months, but now every time I want to run a module it stops responding.
so I use IDLE and when I open a new file to define a function I can write, then I click run and I save my file, and then when I click save IDLE doesn't restart as it use to and I can't write on it or do anything else. there is no crash report, no nothing, just all frozen. I close it and if then I re open and write on IDLE it works but as soon as I try to run a new file again it freezes.
How can I solve it?
to try stuff out i've been using repl.it online and it is useful for a learner like me but still I would like to be able to use IDLE on my mac.
THANK YOU FOR YOUR HELP!!!!
Try using Anaconda, which is another Python distribution, but more science-oriented. It has all the commonly required modules already in it (plus other for scientific purposes), so that you don't have to install them as and when required. And when you install Anaconda, it automatically installs Spyder, which is a much better IDE than IDLE.

Categories

Resources