I am trying to create an exe file for my pygame game with pyinstaller. The command I am using is pyinstaller --noconsole 2d_minecraft.py. It creates the dist folder successfully.
I am getting this error after a successful execution of the exe file created. This does not happen without the --noconsole option.
code:
import pymc
import pygame
import random
import perlin_noise
pygame.init()
WIN = pygame.display.set_mode((960, 640))
WIN_WIDTH, WIN_HEIGHT = WIN.get_width(), WIN.get_height()
WORLD_WIDTH, WORLD_HEIGHT = 64, 64
FPS = 60
pymc.set_title("2d Minecraft")
class objects():
to_draw = []
run = True
cam_y = (WORLD_HEIGHT/2)*64
cam_x = (WORLD_WIDTH/2)*64
block_textures = {
[...]
}
class tile():
[...]
def handle_keys():
[...]
def generate_world():
[...]
def main():
clock = pygame.time.Clock()
tiles = generate_world()
while objects.run:
clock.tick(FPS)
objects.to_draw = []
for event in pygame.event.get():
if event.type == pygame.QUIT:
objects.run = False
pygame.quit()
exit()
for x in range(0, WORLD_WIDTH):
for y in range(0, WORLD_HEIGHT):
objects.to_draw.append(tiles[x][y])
handle_keys()
pymc.draw_window(WIN, objects.to_draw, fill=(0, 150, 255))
main()
main()
I figured it out!! All I needed to do was change exit() to sys.exit() (and add import sys as well).
Related
I'm having a problem. I want to load and play a video in pygame but it doesn't start. The only thing that I am seeing is a black screen. Here is my code:
import pygame
from pygame import display,movie
pygame.init()
screen = pygame.display.set_mode((1024, 768))
background = pygame.Surface((1024, 768))
screen.blit(background, (0, 0))
pygame.display.update()
movie = pygame.movie.Movie('C:\Python27\1.mpg')
mrect = pygame.Rect(0,0,140,113)
movie.set_display(screen, mrect.move(65, 150))
movie.set_volume(0)
movie.play()
Can you help me??
The pygame.movie module is deprecated and not longer supported.
If you only want to show the video you can use MoviePy (see also How to be efficient with MoviePy):
import pygame
import moviepy.editor
pygame.init()
video = moviepy.editor.VideoFileClip("video.mp4")
video.preview()
pygame.quit()
An alternative solution is to use the OpenCV VideoCapture. Install OpenCV for Python (cv2) (see opencv-python). However, it should be mentioned that cv2.VideoCapture does not provide a way to read the audio from the video file.
This is only a solution to show the video but no audio is played.
Opens a camera for video capturing:
video = cv2.VideoCapture("video.mp4")
Get the frames per second form the VideoCapture object:
fps = video.get(cv2.CAP_PROP_FPS)
Create a pygame.time.Clock:
clock = pygame.time.Clock()
Grabs a video frame and limit the frames per second in the application loop:
clock.tick(fps)
success, video_image = video.read()
Convert the camera frame to a pygame.Surface object using pygame.image.frombuffer:
video_surf = pygame.image.frombuffer(video_image.tobytes(), video_image.shape[1::-1], "BGR")
See also Video:
Minimal example:
import pygame
import cv2
video = cv2.VideoCapture("video.mp4")
success, video_image = video.read()
fps = video.get(cv2.CAP_PROP_FPS)
window = pygame.display.set_mode(video_image.shape[1::-1])
clock = pygame.time.Clock()
run = success
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
success, video_image = video.read()
if success:
video_surf = pygame.image.frombuffer(
video_image.tobytes(), video_image.shape[1::-1], "BGR")
else:
run = False
window.blit(video_surf, (0, 0))
pygame.display.flip()
pygame.quit()
exit()
You are not actually blitting it to a screen. You are also not utilizing a clock object so it will play as fast as possible. Try this:
# http://www.fileformat.info/format/mpeg/sample/index.dir
import pygame
FPS = 60
pygame.init()
clock = pygame.time.Clock()
movie = pygame.movie.Movie('MELT.MPG')
screen = pygame.display.set_mode(movie.get_size())
movie_screen = pygame.Surface(movie.get_size()).convert()
movie.set_display(movie_screen)
movie.play()
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
movie.stop()
playing = False
screen.blit(movie_screen,(0,0))
pygame.display.update()
clock.tick(FPS)
pygame.quit()
I just got that MELT.MPG from the link provided in the comment. You should be able to simply switch out that string for your actual MPG you want to play and it will work... maybe.
As you probably know, the pygame.movie module is deprecated and no longer exists in the latest version of pygame.
An alternative would be to read in frames of the video one by one and blit them onto the pygame screen using the the cv2 module (OpenCV), that can be installed with the command prompt command:
pip install opencv-python
Then, you can run the code:
import cv2
import pygame
cap = cv2.VideoCapture('video.mp4')
success, img = cap.read()
shape = img.shape[1::-1]
wn = pygame.display.set_mode(shape)
clock = pygame.time.Clock()
while success:
clock.tick(60)
success, img = cap.read()
for event in pygame.event.get():
if event.type == pygame.QUIT:
success = False
wn.blit(pygame.image.frombuffer(img.tobytes(), shape, "BGR"), (0, 0))
pygame.display.update()
pygame.quit()
I am working on a pygame space invaders game. When I try to blit the background image it doesn't work. I am fairly new to pygame so I don't know what to do. I tried downloading another picture and using that but the problem persists.
import pygame
import os
import time
import random
import math
import sys
pygame.init()
screen = pygame.display.set_mode((750,750))
pygame.display.set_caption("Space Invaders")
FPS = 60
clock = pygame.time.Clock()
#Ships
RED_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_red_small.png'))
GREEN_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_green_small.png'))
BLUE_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_blue_small.png'))
YELLOW_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_yellow.png'))
#Lasers
RED_LASER = pygame.image.load(os.path.join('assets','pixel_laser_red.png'))
BLUE_LASER = pygame.image.load(os.path.join('assets','pixel_laser_blue.png'))
YELLOW_LASER = pygame.image.load(os.path.join('assets','pixel_laser_yellow.png'))
GREEN_LASER = pygame.image.load(os.path.join('assets','pixel_laser_green.png'))
#Background
BG = pygame.transform.scale2x(pygame.image.load(os.path.join('assets','background-black.png')).convert_alpha())
def main():
run = True
def redraw_window():
screen.blit(BG, (0, 0))
pygame.display.update()
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(BG, (0,0))
pygame.display.update
clock.tick(120)
pygame.quit()
You need parenthesis when calling display update:
screen.blit(BG, (0,0))
pygame.display.update() # need parenthesis
clock.tick(120)
So I have a file in the same folder as the file i'm coding right now and when the code runs in pycharm, if I press left arrow the other file is opened, but if I open the file using python straight from the file location it just opens and closes. Here is my code:
import pygame
import sys
import random
import subprocess
pygame.init()
GUI = pygame.display.set_mode((800,600))
pygame.display.set_caption("The incredible guessing game")
x = 284
y = 250
width = 68
length = 250
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run =False
if event.type == pygame.KEYDOWN:
command = "python AhjaiyGame.py"
subprocess.call(command)
pygame.draw.rect(GUI, (255,210,0), (x,y,length,width))
pygame.display.update()
pygame.quit()
Simply put, the program exists because it has completed running.
Python is tabulation based, and in the code you posted, the while loop is not actually doing anything.
You need to indent the code you need looped:
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run =False
if event.type == pygame.KEYDOWN:
command = "python AhjaiyGame.py"
subprocess.call(command)
pygame.draw.rect(GUI, (255,210,0), (x,y,length,width))
pygame.display.update()
pygame.quit()
Notice the tabulation.
In this example, pygame.quit() will only be called when run becomes False and the while loop completes.
import pygame
import sys
import random
import subprocess
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("The incredible guessing game")
x = 40
y = 30
width = 10
height = 20
vel=0.1
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run =False
keys=pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x-=vel
if keys[pygame.K_RIGHT]:
x+=vel
if keys[pygame.K_UP]:
y-=vel
if keys[pygame.K_DOWN]:
y+=vel
win.fill((0,0,0))
pygame.draw.rect(win, (255,0,0), (x,y,width,height))
pygame.display.update()
pygame.quit()
I'm trying to display an image using pygame, but I get this error:
Traceback (most recent call last):
File "H:/profile/desktop/untitled/venv/Scripts/AhjaiyGame.py", line 28, in
start = pygame.image.load(os.path.join(folder, "wecvguh.png"))
pygame.error: Couldn't open H:\profile\desktop\untitled\venv\Scripts\wecvguh.png
Code block:
import sys
import random
import os
import subprocess
import pygame
pygame.init()
GUI = pygame.display.set_mode((800,600))
pygame.display.set_caption("The incredible guessing game")
x = 284
y = 250
width = 68
length = 250
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run =False
if event.type == pygame.KEYDOWN:
command = "python AhjaiyCPT.py"
subprocess.call(command)
pygame.display.update()
folder = os.path.dirname(os.path.realpath(__file__))
start = pygame.image.load(os.path.join(folder, "wecvguh.png"))
def img(x,y):
gameDisplay.blit(start, (x,y))
while run:
gameDisplay.fill(white)
start(x, y)
pygame.quit()
The code has two "run" loops, so it never gets to the second loop.
The code's indentation is confused - maybe from a paste into SO?. The overwhelming majority of programmers use 4 spaces to indent. This is probably a good custom to follow.
The code also loaded the "start" image every loop iteration, this is unnecessary (unless it changes on disk, in this case use os.stat() to check for changes before re-loading it).
Re-worked main loop:
folder = os.path.dirname(os.path.realpath(__file__))
start = pygame.image.load(os.path.join(folder, "wecvguh.png"))
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
command = "python AhjaiyCPT.py"
subprocess.call(command)
gameDisplay.fill(white)
gameDisplay.blit(start, (x,y))
pygame.display.update()
pygame.quit()
I'm kinda new to asyncio and async def/await syntax, so I wanted to ask how exactly should I do something like this:
import asyncio
import pygame
import logging
from pygame import *
log = logging.getLogger('')
class Client:
def __init__(self, host, port):
self.host = host
self.port = port
self.loop = asyncio.get_event_loop()
self.loop.run_until_complete(self.create_client())
async def create_client(self):
self.reader, self.writer = await asyncio.open_connection(self.host,
self.port,
loop=self.loop)
asyncio.ensure_future(self._handle_packets(), loop=self.loop)
async def _handle_packets(self):
while True:
data = await self.reader.read(4096)
if not data:
continue
message = data.decode()
log.debug("(NET) Received "+message)
def send(self, data):
self.loop.run_until_complete(asyncio.ensure_future(self._send(data),
loop=self.loop))
async def _send(self, data):
self.writer.write(data)
await self.writer.drain()
print("_send done")
def disconnect(self):
print("DC")
self.loop.close()
def main():
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Pyond client")
bg = Surface((640, 480))
bg.fill(Color("#004400"))
client = Client('127.0.0.1', 2508)
while True:
pygame.event.pump()
for e in pygame.event.get():
if e.type == QUIT:
raise SystemExit
elif e.type == KEYUP:
if e.key == K_UP:
client.send(b"{'Hello':'World'}")
screen.blit(bg, (0, 0))
pygame.display.update()
client.disconnect()
if __name__ == "__main__":
main()
This code creates 640x480 window with pygame, then reads for incoming K_UP (up arrow) key. Upon pressing, sends json-like string to the server. _handle_packets was supposed to read any incoming data from server and just print it.
I was testing this code and sending works okay, but receiving is quite delayed. I'm sure I need to put handler somewhere else, so where exactly?
And btw, send works only once. Need help on this one too.
A couple of problems here.
The first one is pretty fundamental. The asycnio event loop stops running after create_client() finishes, and only runs again while you send() data. So, the only time it is able to run _handle_packets is when you are send()ing. Ideally, you should start the event loop once in a higher scope and close it once you are done with everything.
The second problem is that whenever you client.send(b"{'Hello':'World'}"), you will block the outer pygame while True loop, preventing any other events to be processed until the previous one is sent. You should make use of a asyncio.Queue to queue the events & send them from the Client class.
Here are some changes I would make (sorry, untested; I don't have pygame installed ATM):
# vim: tabstop=4 expandtab
import asyncio
import pygame
import logging
from pygame import *
log = logging.getLogger('')
class Client:
def __init__(self, host, port, loop):
self.host = host
self.port = port
self.loop = loop
self.send_q = asyncio.Queue()
async def connect(self):
self.reader, self.writer = await asyncio.open_connection(self.host,
self.port,
loop=self.loop)
self.loop.create_task(self._handle_packets())
self.loop.create_task(self._send())
async def _handle_packets(self):
while True:
data = await self.reader.read(4096)
if not data:
continue
message = data.decode()
log.debug("(NET) Received "+message)
def send(self, data):
self.send_q.put_nowait(data)
async def _send(self):
while True:
data = await self.send_q.get()
self.writer.write(data)
await self.writer.drain()
def disconnect(self):
print("DC")
self.writer.close()
async def main(loop):
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Pyond client")
bg = Surface((640, 480))
bg.fill(Color("#004400"))
client = Client('127.0.0.1', 2508, loop)
await client.connect()
while True:
pygame.event.pump()
for e in pygame.event.get():
if e.type == QUIT:
raise SystemExit
elif e.type == KEYUP:
if e.key == K_UP:
client.send(b"{'Hello':'World'}")
screen.blit(bg, (0, 0))
pygame.display.update()
client.disconnect()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
loop.close()
Another important thing to keep in mind is that you should never block the asyncio event loop with pygame, otherwise the Client network processing in the background will stall. I've never used pygame, so I'm not familiar with which pygame function might be "blocking", but they should be called using result = await loop.run_in_executor(None, blocking_func, *func_args). This will call blocking functions in another thread.