Why my rectrangle does not appere(python 3.8 windoes 7) [duplicate] - python

This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 1 year ago.
I tried to make a clone of a game but the rectangle (which is the basic shape that I will need)does not appear. What did I do wrong or its just pygame going crazy?
code:
# importing modules
import pygame
import sys
import random
# starting pygame
pygame.init()
# making a screen
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
pygame.display.quit()
pygame.quit()
exit()
# colors
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (4, 255, 0)
# cube
if running == True:
pygame.draw.rect(screen, blue, (395, 0, 10, 10)),
pygame.draw.rect(screen, blue, (395, 10, 10, 10)),
pygame.draw.rect(screen, blue, (395, 20, 10, 10)),
clock.tick(60)
and also how am I going to make it empty and 3d. I know I am asking for a lot but I believe someone can explain

You have to draw the scene in the application loop:
# importing modules
import pygame
import sys
import random
# colors
white = (255, 255, 255)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (4, 255, 0)
# starting pygame
pygame.init()
# making a screen
(width, height) = (500, 500)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Mincraft')
running = True
# fps counter
clock = pygame.time.Clock()
print(clock)
# geting the x button to work
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.draw.rect(screen, blue, (395, 0, 10, 10))
pygame.draw.rect(screen, blue, (395, 10, 10, 10))
pygame.draw.rect(screen, blue, (395, 20, 10, 10))
pygame.display.update()
clock.tick(60)
pygame.display.quit()
pygame.quit()
exit()
The typical PyGame application loop has to:
handle the events by either pygame.event.pump() or pygame.event.get().
update the game states and positions of objects dependent on the input events and time (respectively frames)
clear the entire display or draw the background
draw the entire scene (blit all the objects)
update the display by either pygame.display.update() or pygame.display.flip()
limit frames per second to limit CPU usage with pygame.time.Clock.tick

Related

Missing Corner on Rectangle Pygame [duplicate]

This question already has answers here:
How to Make a Border in Pygame
(1 answer)
How can I draw a rectangular outline (not filled) with PyGame?
(1 answer)
Closed 1 year ago.
When working on my game, I noticed that shapes have "missing corners." I have tested this with lines and a rectangle. How do I fix this?
import pygame
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
PASCOLOR = (199, 121, 121)
# add font
gameFont = pygame.font.Font('PressStart2P.ttf', 25)
gameDisplay = pygame.display.set_mode((1200, 650))
pygame.display.set_caption('test game')
exitgame = False
while not exitgame:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitgame = True
gameDisplay.fill(BLACK)
# add outside rectangle
pygame.draw.rect(gameDisplay, WHITE, (100, 100, 1000, 450), 5)
# Add images
addimg = pygame.image.load('addimg.png')
# resize ranta
addimg = pygame.transform.scale(addimg, (63.5, 87))
def loadImages():
gameDisplay.blit(addimg, (110,110))
loadImages()
# add text to screen
titletxt = gameFont.render("test game", True, PASCOLOR)
gameDisplay.blit(titletxt, (400, 40))
pygame.display.flip()
pygame.quit()
quit()
Is this how Pygame renders rectangles or an error?
See that the pixels on the corners are missing.
Set the corner radius (border_radius) to get a better result (see pygame.draw.rect):
pygame.draw.rect(gameDisplay, WHITE, (100, 100, 1000, 450), 5)
pygame.draw.rect(gameDisplay, WHITE, (100, 100, 1000, 450), 5, border_radius=1)
See also How can I draw a rectangular outline (not filled) with PyGame?.

Python PyGame Image.png is not displaying [duplicate]

This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 1 year ago.
I ran into a problem that if I run my project it is not showing the image the image is very big so I resized it and it was not helping.
The Code:
import pygame
import sys
def title(title):
pygame.display.set_caption(title)
def background(Color1, Color2, Color3):
pygame.draw.rect(screen, (Color1, Color2, Color3), pygame.Rect(0, 0, 2000, 3000))
pygame.display.flip()
screen = pygame.display.set_mode((1280, 720))
Card1Img = pygame.image.load('Card1.png')
Card1Img = pygame.transform.scale(Card1Img, (100,100))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
background(255, 255, 255)
title("Card-Jitsu")
screen.blit(Card1Img, (0, 0))
The Image is 1000 x 1126 px:
I did just want to make a copy of Card-Jitsu in python and pygame
The problem is that the display is not updating and drawing the image. You need to have the display update at the end of your while loop. The below code should work:
import pygame
import sys
def title(title):
pygame.display.set_caption(title)
def background(Color1, Color2, Color3):
pygame.draw.rect(screen, (Color1, Color2, Color3), pygame.Rect(0, 0, 2000, 3000))
#Having the display update here causes flickering
screen = pygame.display.set_mode((1280, 720))
Card1Img = pygame.image.load('Card1.png')
Card1Img = pygame.transform.scale(Card1Img, (100,100))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
background(255, 255, 255)
title("Card-Jitsu")
screen.blit(Card1Img, (0, 0))
pygame.display.flip() #update the display after the image and background have been drawn

