I want to create a bunch of simple geometric shapes (colored rectangles, triangles, squares ...) using pygame and then later analyze their relations and features. I first tried turtle but apparently that is only a graphing library and cannot keep track of the shapes it creates and I wonder if the same holds true for Pygame. To illustrate the point, say I have this script:
# Import a library of functions called 'pygame'
import pygame
from math import pi
# Initialize the game engine
pygame.init()
# Define the colors we will use in RGB format
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
BLUE = ( 0, 0, 255)
GREEN = ( 0, 255, 0)
RED = (255, 0, 0)
# Set the height and width of the screen
size = [800, 600]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Example code for the draw module")
#Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()
while not done:
# This limits the while loop to a max of 10 times per second.
# Leave this out and we will use all CPU we can.
clock.tick(10)
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done=True # Flag that we are done so we exit this loop
screen.fill(WHITE)
# Draw a rectangle outline
pygame.draw.rect(screen, BLACK, [75, 10, 50, 20], 2)
# Draw a solid rectangle
pygame.draw.rect(screen, BLACK, [150, 10, 50, 20])
# Draw an ellipse outline, using a rectangle as the outside boundaries
pygame.draw.ellipse(screen, RED, [225, 10, 50, 20], 2)
# Draw a circle
pygame.draw.circle(screen, BLUE, [60, 250], 40)
# Go ahead and update the screen with what we've drawn.
# This MUST happen after all the other drawing commands.
pygame.display.flip()
# Be IDLE friendly
pygame.quit()
It creates this image:
Now, suppose I save the image created by Pygame. Is there a way Pygame would be able to detect the shapes, colors and coordinates from the image?
PyGame is a gaming library - it helps with making graphics and audio and controllers for games. It doesn't have support to detect objects in a preexisting image.
What you want is OpenCV (It has Python bindings) - this is made to "understand" things about an image.
One popular math algorithm used to detect shapes (or edges) of any sort are Hough Transforms. You can read more about it here - http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html
OpenCV has Hough transform functions inside it which are very useful.
You could attempt to make your own Hough transform code and use it ... but libraries make it easier.
Yes, It can, but pygame is also good for making games but unfortunately, you can't convert them to IOS or Android, in the past, there was a program called PGS4A which allowed you to convert pygame projects to android but sadly, the program has been discontinued and now, there is no way. On this case, my sggestion would be that if you ever wanted to do this, download Android Studio from this link "http://developer.android.com/sdk/index.html#top" and google on how to use libgdx with Android Studio, this guy has an extremely helpful tutorial which has a lot of parts, but if your goal is to make commercial applications, I would highly recommend you to check this tutorial "https://www.youtube.com/watch?v=_pwJv1QRSPM" extremely helpful. Good luck with your goals and hoped this helped you on making your decision, but python is a good programming language, it will give you the basic idea on how programming is.
Related
So I dont have code because its not an issue with coding as I already have the program done and working however I noticed that:
In gaming menus mouse sensitivity doesnt matter because I set coords to click/move and since its not sensitivity related it works just fine.
However,
When moving a game camera(character view) if I do for example 'move 1000 pixels to the right' this will be affected by mouse sensitivity & ingame sensitivity is there no way to make this work in all resolutions / mouse sensitivity?
Essentially I need to be able to make my friends 'camera"/'character view' to move as much as mine while having a different mouse DPI
Instead of moving in a relative way move to a fixed point of your display for example:
import pyautogui
import time
time.sleep(3)
pyautogui.moveTo(100, 100)
This will move your mouse to 100 pixels along 100 pixels down on your monitor. Other examples:
# To make it drag along the screen
# (x, y, time taken)
pyautogui.moveTo(100, 100, 5)
# To make it hold while moving
# The 5 slows it
pyautogui.dragTo(100, 100, 5)
# To check if the next move pos is on display
# This will return false
pyautogui.onScreen(0, -1)
# To Move only x/y
pyautogui.moveTo(None, 500)
Allthough pyautogui should only move directional to the pixels irrespective of the DPI so may you please attach your code.
Hello fellow programmers,
I wrote a little python program which is use to launch random games on a retrograming distribution, and I use pygame to display the image of the game before launching it
I use a background and my issue is that the background image is clean but when displaying it and the cover of the game over it, it appears with a strange ugly gradient effect as you can see there : https://imgur.com/a/BnNdoqn
It appears mostly in the corner and the cover itself is entirely unaffected.
Here is my pygame code displaying both images :
log('showPic %s' %file)
# INITS
pygame.init()
pygame.mouse.set_visible(0)
backgroundPicture = pygame.image.load(backgroundFile)
picture = pygame.image.load(file)
# # CREATE FULLSCREEN DISPLAY. X = 1920- Y = 1080
fullscreen = pygame.display.set_mode((1920,1080), FULLSCREEN)
fullscreen.blit(backgroundPicture, (0,0))
# # PASTE PICTURE ON FULLSCREEN
x = (1920 - picture.get_width()) /2
y = (1080 - picture.get_height()) /2
fullscreen.blit(picture, (x,y))
# # SHOW FULLSCREEN
pygame.display.flip()
# # WAIT 5 SECONDS (need import time)
time.sleep(5)
# # EXIT PYGAME (Not needed but recommanded)
pygame.display.quit()
pygame.quit()
backgroundPicture is the background image and picture is the cover of the game, I combined the too like it appears in second capture.
So mainly I don't know much at all about display, images, graphical libraries and all that.
I think that this might be related to transparency or alpha layer or compression format of the image but I have no knowledge at all about that either.
The code is launched on a raspberry pi with a linux distribution, don't know much more about it.
Also strangely, one of my users said the strange gradient effect seems to disappear after ten or so launches of the script, but I couldn't reproduce that.
So what am I missing to get rid of that ugly effect ?
Here is the background image here if its characteristic might be related to the problem :
Thank you for your help !
The effect you are seeing is called "banding", see Wikipedia article. It is caused by not having enough bit-depth to represent fine gradations of colour and is most noticeable in large, untextured areas.
There are not many things you can do about it. Your options are basically:
to go to a 16-bit setup instead of 8-bit, if pygame can do that, or
add a small amount of random noise, or dithering to break it up.
I want to darken current screen a bit before poping up a new surface. I know there is no function in pygame to do this and that I will have to process the image to darken it. But as long as I know the only way to get current displaying surface in pygame is by saving it to disk as a file which slows down the game. Is there any other way to do this with pygame? Like saving the image to a value in memory so that I can process it without saving it somewhere.
Thanks in advance,
Stam
You don't need to save anything to a file.
When you read an image to a file, it is a Surface object. You them blit this object to the screen. But these Surface objects have the same methods and properties than the object working as the screen - (which is also a Surface): you can draw primitives, and blit other images to them - all in memory.
So, once you read your image, just make a copy of it, draw a filled rectangle with a solid transparent color on it to darken it, and then blit it to the screen. Repeat the process increasing the transparency level and pasting it on the screen again if you want a fade in effect.
import pygame
screen = pygame.display.set_mode((640,480))
img = pygame.image.load("MYIMAGE.PNG")
for opacity in range(255, 0, -15):
work_img = img.copy()
pygame.draw.rect(work_img, (255,0, 0, opacity), (0,0, 640,480))
screen.blit(work_img, (0,0))
pygame.display.flip()
pygame.time.delay(100)
I am working on a Tank game as a school project and I am trying to make it as user-friendly as possible (with things like customizable keybindings, display settings). However, I don't want the game window to lose it proportions so I figured I would add borders to the sides of the game window in order to support wide screens.
I've attached an imageto illustrate what I'm looking for:
So for this to work, I would need a way to make "screen layers". The base layer is the whole screen with some graphics on the sides added to it and a font displaying score. And then a second layer would be rendered in the middle of the screen which will be the game window and its width and height will be obviously smaller than the base layer.
At the moment, the game is rendered from the top left corner of the screen until all the tiles have been rendered out. So that is why I figured it would be the best to make the tiles render out on a separate surface positioned in the middle of the screen surface.
Would really appreciate some tips on how I can make this possible! :) I don't have that much experience in Classes and pygame, so it would be awesome if you could give a fleshed out description as well! :D
Just draw your different stuff on different surfaces.
For example, create a Surface for your game window (let's call it game_surf), and draw your tanks and bullets etc. on that new Surface instead of the pygame main window (which is just another Surface).
# the main window
screen = pygame.display.init()
# just a regular surface
game_surf = pygame.surface.Surface(width, height)
...
# in your main loop
# or however you draw your sprites
your_sprite_group.draw(game_surf)
Then blit it onto the screen
screen.blit(game_surf, the_position)
Simplest way: Create a group for each 'layer'.
import pygame
front_sprites = pygame.sprite.Group()
middle_sprites = pygame.sprite.Group()
background_sprites = pygame.sprite.Group()
Then, just draw them all onto the screen in the following order:
background_sprites.draw(screen)
middle_sprites.draw(screen)
front_sprites.draw(screen)
When i try to blit an image in pygame the image blits smaller than the actual image, my simplified code is
import pygame
pygame.init()
screen = pygame.display.set_mode((1176,674),0,32)
background = pygame.image.load("picture.jpg").convert()
while True:
screen.blit(background, (0,0))
pygame.display.flip()
The resulting image is about half the size it should be
It is entirely possible that when you view it you have zoomed in. Or there could be formatting irregularities. Either way, there are two simple solutions. Solution number 1 is to just scale up the image in an image editor, if you don't have one, GIMP is free online. A better way is to scale it in the code. You can just double the size of an image you loaded with this modification to your code, solving your problem. Use pygame.transform:
import pygame
pygame.init()
screen = pygame.display.set_mode((1176,674),0,32)
background = pygame.image.load("start_screen.jpg").convert()
background = pygame.transform.scale(background, (1176,674))
while True:
screen.blit(background, (0,0))
pygame.display.flip()
this should work
I found that the issue came from my MacBooks retina screen resolution, once i downloaded a program to adjust the resolution the program worked properly