I´m a newbie on pygame, and i have this code for trying to do kind of a startscreen for a football game, this start screen has a static background image that is"img_start_screen" and the logo of the game that I want it to have a little animation that stops when it reaches the certain point on the screen, that logo is the variable "logo"
import pygame,sys
from pygame.locals import *
pygame.init()
screen=pygame.display.set_mode((1280,720))
pygame.display.set_caption("CEFoot 3.0")
FPS=60
fpsClock=pygame.time.Clock()
img_start_screen=pygame.image.load("start_screen.gif")
logo=pygame.image.load("logo8bit.png")
logox=390
logoy=0
direction="down"
while True:
window.blit(img_start_screen,(0,0))
if direction == "down":
logoy+=5
if logoy==300:
direction="center"
if direction == "center":
pass
screen.blit(logo,(logox,logoy))
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update
fpsClock.tick(FPS)
The problem is that when I run the code it starts as a black screen and not until I move the window "out" of the Windows screen it refreshes, the images aren´t the problem as I ran the code without them and it showed the same black screen. I´m beginning to think that it isn´t code problem but compability as I´m running Python 3.2 and Pygame 1.9(I think) the last version that is really old , all this on Windows 10, so this might be the problem.Thanks in advance.
As you know pygame.display.update is a function
as seen if you print it:
>>> print pygame.display.update
<built-in function update>
so when you call pygame.display.update without parenthesis () you are merely stating a reference to a function. Whereas if you use parenthesis you call the function:
>>> print pygame.display.update()
None
this calls the function and returns None
Related
I've read everything I could find about the mouse in pygame and yet I miss something.
The device has (and will not) have a mouse attached so I need to remove the cursor from the game surface, ideally even before it draws the first screen.
But no matter what I try I always have a big oppalin cursor in the middle of the screen.
My current function is this :
# initialize the pygame module
pygame.init()
# create a surface on screen that has the size of 240 x 180
screen = pygame.display.set_mode((160,80))
# define a variable to control the main loop
running = True
pygame.display.flip()
# main loop
while running:
screen.fill((0, 0, 0))
# hide cursor
pygame.mouse.set_pos((160,80)) # my screen is actually 160 x 80 px so this should hide it by pushing it over the edge
pygame.mouse.set_visible(False) # this should hide the mouse plain and simple
# Here I do other unrelated (but working) stuff, like displaying images and text
# event handling, gets all event from the event queue
for event in pygame.event.get():
# only do something if the event is of type QUIT
if event.type == pygame.QUIT:
# change the value to False, to exit the main loop
running = False
pygame.display.flip()
And the mouse is still on screen, sometimes after a while it disappears.
I tried other method like making the cursor transparent, but that crash the app.
Note that I'm running Pygame on an Alpine Linux with direct output to the framebuffer on a Raspberry Pi. There's no mouse attached to the device but if I connect one I can move the cursor around.
Pygame is Version 2.1.2 (compiled and installed by pip)
Python is 3.10.0
Any help or pointer would be greatly appreciated.
(sorry if it's a dumb question; I'm a total noob with python/pygame)
Just write the set_visible(False) outside the main loop, you don't need to call this every frame:
pygame.init()
screen = pygame.display.set_mode((160,80))
pygame.mouse.set_visible(False) # Hide cursor here
running = True
pygame.display.flip()
# main loop
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# unrelated with your question but I also suggest that you clear your background inside the loop, at the contrary you likely will need this at each frame
screen.fill((0, 0, 0))
# ... your code, blitting images etc
pygame.display.flip()
i typed the below code in my sublime text, but as this code is used by people to change the background color; i hope it would happen, but, unfortunately it didn't. i.e precisely color didn't change. i
then i started to research in stackoverflow.
some questions like this in (mac os ) has been already asked.
though, you people say that that question will answer my question, it does to some level; but I would love to get the answer of this question. Why does my Tkinter window background not change?
likewise a similar question was asked, like this; Python tkinter window background/canvas not changing colour
import sys
import pygame
def run_game():
#initialize game and create a screen object.
pygame.init()
screen = pygame.display.set_mode((1200,800))
pygame.display.set_caption("Alien Invasion")
#set the background color.
bg_color=pygame.color.Color('#FFFFF')
# start the main loop for the game.
while True:
#watch for keyboard and mouse events.
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
#redraw the sceen during each pass through the loop
screen.fill(bg_color)
#make most recently drawn screen visible
pygame.display.flip()
run_game
I just found out about pygame.surface.scroll() and what I understand from the pygame documents that scroll() is for moving surface without the need to rebuild the background again to cover the old surface, just like pygame.rect.move_ip() but for surfaces.
Anyway, I don't know how to use it and the examples in the pygame documents are hard to me to understand as long as I am beginner and, after searching for long time, I couldn't found anything useful to understand how to use it.
Here is my code.
import pygame
from pygame.locals import*
screen=pygame.display.set_mode((1250,720))
pygame.init()
clock=pygame.time.Clock()
boxx=200
boxy=200
image = pygame.Surface([20,20]).convert_alpha()
image.fill((255,255,255))
while True :
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT :
pygame.quit()
quit()
image.scroll(10,10)
screen.blit(image,(boxx,boxy))
pygame.display.update()
clock.tick(60)
EDIT: Your image and screen variables are backwards. That is also causing you some confusion I'm sure..
Your problem may is that you are trying to scroll an all black background. It is probably scrolling, and you just don't know it because the white box you used blit() to draw on the screen is stationary.
Try using something you can see scroll, like an image file. If you wanna move the white box, you can add a counter as a speed variable. Read this, then run it.
import pygame
from pygame.locals import*
screen=pygame.display.set_mode((1250,720))
pygame.init()
clock=pygame.time.Clock()
boxx=200
boxy=200
image = pygame.Surface([20,20]).convert_alpha()
image.fill((255,255,255))
speed = 5 # larger values will move objects faster
while True :
screen.fill((0,0,0))
for event in pygame.event.get():
if event.type==pygame.QUIT :
pygame.quit()
quit()
image.scroll(10,10)
# I did modulus 720, the surface width, so it doesn't go off screen
screen.blit(image,((boxx + speed) % 720, (boxy + speed) % 720))
pygame.display.update()
clock.tick(60)
I can't say for sure the scroll function is working or not, learn to use an image as your background so you can see it moving first.
I have a pygame window embedded in a a frame in tkinter. In another frame I have a button which calls the following function when clicked:
def setStart():
global start
# set start position
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
start = event.pos
print("start",start)
break
I am intending for the program to print the position of the place where the user clicked on the pygame surface after the button is clicked. However on the first click of the button and the following click of the pygame surface there is no output. It is on the second click of the button before the corresponding second click on the pygame surface that python prints out an output like :
('start', (166, 115))
How can I get it to give me a result right after the click on the pygame surface? I had the same problem when I had two seperate tkinter and pygame windows so the embedding of pygame into tkinter is unlikely to be the cause of the problem.
EDIT: after further testing it appears that if the button is pressed and then the pygame surface is clicked on multiple times, upon a second click of the button the coordinates of all of these clicks are printed out as a batch.
In the most basic form here's how you do it in pygame:
import pygame
pygame.init()
screen = pygame.display.set_mode((100, 100))
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP: # or MOUSEBUTTONDOWN depending on what you want.
print(event.pos)
elif event.type == pygame.QUIT:
quit()
pygame.display.update()
More information on how to handle pygame events can be found in the docs.
And here's how you do it in tkinter:
try:
import tkinter as tk # Python 3
except ImportError:
import Tkinter as tk # Python 2
root = tk.Tk()
def print_pos(event):
print(event.x, event.y)
root.bind("<Button-1>", print_pos)
root.mainloop()
More information on tkinter events can be found in effbots documentation.
I would suggest not putting break in an event loop in pygame. Doing so makes all other events go un-handled, meaning that it's possible for the program to not respond to certain events.
Your "EDIT: [...]" is unfortunately wrong, given the code you've given us. I had no problem with the code; it printed the position of where I released the mouse button and always printed just one position. So there have to be a logical error somewhere else in your code.
First I have to say that I don't know anything about pygame. However, if you are already using Tkinter, I could help you maybe: I would define a new function (let's call it mouse_click). In the button-click-function I would bind the new function to the game's surface. In the new function I print out the current mouse position:
def button_click(self):
game_surface.bind("<Button-1>", self.mouse_click)
def mouse_click(self, event):
print "X:", event.x
print "Y:", event.y
I hope this is helpful. Please notice that you should modify this code to make it work in your program (using the correct widget names and so on).
By the way, "Button-1" is the event identifier of the left mouse button.
I have a project in Pygame 1.9.2 where I reinitialize the display multiple times, and draw text to the display surface. It works fine until I close the Pygame display and re-initialize it again.
It follows a structure like this:
from pygame import *
init() # this is pygame.init()
running = True
while running:
display.set_mode((800,600))
visible = True
textFont = font.SysFont("Comic Sans MS", 12)
while visible:
for evt in event.get():
if evt.type == QUIT:
visible = False
if evt.type == KEYDOWN:
if evt.key == K_ESCAPE:
visible = running = False
textPic = textFont.render("Hello world!", True, (255,255,255))
display.get_surface().blit(textPic, (0,0))
display.flip()
quit()
This program works until the display is closed for the first time and then reinitialized, after which I receive the following error when trying to use textFont.render:
pygame.error: Text has zero width
I'm tearing my hair out trying to figure out what's wrong... I know that "Hello world!" has a width greater than zero. How do I fix this problem?
The problem is that pygame.quit() was called before the program was finished. This causes every Pygame module (e.g. pygame.font) to be uninitialized.
Instead of relying on pygame.quit to close the Pygame display, use pygame.display.quit at the end of every while running loop. Then put pygame.quit at the very end of the script to unload the rest of the modules after they're done being used.
(Calling pygame.init() again before rendering text won't fix this issue either, because it causes the Pygame display to stop responding. (This might be a bug with Pygame 1.9.2)
I found a solution that worked for me: just delete the font element before calling pygame.display.quit(), ie just del font every time you're done using it.
The font element is the one you created using the command:
font.SysFont("Comic Sans MS", 12)
but that I personally create using pygame.font.Font(None, font_size)