cannot see sprite in pygame [duplicate]

This question already has answers here:
Pygame on Mac Mojave
(1 answer)
Problems getting pygame to show anything but a blank screen on Macos
(10 answers)
Closed 2 years ago.
My kid is learning python via pygame
he saw in online tutorial about sprites
and wrote this code, but we dont see anything
import pygame
import random
WIDTH = 360
HEIGHT = 480
FPS = 30
# colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill(WHITE)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("my game")
clock = pygame.time.Clock()
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
# game loop
running = True
while running:
# keep the loop running at the right speed
clock.tick(FPS)
# events
for event in pygame.event.get():
# check for closing the window
if event.type == pygame.QUIT:
running = False
# update
all_sprites.update()
# draw/render
screen.fill(BLACK)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()

Remove black border in pygame fullscreen

I am creating a game using pygame 1.9.6. But, as you can see by running this simple example, some black borders appear around the window, even with the FULLSCREEN flag.
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((900, 500), FULLSCREEN)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
screen.fill((255, 255, 255))
pygame.display.flip()
I tried to add the flag NOFRAME in the screen initialisation, but it didn't work.
I wonder if it is possible to remove the border, for example by increasing the screen size to just fit in the current screen. But I also want to keep the 900x500 resolution.
Can pygame resize the entire screen? Do I have to blit() everything on a Surface, then rescale it and draw it on the actual screen?
Thanks #Glenn Mackintosh!
The best way to remove the black border is actually to add the SCALED flag in the screen declaration like this:
screen = pygame.display.set_mode((900, 500), FULLSCREEN|SCALED)
But to use it, the pygame version 2.0.0 or more is required.
The only way to remove the black bars is to set the pygame.display.set_mode((width, height)) to the users resolution. Use pygame.display.Info() to get the users' display resolution. Furthermore, you can have a preferred resolution such as 900 x 500 to get a "differential number" to get a retro/pixelated look.
resolution = pygame.display.Info()
width = resolution.current_w
height = resolution.current_h
dx = width // 900
dy = height // 500
This dx and dy can then be used to scale everything to a bigger size.
I would try this code:
import pygame
from pygame.locals import *
pygame.init() # You forgot the initialize the pygame window!
resolution = pygame.display.Info() # Get the users resolution
width = resolution.current_w
height = resolution.current_h
screen = pygame.display.set_mode((width, height), FULLSCREEN, 0, 32)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
screen.fill((255, 255, 255))
mouse_pos = pygame.mouse.get_pos()
pygame.draw.circle(screen, (0, 0, 255), mouse_pos, 20)
pygame.draw.circle(screen, (0, 0, 200), mouse_pos, 18)
pygame.draw.circle(screen, (0, 0, 100), mouse_pos, 15)
pygame.draw.circle(screen, (0, 0, 50), mouse_pos, 10)
pygame.display.flip()

Pygame: Rescale pixel size

With pygame, I created a 20x20 pixel window and added a 2x2 pixel rectangle.
When I run the program, the window size is super small and I can barely see the rectangle. How can I increase the window size whilst keeping the number of pixels constant, i.e. increase the pixel size? I am aware of this similar question, but there a somewhat more complicated case is discussed.
import pygame
screen_width, screen_height = 20, 20
x, y = 10, 10
rect_width, rect_height = 2, 2
vel = 2
black = (0, 0, 0)
white = (255, 255, 255)
pygame.init()
win = pygame.display.set_mode((screen_width, screen_height))
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
win.fill(black)
pygame.draw.rect(win, white, (x, y, rect_width, rect_height))
pygame.display.update()
pygame.quit()
Don't draw directly to the screen, but to another Surface.
Then scale that new Surface to the size of the screen and blit it onto the real screen surface.
Here's an example:
import pygame
screen_width, screen_height = 20, 20
scaling_factor = 6
x, y = 10, 10
rect_width, rect_height = 2, 2
vel = 2
black = (0, 0, 0)
white = (255, 255, 255)
pygame.init()
win = pygame.display.set_mode((screen_width*scaling_factor, screen_height*scaling_factor))
screen = pygame.Surface((screen_width, screen_height))
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill(black)
pygame.draw.rect(screen, white, (x, y, rect_width, rect_height))
win.blit(pygame.transform.scale(screen, win.get_rect().size), (0, 0))
pygame.display.update()
pygame.quit()

Categories