Disabling anti-aliasing in pygame - python

I have tried to set single pixels in pygame with pygame.PixelArray. Unfortunately, It looks like pygame automatically anti-aliases those pixels. This is what I have tried so far:
import pygame
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
WHITE = (255,255,255)
class GUI:
def __init__(self):
self.screen = pygame.display.set_mode((300, 300))
pygame.mouse.set_visible(True)
self.clock = pygame.time.Clock()
def gameloop(self):
running = True
while running:
self.screen.fill(WHITE)
# event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# drawing
# for some reason, everything gets anti-aliased
pixel_array = pygame.PixelArray(self.screen)
pixel_array[100][100] = BLACK
pixel_array[100][101] = BLUE
pixel_array[101][100] = BLUE
pixel_array[101][101] = BLACK
del pixel_array
# update full display
pygame.display.flip()
self.clock.tick(30)
def main():
pygame.init()
gui = GUI()
gui.gameloop()
pygame.quit()
if __name__ == '__main__':
main()
What I have got:
What I expected to get:
System:
Python version: 3.7.2 (64-bit)
OS: Windows 10 Home Version 1803 Build 17134.590
pygame version: 1.9.4
Display: Integrated in Lenovo-Laptop (1920 x 1080)
Processor: Intel-Core-i5-6300HQ
IGP: Intel HD Graphics 530
GPU: Nvidia GeForce GTX 960M

After a hint from Eric, I figured out that the problem wasn't caused by pygame, but by the display resolution settings. The display was scaled to 125% by default.
I don't know how to describe where you can find these settings in english since my Windows is set to german, so I made screenshots:

Related

CV2 Camera output inside PyGame [duplicate]

I want to open the camera with Python using the pygame module on a Windows 7 machine, but it's not working. I have previously used "/dev/video0" which is the read device in Linux. The pygame documentation just shows how to open a camera device in Linux. I am using pygame version 1.9.1 and Python 2.7.
How can I open the camera on a Windows device? When I try my existing script, the error I get is:
File "E:/test_python/open_cam2.py", line 10, in <module>
cam = pygame.camera.Camera("/dev/video0", (640, 480))
File "C:\Python27\lib\site-packages\pygame_camera_vidcapture.py", line 47, in init
self.dev = vidcap.new_Dev(device, show_video_window)
TypeError: an integer is required
Try this,
import pygame.camera
import pygame.image
import sys
pygame.camera.init()
cameras = pygame.camera.list_cameras()
print "Using camera %s ..." % cameras[0]
webcam = pygame.camera.Camera(cameras[0])
webcam.start()
# grab first frame
img = webcam.get_image()
WIDTH = img.get_width()
HEIGHT = img.get_height()
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame Camera View")
while True :
for e in pygame.event.get() :
if e.type == pygame.QUIT :
sys.exit()
# draw frame
screen.blit(img, (0,0))
pygame.display.flip()
# grab next frame
img = webcam.get_image()
The pygame.camera module natively supports cameras under Windows since version 2.0.2. See a minimal example using the pygame.camera module (tested with Windows):
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
camera_list = pygame.camera.list_cameras()
camera = pygame.camera.Camera(camera_list[0])
window = pygame.display.set_mode(camera.get_size())
clock = pygame.time.Clock()
camera.start()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
camera_frame = camera.get_image()
window.fill(0)
window.blit(camera_frame, (0, 0))
pygame.display.flip()
pygame.quit()
exit()
This should work...
import pygame
import pygame.camera
pygame.init()
gameDisplay = pygame.display.set_mode((1280,720), pygame.RESIZABLE)
pygame.camera.init()
cam = pygame.camera.Camera(0,(1280,720))
cam.start()
while True:
img = cam.get_image()
gameDisplay.blit(img,(0,0))
pygame.display.update()
for event in pygame.event.get() :
if event.type == pygame.QUIT :
cam.stop()
pygame.quit()
exit()
I'm using windows 10 , pygame version 1.9.6

Pygame screen does not draw content on MacOS

I'm trying to run a very basic pygame application, however I cannot get it to draw content to, or even fill the screen. I do update the screen in the main loop after filling it, however the screen stays blank and does not color black.
Printing inside the run function prints 60 times every second as expected. I do run MacOS Catalina which may be the problem, although I did not find any other mention of this problem.
import pygame
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
class Simulator:
running = True
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((800, 600), 0, 32)
self.clock = pygame.time.Clock()
pygame.display.set_caption('Traffic Simulator')
def run(self):
while (self.running):
for e in pygame.event.get():
if (e.type == pygame.QUIT):
self.running = False
self.screen.fill(BLACK)
pygame.display.flip()
pygame.display.update()
self.clock.tick(60)
if __name__ == "__main__":
sim = Simulator()
sim.run()
pygame.quit()
It shows a window with the title 'Traffic Simulator' with the default system color grey in the window, which I expect to be black.
#sloth Was right after all. After building pygame manually instead of pulling it from pip, it worked.
Here are the instructions I followed.
https://www.pygame.org/wiki/MacCompile

