I'm just going to give you all the code and say that I think it's probably something with the pygame. If someone sees the error or you try it on your own compiler and it works, it would be great if you could tell me what it is. Right now for me it just opens a black screen. Thanks so much, here's the code:
import pygame
from pygame.locals import *
pygame.init()
test = pygame.display.set_mode((1280, 660))
pygame.display.flip()
pygame.draw.rect(test, (255,255,255), (100, 100, 100, 100))
running = True
while running:
for event in pygame.event.get():
if event.type==QUIT:
running = False
pygame.quit()
We want move pygame.display.flip() after all the drawing commands. This is basically telling the program to update the screen after all the draw commands. That being said, all draw commands after this line will not be updated in the visual screen.
Here is the revised code:
import pygame
from pygame.locals import *
pygame.init()
test = pygame.display.set_mode((1280, 660))
pygame.draw.rect(test, (255,255,255), (100, 100, 100, 100))
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type==QUIT:
running = False
pygame.quit()
If you are using a white screen 255,255,255 is white and your rectangles are just white, try to use (0, 0, 0) or any other color what are you experiencing?
Edited:
The problem is with pygame.display.flip() it is updating the display you should call it after the call to draw
Related
import pygame
import sys
pygame.init()
width, height = 1000, 800
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Colonizer")
win.fill("white")
pygame.display.update()
def draw():
pygame.draw.line(win, ("black"), [width,height/2], [width*2,height/2], 5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
draw()
pygame.display.update()
I'm trying to make one simple line and for some reason pygame won't draw it, it just shows white screen.
You cannot "see" the line because it is outside the window. The line starts to the right of the window and continues even further out to the right. Change the coordinates. e.g.:
pygame.draw.line(win, ("black"), [width,height/2], [width*2,height/2], 5)
pygame.draw.line(win, ("black"), [0, height//2], [width, height//2], 5)
I'm completely new to Python and I just started learning Pygame. I'm trying to work with drawing shapes right now, but I can't get the window to open. Idk what the problem is, here's my code:
`#import libraries
import pygame
import math
#init pygame
pygame.init()
#clock
clock = pygame.time.Clock()
#window screen
screen_height_x = 500
screen_height_y = 400
screen = pygame.display.set_mode((screen_height_x, screen_height_y))
#RGBA colors
Black = (0, 0, 0)
White = (255, 255, 255)
Blue = (0, 0, 255)
Red = (255, 0, 0)
Green = (0, 255, 0)
#program
running = True
while running:
for event in pygame.event.get():
running = False
screen.fill(Black)
pygame.display.update()
clock.tick(30)
#Quit
pygame.quit()
quit()`
You're immediately quitting the main loop and you also aren't drawing anything. Try this instead:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(Black)
pygame.display.update()
clock.tick(30)
#Quit
pygame.quit()
quit()
Disclaimer: I cannot try this out at the moment but it should get you started in the right direction.
Is this a class part of the projet or the main class ? If it is the main class, you it should contain a main function.
If this is the main function, you should declare it by adding def main(): before the pygame.init()
And add the module to exectute this as the main script under Quit
if __name__ == "__main__":
main()
Sorry, im french my english is not the best aha !
I am new to Pygame and I wish to fill only certain parts of my screen, for example the half. Currently I am only able to fill the complete screen. Can someone help?
import pygame
color= (255, 0, 0)
screen = pygame.display.set_mode((740, 780))
screen.fill(color)
The 2nd parameter of .fill() is a rectangle, which defines the area to be filled.
The width and height of a pygame.Surface object can be get by .get_width() respectively .get_height():
e.g.
screen.fill(color, (0, 0, screen.get_width()// 2, screen.get_height()))
import pygame
size = w,h = 300, 400
scr = pygame.display.set_mode((w,h))
pygame.display.set_caption("Hello")
scr.fill((0,255,0), rect=(0,0,w,h/2))
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False
One way to have partial fill may include "drawing" a shape (i.e. rectangle) on half of the screen.
import sys
import pygame
def half_screen():
#Initialize game and create screen object.
pygame.init()
color= (255, 0, 0)
screen = pygame.display.set_mode((200, 400))
#Draw rectangle to fill the left half of the screen.
left_half = pygame.draw.rect(screen, color,(0,0, 100, 400))
#Start loop for game- keeps screen open until you decide to quit.
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#Make the current screen visible.
pygame.display.flip()
half_screen()
You can find more information on the draw module - pygame.draw - by going to https://www.pygame.org/docs/ref/draw.html
I am new to pygame, and I was expecting the display be cyan and the rectangle to be drawn. The window appears but not in cyan and without the rectangle?
I believe it has something to do with the order or spacing.
Everything was working before I added the rect.
import pygame
import sys
from pygame.locals import *
pygame.init()
cyan = (0,255,255)
soft_pink = (255,192,203)
screen_width = 800
screen_height = 600
gameDisplay = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('''example''')
pygame.draw.rect(gameDisplay,soft_pink,(389,200),(300,70),4)
gameDisplay.fill(cyan)
gameExit = True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
You should be very careful with your formatting for Python code. Testing your code and fixing up the formatting for the while loop reveals the problem:
C:\src\python\pygame1>python buggy.py
Traceback (most recent call last):
File "buggy.py", line 16, in <module>
pygame.draw.rect(gameDisplay,soft_pink,(389,200),(300,70),4)
TypeError: function takes at most 4 arguments (5 given)
If you just replace the pygame.draw.rect call with the correct number of parameters it shows a cyan window. I tested the following replacement line:
pygame.draw.rect(gameDisplay,soft_pink,(389,200,300,70))
When initializing a pygame screen, a surface is returned which you can fill and should fill continuously in your while loop. I suggest you use a surface object for the rectangle as well. Just change your code like so:
import pygame
import sys
from pygame.locals import *
pygame.init()
cyan = (0,255,255)
soft_pink = (255,192,203)
screen_width = 800
screen_height = 600
gameDisplay = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Example') # shouldn't use triple quotes
gameExit = True
surf = pygame.Surface((200, 75)) # takes tuple of width and height
rect = surf.get_rect()
rect.center = (400, 300)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
gameDisplay.fill(cyan) # continuously paint screen cyan
surf.fill(soft_pink) # continuously paint rectangle
gameDisplay.blit(surf, rect) # position rectangle at position
Remember continuous rendering is the underlying secret to game development, and objects are painted in the order you specify. So in this case you'd actually see the rectangle because it's painted after the screen is.
When i make a pygame game and hover over it, the cursor turns into the loading thing, that means I can't resize the window, or if i were to make buttons i wouldn't be able to use them... Help? Here's my code.
import pygame
import random
import time
import os
from pygame.locals import *
red = (255,0,0)
white = (255,255,255)
black = (0,0,0)
green = (0,255,0)
blue = (0,0,255)
pygame.init()
background_colour = (white)
(width, height) = (800, 600)
screen = pygame.display.set_mode((width, height), pygame.RESIZABLE)
screen.fill(background_colour)
while True:
pygame.draw.circle(screen,
(random.randint(0,255),
random.randint(0,255),random.randint(0,255)),
(random.randint(0,800),random.randint(0,600)), 20)
pygame.display.flip()
So yeah, it always happens with pygame.
Add this in top of your while.
This for runs throught events and if
event.type == pygame.QUIT
This means that you presed cross for closing the window and window will be closed. You can catch all pygame events like this.
while True: #your wile
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#your code