I'm following a very basic pygame tutorial, which I've done before. I'm suspecting there's a version issue, though I'm at a loss how to move forward.
macOS 10.14.4
Python 3.7.2
pip 18.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
code:
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800,600))
surf = pygame.Surface((75,25))
surf.fill((125,125,125))
# What does this rect do?
rect = surf.get_rect()
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
screen.blit(surf, (400,300))
pygame.display.flip()
The only question I have there is what is that rect assignment for? I can't blit the rect, so it appears to have no purpose.
I've gone through a couple other tutorials, copy-pasted code and I get the same result with each one: a white screen with no rectangles. It's as if the display code works, but any other surface just don't appear.
No errors in the console. I'm really not sure how to debug at this point.
There is nothing to debug here. rect = surf.get_rect() is not used, maybe like martineau said in the comment will be used later in the tutorial.
What the code is doing is to draw a small gray rectangle on a gray surface (that's why you do not see anything).
surf = pygame.Surface((75,25))
This creates a small gray surface, which is blit later in the main loop at coordinates (top-left corner) 400, 300:
screen.blit(surf, (400,300))
As I said, the reason you do not see it is because surf il filled with gray (this line):
surf.fill((125,125,125))
and your background is gray too. I do not know if this is system dependent, on my linux the background is black and the rectangle is clearly visible.
So try to change the color of the rectangle to something clearer or darker, or fill the display surface with black before the main loop by doing:
screen.fill((0, 0, 0))
Related
So I am attempting to draw a chess board (the current code has nothing to do with chess boards, as I'm trying to debug my issue), and I'm running into an interesting issue with pygame.
import pygame
pygame.init()
clock = pygame.time.Clock()
window_width = 400
window_height = 400
display_window = pygame.display.set_mode((window_width, window_height))
. . .
def chess_game():
playing = True
while playing:
clock.tick(30)
# chess_board.draw()
pygame.draw.rect(display_window, (255, 0, 0), (0, 0, window_width, window_height))
pygame.draw.circle(display_window, (0, 255, 0), (100, 100), 20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
playing = False
pygame.quit()
break
chess_game()
So when I run this code, I am expecting to get a red window with a green circle. Instead, I get a black screen. The strange thing I'm running into, however, is that if I drag the window off the screen (so that a portion of the window is outside of my desktop window), the portion of the window that is outside my desktop will be drawn correctly. See the image below.
As you can see, the red background and the circle are there, they just aren't visible unless I drag the window off the screen.
I don't think it's a pygame problem, as I built an actually functional program using similar code already. If this helps at all, I'm on a Windows 10 laptop using PyCharm.
Thanks!
You'll need to call
pygame.display.update()
on each frame. Dragging the window has the side effect of ensuring the newly "revealed" pixels are updated.
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 am trying to create a simple program in which a user can move a shape on the screen using his finger (on a touchscreen).
this is my code so far:
import pygame
def main():
pygame.init()
DISPLAY = pygame.display.set_mode((1000,500),0,32)
WHITE = (255,255,255)
blue = (0,0,255)
DISPLAY.fill(WHITE)
pygame.mouse.set_visible(False)
pygame.draw.rect(DISPLAY, blue,(480,200,50,250))
pygame.display.update()
pygame.mouse.set_pos(480, 200)
while True:
for event in pygame.event.get():
pos = pygame.mouse.get_pos()
pygame.draw.rect(DISPLAY, blue, (pos[0]-25,pos[1], 50, 250))
pygame.display. update()
DISPLAY.fill(WHITE)
main()
The problem is that when I touch the screen, nothing will happen until I move my finger. If I print the events I can see that there is no event listed until I start moving my finger, so that is probably the reason.
I want to be able to register the finger press (as an event I guess) on the screen even before it starts to move, is there anyway to do this using PyGame?
Thanks.
In case anyone is stumbling across this later, in pygame 2 there is much better touch support. You can install with
pip install pygame==2.0.0.dev6
(check the pygame github for the most recent version) And there are three new event types. pygame.FINGERDOWN, pygame.FINGERUP, and pygame.FINGERMOTION. The pygame.FINGERUP event registers the touch input right when the screen is touched instead of when the screen is released like pygame.MOUSEBUTTONDOWN
I have made a game that utilises a computer touch screen and manages the touch quite well by handling two types of events. One is pygame.MOUSEMOTION and the other one is pygame.MOUSEBUTTONDOWN. Both of them contain the attribute event.pos. Problem is, at least when it comes to my touch screen (Lenovo), that the initial touch is not registered by the event handler of pygame.
You can confirm this by printing all the events to the terminal while touching your screen. I get nothing until I release or move my finger but on release I get the event pygame.MOUSEBUTTONDOWN rapidly followed by pygame.MOUSEBUTTONUP.
I know that this doesn't really solve your problem, but it might perhaps help you in some way. Otherwise, check out Kivy. There might be a solution there.
import pygame
def main():
pygame.init()
DISPLAY = pygame.display.set_mode((1000,500),0,32)
WHITE = (255,255,255)
blue = (0,0,255)
DISPLAY.fill(WHITE)
pygame.mouse.set_visible(False)
pygame.draw.rect(DISPLAY, blue,(480,200,50,250))
pygame.display.update()
pygame.mouse.set_pos(480, 200)
while True:
for event in pygame.event.get():
if event.type == MOUSEBUTTONDOWN or event.type == MOUSEMOTION:
pos = event.pos
pygame.draw.rect(DISPLAY, blue, (pos[0]-25,pos[1], 50, 250))
pygame.display. update()
DISPLAY.fill(WHITE)
main()
I took this tutorial from pygame.org which should show how to resize the window properly (image has to be supplied to it, you can use for instance my gravatar). The image should resize to the window, but this doesn't happen with me. Only one VideoResize event is created as soon as I resize the window even so slightly:
<Event(16-VideoResize {'h': 500, 'w': 501, 'size': (501, 500)})>
No other VideoResize events are created (other things like mouse movement or keypresses work). So is the tutorial wrong? Is my computer wrong? What is the proper way of doing it?
I'm running: Python 2.7.5, Pygame 1.9.1, Fedora 20, MATE 1.8.1, Toshiba Satellite.
Here's the code (slightly modified to print the event, but neither the original nor this one work):
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((500,500), RESIZABLE)
pic = pygame.image.load("example.png")
screen.blit(pygame.transform.scale(pic, (500,500)), (0,0))
pygame.display.flip()
done = False
while not done:
pygame.event.pump()
for event in pygame.event.get():
if event.type == QUIT:
done = True
elif event.type == VIDEORESIZE:
print event # show all VIDEORESIZE events
screen = pygame.display.set_mode(event.dict['size'], RESIZABLE) # A
screen.blit(pygame.transform.scale(pic, event.dict['size']), (0,0))
pygame.display.flip()
pygame.display.quit()
If I comment the line # A, then I get plenty of events, but this is the line which resizes the window.
Well I ran the tutorial example with only one change which is I used a pic of a cat called cat.png and it worked fine. The resize is working you just grab a corner and it allows me to adjust it freely with dragging. The picture fills the window whatever size I make it. Have you done other scripts with pygame successfully?
In the following code, there is not just one circle on the screen at any given point in time.
I want to fix this to make it so that it looks like there is only one circle, instead of leaving a smudge trail where ever the mouse cursor has been.
import pygame,sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((640,400),0,32)
radius = 25
circle = pygame.Surface([radius*2]*2,SRCALPHA,32)
circle = circle.convert_alpha()
pygame.draw.circle(circle,(25,46,100),[radius]*2,radius)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
screen.blit(circle,(pygame.mouse.get_pos()[0],100))
pygame.display.update()
pygame.time.delay(10)
You need to specifically erase the circle before you blit it again. Depending on how complicated your scene is, you may have to try different methods. Generally what I do is have a "background" surface that a blit to the screen every frame and then blit the sprites/other surfaces in their new positions (blits in Pygame are very fast, so even in fairly large screens I haven't had speed issues doing this). For your code above, it's simple enough just to use surface.fill(COLOR) where COLOR is your background color; eg, (255,255,255) for white:
# ...
screen = pygame.display.set_mode((640,400),0,32)
backgroundColor = (255,255,255)
# ...
while True:
# ...
screen.fill(backgroundColor)
screen.blit(circle,(pygame.mouse.get_pos()[0],100))
pygame.display.update()
pygame.time.delay(10)
Edit in answer to your comment: It is possible to do this in a more object-oriented way.
You will need to have a background Surface associated with your screen (I usually have a Display or Map class (depending on the type of game) that does this). Then, make your object a subclass of pygame.sprite. This requires that you have self.image and self.rect attributes (the image being your surface and the rect being a Pygame.rect with the location). Add all of your sprites to a pygame.group object. Now, every frame, you call the draw method on the group and, after you update the display (ie, with pygame.display.update()), you call the clear method on the group. This method requires that you provide both the destination surface (ie, screen above) and a background image.
For example, your main loop may look more like this:
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
circle.rect.center = (pygame.mouse.get_pos()[0],100)
circleGroup.draw(screen)
pygame.display.update()
circleGroup.clear(screen, backgroundSurface)
pygame.time.delay(10)
See the documentation on the Sprite and Group classes for more information.