pygame: Bouncing rocket - python

I am just learning how to use pygame.
But whenever I run the code, it doesn't run but a python rocket appears and starts bouncing.
The only thing I can do on the rocket is force quit.
My python is 3.8.2 and pygame is 1.9.6, on my mac, if it helps.
This is the code I'm trying to run, just a basic set up code:
import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")
x = 50
y = 50
width = 40
height = 60
vel = 5
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
pygame.display.update()
pygame.quit()

I just ran your code using python 3.7 on a macbook Mojave and I just got a red rectangle (like your code specifies) in one position. Possibly try restarting your computer, checking you version of python, or renaming the file (originally I named the file pygame and it was giving me a bunch of issues) to make it run the correct thing.

Jubin- I just ran your code on Mac and it works. I think the trouble is that IDLE won't allow it to run for some reason.
Try running it from the command line using Terminal app. It runs fine for me doing that, but will not run from IDLE.
Open Terminal
Navigate your way to the folder that contains your file
Type "python filename.py"

Related

Why is a Drag and Drop event on Pygame not allowed?

I have a simple Pygame display:
pygame.init()
screen = pygame.display.set_mode((1024, 576))
clock = pygame.time.Clock()
while True:
for event in pygame.event.get(): # to handle clicks on the screen (prevent crash)
if event.type == pygame.QUIT:
pygame.display.quit()
if event.type == pygame.DROPFILE:
path = event.file
print(path)
pygame.display.update()
I'm currently testing the "drop file" event to use it in a project I'm working on. Unfortunately, when I drag a file onto the screen, the cursor turns into a "not allowed" sign and nothing happens when I drop the file. Why does it happen?
Without changing your code much (adding 'import pygame'), it doesn't work for me either. I droped a file and then the same happend for me what happend for you. Thats what I thought.
I first tried Python 3.8.6 with Pygame 1.9.6. Then I remembered, that I have an other installation of Python with 3.9.1 and Pygame version 2.0.0.
This second combination worked for me. I don't know which part made the difference in the end, but I think they did much work for pygame 2.0.0, so give it a try.
This works for me on Windows 10.

Pycharm - pygame shows only black screen

Here is the code
import pygame
pygame.init()
screen_width = 480
screen_height = 640
screen = pygame.display.set_mode((screen_width, screen_height))
background = pygame.image.load("D:/Python/Pygame_200830/Background.png")
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#screen.fill((0, 0, 255))
screen.blit(background, (0, 0))
pygame.display.update()
pygame.quit()
if I open this with VS code, it works well but, when I do this with Pycham. I can't see "backgound.png"
it's just only black display... and the only difference is IDE program.
I already added external function or something... only that backgound is the problem.
Could you let me know what is the reason...?
It is likely that the root cause of your problem is from Pycharm configurations or venv. see if you can open the python console. If you're new to pycharm, you can find the 'python console' option at the bottom of the screen.
Also here's a link that might help you. try some of the solutions there:
https://www.reddit.com/r/learnpython/comments/au6lvd/pycharm_code_works_but_doesnt_display_output/

Screen not showing on Mac using pygame

I'm doing a tutorial for using pygame to make games, and I'm having a problem getting a screen to show up. Here is what I have so far:
import pygame
# Initialize the pygame
pygame.init()
# Create the screen, height = 800, width = 600
screen = pygame.display.set_mode((800, 600))
# Game Loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
I have pygame installed, and have no errors or warnings in PyCharm. When I run this (both in PyCharm and my terminal), I get no screen showing, and all that happens is the spaceship of the python runner is bouncing in my dock. When I run this exact code on my pc, it shows a black screen, which is the desired outcome.
Can anyone help, or am I doing something wrong?
Thank you for your assistance.
I have no problem running your code on a Mac. However, the window showing up is a grey one, not black.
In order to force the display of the window, you can add this after the screen creation:
pygame.display.update()
And to try to figure out the problem,you may want to add a text in the window bar, that may generate a message pointing to your problem.
pygame.display.set_caption("Hello world")

Sublime text 3, pygame display is empty

I recently installed sublime text 3 and have been moving over from pycharm to sublime but have run into some difficulties with pygame. I installed python 3 and got it working, then fresh installed pygame, but when I brought over some code and ran it I got an empty pygame screen. Which looked like this:
I was filling the screen black and drawing a red rectangle yet none of it displayed. I am sure the program works because I tested it on both pygame and repl.it. But in case it would help here is the code for the project:
import pygame
import sys
pygame.init()
display_width = 600
display_height = 600
screen = pygame.display.set_mode((display_width, display_height))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 0, 0), ((300, 300), (20, 20)))
pygame.display.update()
I think it has to do with something I am not setting up with sublime. Help appreciated. Thx.
It turns out pygame is not compatible with Mac OS Mojave so you need to use the new dev build to get it to work.
Use pip install pygame==2.0.0.dev4

Pygame display not responding

I'm slowly trying to get to know pygame and write my first game in it and honestly, I didn't expect problems so early. So far I've only set a display, that is supposed to be there indefinitely (I just wanted to try it out):
import pygame
pygame.init()
(width, height) = (1000, 700)
screen = pygame.display.set_mode((width, height))
while True:
pygame.display.flip()
But when the window appears it says it's "not responding". I tried deleting the loop so that display would just blink once and vanish, because programm would die immiedately after it's created, but I get the same "not responding" window. I'm using pygame 1.9.2 and python 3.5. I wonder if the trouble may be because of anaconda - the window is opened as subcart for anaconda by default.
Edit: So far I discovered that when I open it not from spyder, but just click on a file it works just fine. Is there any way to make it work by simple run and compile while in spyder or it's just how it's supposed to work?
Add this to your loop. For me the only time it isnt responding is when I click the X and this could be to do with the fact that pygame doesn't know what to do when that happens.
import sys
for evt in pygame.event.get():
if evt.type == pygame.QUIT:
pygame.quit()
sys.exit()
#Try This
import pygame
(width, height) = (1000, 700)
screen=pygame.display.set_mode((width, height))
pygame.display.update()
while True:
for event in pygame.event.get():``
if event.type == pygame.QUIT:
pygame.quit()
quit()

Categories