import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((600,170),0,32)
pygame.display.set_caption("Hello World!") //set caption
background = pygame.image.load('bg.jpeg').convert //load picture and convert it
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(background,(0,0))
pygame.display.update() //refresh
The Error I get :
File "/Users/huangweijun/PycharmProjects/untitled1/first.py", line 12, in<module>
screen.blit(background,(0,0))
TypeError: argument 1 must be pygame.Surface, not builtin_function_or_method
i have download pygame
i don't know how to solve this issue.
The first argument of the function screen.blit is a pygame Surface. Think of this as a screen to draw to. You are specifying an object of the class Image. This will not work for you cannot draw to an image.
Replace background with screen and add background as an argument between screen and (0,0). You code should now look like this:
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((600,170),0,32)
pygame.display.set_caption("Hello World!") //set caption
background = pygame.image.load('bg.jpeg').convert //load picture and convert it
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(screen,background,(0,0))
pygame.display.update() //refresh
pineapple, Looks like the issue here is in this line:
background = pygame.image.load('bg.jpeg').convert
I think what you wanted to use was:
background = pygame.image.load('bg.jpeg').convert_alpha()
Hopefully that's what you were looking for!
EDIT: You could have also just added parentheses after the convert() Oops!
Try them both and see what happens!
-Travis
Related
I've made a menu screen where clicking on a button leads to a different screen in the same window.
def main():
import pygame, random, time
pygame.init()
size=[800, 600]
screen=pygame.display.set_mode(size)
pygame.display.set_caption("Game")
done=False
clock=pygame.time.Clock()
while done==False:
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
done=True
break
if button_VIEW.collidepoint(pos):
if event.type == pygame.MOUSEBUTTONDOWN:
print("VIEW.")
view()
break
screen.fill(black)
...
def view():
done=False
clock=pygame.time.Clock()
while done==False:
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
if event.type == pygame.QUIT:
done=True
break
...
If possible, I'd like to know how I can avoid the error:
screen.fill(black)
error: display Surface quit
>>>
After looking at other questions on here, I tried adding breaks to the exits of any loops, but still the error occurs.
I understand the issue is that the program is trying to execute screen.fill(black) after the window has been closed, but I have no further ideas on how to prevent the error.
I appreciate any help. Sorry if it seems simple.
Several possibilities:
end the process (with e.g. sys.exit()) in the view function. Not ideal.
return a value from the view function to indicate that the application shoud end (e.g. return done), and check for that return value in the main function (if done: return). Better.
make done global and check for its value in the main function. I really would not like this solution.
my favourite: avoid multiple event loops altogether, so the problem solves itself (so you could just e.g. exit the main function with return).
import pygame
import sys
import os
from pygame.locals import *
#Allows for the editing of a window
pygame.init()
#Sets screen size
window = pygame.display.set_mode((800,600),0,32)
#Names the window
pygame.display.set_caption("TEST")
#Types of colors (red,green,blue)
black = (0,0,0)
blue = (0,0,255)
green = (0,255,0)
yellow = (255,255,0)
red = (255,0,0)
purple = (255,0,255)
lightblue = (0,255,255)
white = (255,255,255)
pink = (255,125,125)
mif="climb.PNG"
mif=pygame.image.load(mif).convert()
#Loop
gameLoop = True
while gameLoop:
pygame.display.flip() #must flip the image o the color is visable
window.blit(mif, (0,0))
window.fill(black) #used to fill the creen with the certian color variables
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameLoop=False #Allows the user to exit the loop/game
pygame.quit() #quit the pygame interface
exit(0)
When I select run my yellow window pops up but no image...
I'm new to pygame and python so maybe I just did something wrong?
Is there a specific place that I should have my image saved to?
I'm running on a windows 8 hp ENVY...
I just want to learn what I'm doing wrong so I don't make the error again. (I should also add that I have tried many different pictures and some where not able to be located.)
First fill window with black color window.fill(black),
then put image on it window.blit(mif, (0,0)),
and last send it to your graphic card (and on your monitor) pygame.display.flip()
window.fill(black)
window.blit(mif, (0,0))
pygame.display.flip()
If your image is as big as window then you don't need to use fill(black).
You can keep image in the same folder with script. Or you can create subfolder only for image to have order in folders.
If you call this folder images then you have to add this to filenames images/climb.PNG.
BTW: better use / than \ in path because \n in path can be interpreted as ENTER/new line as in any text.
But the most properly is:
import os
filename = os.path.join('images', 'climb.PNG')
I am trying to create a program that creates a ball every time the mouse clicks, wherever it clicks. I am new to Tkinter and its syntax, but it seems like a pretty useful GUI.
Thanks
Here is the code. This code also tracks whenever a key is pressed, and it prints the syntax for that key.
from Tkinter import *
root = Tk()
def key(event):
pressedkey = repr(event.char)
print "pressed", pressedkey
def callback(event):
canvas.focus_set()
print "clicked at", event.x, event.y
ball = canvas.create_oval(event.x-15, event.y-15, event.x+15, event.y+15, outline='black', fill='gray40', tags=('ball'))
canvas = Canvas(root, width =1224,height=1024,bg='white')
canvas.bind("<Key>", key)
canvas.bind("<Button-1>", callback)
canvas.pack()
root.mainloop()
Here is the Tkinter documentation
The problem isn't errors, it's just that i'm trying to get this crosshair image to follow the mouse about the screen, I succeeded but it's leaving a trail of the same image behind it so the screen eventually becomes spammed with the image, heres the code:
import sys, pygame;
from pygame.locals import *;
spaceship = ('spaceship.png')
mouse_c = ('crosshair.png')
pygame.init()
screen = pygame.display.set_mode((800, 600))
mousec = pygame.image.load(mouse_c).convert_alpha()
space_ship = pygame.image.load(spaceship).convert_alpha()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN and event.button == 1:
print("test1")
elif event.type == MOUSEBUTTONDOWN and event.button == 3:
print("test3")
pos = pygame.mouse.get_pos() #Here is the mouse following code
screen.blit(mousec, (pos)) #Setting image to pos of mouse.. atleast I think I am
pygame.display.update()
Your code has no problem....it is doing what you are telling it.
However for any image drawn a new background is created (along with the image) that is set as the new background it self so the background needs to be erased and set to default before the function pygame.display.update() or pygame.display.flip() is called.
more information here
So basically all you can do is create a new background by taking a image (800x600) ('background.png').
Load it:
bk = pygame.image.load('background.png').convert_alpha()
Blit it before blitting other moving objects.screen.blit(bk, (0,0))pos = pygame.mouse.get_pos() #Here is the mouse following code
screen.blit(mousec, (pos)) #Setting image to pos of mouse
pygame.display.update()
Adding a background to your game provides the game a complete touch but if you do not want to add it then the simplest thing you can do is fill your screen with a color every time the while loop is called.
So just after the while loop starts add this:
screen.fill(Color("black"))
I am trying to update canvas background at the same time running a binding event.
(from code)In do_popup popup menu will be implemented and conti will continuously change the canvas background color. how can i use popup option while canvas is updating continuously.
Sample code:
from Tkinter import *
root = Tk()
def do_popup(event,w2):
print w2 # inplace of print some popupmenu will be implemented
def conti():
idt=1
while idt==1:
w.config(bg="red") # in place of red it will be a random color
w.update_idletasks()
w= Canvas(root, width=600, height=600)
w.grid(row=0, column=0)
line1 = w.create_line(200,200,300,300, width=10, tags="line1", fill="black")
w.tag_bind(line1, "<Button-3>", lambda e, w2="test1" :do_popup(e,w2))
f = Frame(root)
f.grid(row=0, column=1, sticky=N)
f1=Button(f, text='visual', command=lambda :conti())
f1.grid(row=0, column=1,columnspan=1, sticky=W+N+S+E)
mainloop()
will multiprocessing work?
I am using windows 7 32 bit with python 2.7.3
Thanks in advance
When your script enters the mainloop then the events are executed.
To make reoccurring updates I like to do this:
def conti():
try:
w.config(bg="red") # in place of red it will be a random color
finally:
# start conti after 10 milliseconds,
root.after(10, conti)
# could also be 0ms to handle events
root.after(0, conti)
You can see root.mainloop as
while not (root.quit was called):
root.update()
This way wou can do:
root.quit()
and conti automatically stops.
There is no concurrency as with threads in mainloops.
But you can put a mainloop() somewhere when you create an own dialog box and conti will go on.
If you use the modules tkMessageBox(Python2) or tkinter.messagebox(Python3) then you conti should run while the dialog is open.
Does this answer your questions?
PS: do root.protocol("WM_DELETE_WINDOW", root.quit) to make the mainloop end when you close the window.