No video device on chromebook

I'm trying to code a game using pygame in googles colaboratory using pygame. I succesfully added pygame by running:
import os
!git clone https://github.com/ntasfi/PyGame-Learning-Environment.git
os.chdir('PyGame-Learning-Environment')
!pip install -e .
!pip install pygame
os.chdir('/content')
But when I wrote this code:
import pygame
pygame.init()
BLACK = [0,0,0]
size = [100,100]
screen = pygame.display.set_mode((size))
screen.fill(BLACK)
screen.update()
pygame.quit()
I get the following error message:
Traceback (most recent call last)
<ipython-input-10-b26800e3009a> in <module>()
BLACK = [0,0,0]
size = [100,100]
screen = pygame.display.set_mode((size))
screen.fill(BLACK)
Error: No available video device
Is there any way i can add make pygame detect the laptop screen?
first of all try this
import pygame
pygame.init()
BLACK = (0, 0, 0) #dont use []'s
WIDTH = 0 #yourwidth
HEIGHT = 0 #yourheight
screen = pygame.display.set_mode((WIDTH,HEIGHT))
screen.fill(BLACK)
running = True
while(running):
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

pygame camera on Windows [duplicate]

I want to open the camera with Python using the pygame module on a Windows 7 machine, but it's not working. I have previously used "/dev/video0" which is the read device in Linux. The pygame documentation just shows how to open a camera device in Linux. I am using pygame version 1.9.1 and Python 2.7.
How can I open the camera on a Windows device? When I try my existing script, the error I get is:
File "E:/test_python/open_cam2.py", line 10, in <module>
cam = pygame.camera.Camera("/dev/video0", (640, 480))
File "C:\Python27\lib\site-packages\pygame_camera_vidcapture.py", line 47, in init
self.dev = vidcap.new_Dev(device, show_video_window)
TypeError: an integer is required
Try this,
import pygame.camera
import pygame.image
import sys
pygame.camera.init()
cameras = pygame.camera.list_cameras()
print "Using camera %s ..." % cameras[0]
webcam = pygame.camera.Camera(cameras[0])
webcam.start()
# grab first frame
img = webcam.get_image()
WIDTH = img.get_width()
HEIGHT = img.get_height()
screen = pygame.display.set_mode( ( WIDTH, HEIGHT ) )
pygame.display.set_caption("pyGame Camera View")
while True :
for e in pygame.event.get() :
if e.type == pygame.QUIT :
sys.exit()
# draw frame
screen.blit(img, (0,0))
pygame.display.flip()
# grab next frame
img = webcam.get_image()
The pygame.camera module natively supports cameras under Windows since version 2.0.2. See a minimal example using the pygame.camera module (tested with Windows):
import pygame
import pygame.camera
pygame.init()
pygame.camera.init()
camera_list = pygame.camera.list_cameras()
camera = pygame.camera.Camera(camera_list[0])
window = pygame.display.set_mode(camera.get_size())
clock = pygame.time.Clock()
camera.start()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
camera_frame = camera.get_image()
window.fill(0)
window.blit(camera_frame, (0, 0))
pygame.display.flip()
pygame.quit()
exit()
This should work...
import pygame
import pygame.camera
pygame.init()
gameDisplay = pygame.display.set_mode((1280,720), pygame.RESIZABLE)
pygame.camera.init()
cam = pygame.camera.Camera(0,(1280,720))
cam.start()
while True:
img = cam.get_image()
gameDisplay.blit(img,(0,0))
pygame.display.update()
for event in pygame.event.get() :
if event.type == pygame.QUIT :
cam.stop()
pygame.quit()
exit()
I'm using windows 10 , pygame version 1.9.6

Pygame on mac error with loading images

I recently started programming on pygame, I use python 3.5.1 and the latest pygame and when I load a bitmap(bmp) it puts it on the screen like this
At school when I program on the computer it works fine just on my macbook pro 13'' with el capitain
import pygame #imports pygame
width = 550 #width is 550
height = 420 #height is 420
size = width, height # size is a tuple
screen = pygame.display.set_mode(size)
white = 255,225,255
screen.fill(white)
gameOn = True
mario = pygame.image.load("mario.bmp") #load the bitmap
marioflip = pygame.transform.flip(mario, True, True)
screen.blit(marioflip, (0,0)) # add it to screen
marioscale = pygame.transform.scale(mario, (50,50))
screen.blit(marioscale, (250,250))
mariorotate = pygame.transform.rotate(mario, 45)
screen.blit(mariorotate,(350,0))
while gameOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.flip()
You are seeing a known bug in SDL_image on that version of OS X. See this link for details and a workaround:
https://bitbucket.org/pygame/pygame/issues/284/max-osx-el-capitan-using-the-deprecated

Categories

Resources