Image tracing path with mouse instead of just moving with the mouse - python

When I drag the mouse over the image, the image traces a path with the mouse instead of moving. See the screenshot for an example of this behavior:
Here is the code:
import pygame
import sys
from pygame.locals import *
pygame.display.init()
clock=pygame.time.Clock()
canvas=pygame.display.set_mode((500,500))
pygame.display.set_caption("hy")
color=(255,0,0)
canvas.fill(color)
pygame.mouse.set_visible(1)
pygame.mouse.set_cursor(*pygame.cursors.diamond)
img=pygame.image.load("maryo.png")
while True:
x,y=pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key==K_ESCAPE:
pygame.quit()
sys.exit()
canvas.blit(img,(x,y))
clock.tick(60)
pygame.display.update()

If you donĀ“t want a tracing path, you need to clear the screen before you blit the img-surface onto a new position in your main game loop.
This can be done by calling the fill() method of the main screen object canvas:
while True:
x,y=pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type==KEYDOWN:
if event.key==K_ESCAPE:
pygame.quit()
sys.exit()
canvas.fill(color) #clear the screen
canvas.blit(img,(x,y)) #blit img onto screen
clock.tick(60)
pygame.display.update()

Related

Pygame window closes instantly

My pygame window closes without any errors right after I run the code
Here's my code:
import pygame
pygame.init() # initialises pygame
win = pygame.display.set_mode((500, 500)) # width, height
pygame.display.set_caption("Project_01")
If anyone can help, thank you in advance :)
See Why is my PyGame application not running at all? Your application will exit right after the window is created. You have to implement an application loop. You must implement an application loop to prevent the window from closing immediately:
import pygame
pygame.init() # initialises pygame
win = pygame.display.set_mode((500, 500)) # width, height
pygame.display.set_caption("Project_01")
clock = pygame.time.Clock()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill((0, 0, 0))
# draw scene
# [...]
pygame.display.flip()
pygame.quit()

I want to create pygame window but it kept saying "video system not initialized" in VS Code

I am using visualcodestudio, but if I run using python IDLE, it runs.
import pygame, sys
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Space Invasion")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
You need to initialize using pygame.init() first before going into the loop. Also, I replaced sys.exit() with pygame.quit() as sys.exit() causes the program to not respond with attempting to close the program.
import pygame, sys
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Space Invasion")
run_game()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.flip()
Or, you could put the loop inside the run_game() function as import random suggested.
import pygame, sys
def run_game():
pygame.init()
screen = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Space Invasion")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.flip()
run_game() # Run the game

Python Crash Course: "pygame.error: video system not initialized" [duplicate]

This question already has answers here:
What is the difference between .quit and .QUIT in pygame
(2 answers)
pygame.error: video system not initialized
(5 answers)
Closed 1 year ago.
I'm trying to do one of the projects in the Python Crash Course book: Alien Invasion Chapter 12. I just started and for some reason the error: pygame.error: video system not initialized keeps popping up. I'm pretty sure I followed the directions appropriately so I don't know what I have possibly done wrong...?
import sys
import pygame
from settings import Settings
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption('Alien Invasion')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
screen.fill(ai_settings.bg_color)
pygame.quit()
sys.exit()
pygame.display.flip()
run_game()
The error occurs because pygame.event.get is called before you initialize the display with pygame.display.set_mode.
It's very important in Python to indent your code correctly. Your program should most likely look similar to this version:
import sys
import pygame
from settings import Settings
def run_game():
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption('Alien Invasion')
# This block should be in the `run_game` function.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Fill the screen in the while loop not in the event
# loop. It makes no sense to fill the screen only when
# the user quits.
screen.fill(ai_settings.bg_color)
pygame.display.flip()
run_game()

Window freeze after pygame program runs

I am learning pygame and have been working on examples given in 'Python Crash Course' book by Eric Matthews (https://g.co/kgs/WP5fay). I have installed pygame version 1.9.3 on macOS High Sierra.
When I run the following program, a window opens and when I click 'X' to close it, the window freezes and the curses keeps circling and I get a python not responding error in activity monitor. I have tried a number of option to fix the problem but it is not going away.
import pygame
import sys
def run_game():
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Alien Invasion')
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game() #when I run this function, a window opens and freezes a couple of seconds after
You have to terminate the application loop and uninitialize all pygame modules with pygame.quit():
def run_game():
pygame.init()
screen = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Alien Invasion')
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.flip()
pygame.quit()
sys.exit()

How do I maximize the display screen in PyGame?

I am new to Python programming and I recently started working with the PyGame module. Here is a simple piece of code to initialize a display screen. My question is : Currently, the maximize button is disabled and I cannot resize the screen. How do I enable it to switch between full screen and back?
Thanks
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300))
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
To become fullscreen at native resolution, do
DISPLAYSURF = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
To make the window resizeable, add the pygame.RESIZABLE parameter when seting the mode. You can set the mode of the screen surface multiple times but you might have to do pygame.display.quit() followed by pygame.display.init()
You should also check the pygame documentation here http://www.pygame.org/docs/ref/display.html#pygame.display.set_mode
The method you are looking for is pygame.display.toggle_fullscreen
Or, as the guide recommends in most situations, calling pygame.display.set_mode() with the FULLSCREEN tag.
In your case this would look like
DISPLAYSURF = pygame.display.set_mode((400, 300), pygame.FULLSCREEN)
(Please use the pygame.FULLSCREEN instead of just FULLSCREEN because upon testing with my own system FULLSCREEN just maximized the window without fitting the resolution while pygame.FULLSCREEN fit my resolution as well as maximizing.)
import pygame, os
os.environ['SDL_VIDEO_CENTERED'] = '1' # You have to call this before pygame.init()
pygame.init()
info = pygame.display.Info() # You have to call this before pygame.display.set_mode()
screen_width,screen_height = info.current_w,info.current_h
These are the dimensions of your screen/monitor. You can use these or reduce them to exclude borders and title bar:
window_width,window_height = screen_width-10,screen_height-50
window = pygame.display.set_mode((window_width,window_height))
pygame.display.update()
This will let you toggle from maximize to the initial size
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
#Below line will let you toggle from maximize to the initial size
DISPLAYSURF = pygame.display.set_mode((400, 300), RESIZABLE)
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
You'd have to add the full screen parameter onto the display declaration like this:
import pygame, sys
from pygame.locals import *
pygame.init()
#Create a displace surface object
DISPLAYSURF = pygame.display.set_mode((400, 300), FULLSCREEN)
mainLoop = True
while mainLoop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainLoop = False
pygame.display.update()
pygame.quit()
Just use pygame.RESIZABLE as an easier option.

Categories