Problem is that all I can see is a black screen when run, think the problem is to do with blitting but not 100% sure
#IMPORTING PYGAME
import pygame, sys
from pygame.locals import *
pygame.init()
FPS = 60
fpsClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((600,400), 0, 32)
pygame.display.set_caption("My Game")
#DIRECTIONS
LEFT = 1
DOWN = 3
UP = 7
RIGHT = 9
#SPEED OF MOVEMENT
MOVESPEED = 4
#COLOURS
WHITE = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
#DRAWING ASSETS/SPRITES
player1 = (windowSurface, pygame.Rect(200, 200, 20, 20), GREEN, UP)
player1.blit(pygame.surface.blit, (200), area=None, special_flags = 0)
player2 = (windowSurface, pygame.Rect(100, 100, 20, 20), GREEN, DOWN)
#player2 = (pygame.Surface.blit(, windowSurface))
barricade1 = {windowSurface:pygame.Rect(50, 5000, 20, 20), :GREEN, :UP}
#barricade1(pygame.Surface.blit(, windowSurface))
barricade2 = {windowSurface:pygame.Rect(250, 250, 20, 20), :GREEN, :DOWN}
#barricade2(pygame.Surface.blit(, windowSurface))
#ball = {'rect':pygame.Rect(200, 200, 20, 20), 'color':GREEN, 'dir':UPDOWNLEFTRIGHT}
#ball(pygame.Surface.blit(, windowSurface))
#SPRITE GROUPS
players = [player1, player2]
barricades = [barricade1, barricade2]
ball = [ball]
windowSurface.fill(WHITE)
pygame.display.update()
fpsclock = pygame.time.Clock
1. Your WHITE Variable is wrong. That is Black in RGB Format. White is (255,255,255)
2. You are filling the screen and immediately after that you update it.
First fill the screen and THEN you can draw your players on top.
windowSurface.fill(WHITE)
#Do drawing here
pygame.display.update()
Related
im creating code for a covid simulator but am a little stuck on the menu.
So far my code for the menu is as followed:
import pygame
pygame.init()
font_title = pygame.font.SysFont("arial", 35)
font_para = pygame.font.SysFont("monospace", 15)
BLACK = (0, 0, 0)
WHITE = (255,255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
menu = pygame.display.set_mode((999, 800))
pygame.display.set_caption('WELCOME TO COVID-19 SIMULATOR')
clock = pygame.time.Clock()
def Label(screen, text, size, font, color, x, y):
label = font.render(text, size, color)
screen.blit(label, (x, y))
Flag = True
def Menu():
while Flag == True:
title = Label(menu, "COVID-19 SIMULATOR: Main Menu", 1, font_title, WHITE, 275, 150)
own_sim = pygame.Rect(350, 290, 290, 40)
saved_sim = pygame.Rect(350, 390, 290, 40)
referrences = pygame.Rect(350, 490, 290, 40)
x_mouse, y_mouse = pygame.mouse.get_pos()
if own_sim.collidepoint((x_mouse, y_mouse)):
if click:
own_sim()
if saved_sim.colllidepoint((x_mouse, y_mouse)):
if click:
saved_sim()
if referrences.collidepoint((x_mouse, y_mouse)):
if click:
referrences()
pygame.draw.rect(menu, RED, own_sim)
pygame.draw.rect(menu, RED, saved_sim)
pygame.draw.rect(menu, RED, referrences)
pygame.display.flip()
clock.tick(60)
Menu()
I need my red boxes to have have text on, which i cant manage to do, and when they are clicked i want them to call another subprogram. I would be very grateful for any help, thanks in advance.
There is a typo in your code. collidepoint is written with ll, not with lll:
if saved_sim.colllidepoint((x_mouse, y_mouse)):
if saved_sim.collidepoint((x_mouse, y_mouse)):
size = width, height = 800, 500
display_surface = pygame.display.set_mode((width, height))
self.screen = pygame.display.set_mode(size)
pygame.display.set_caption("Temperature Test")
myfont = pygame.font.SysFont("freesansbold.ttf", 25)
white = (255, 255, 255)
green = (0, 255, 0)
blue1 = (255, 0, 0)
black = (0,0,0)
red = (255, 0, 0)
while True:
pygame.display.update()
for row in sensor.pixels:
row.sort()
print(["Temperature is: {0}".format(temp) for temp in row], 1, red)
celsius = myfont.render("Temperature is: {0}".format(row[-1]), 1, red)
#pygame.draw.rect(screen, (0,0,0),celsius.get_rect())
screen.blit(celsius, celsius.get_rect(center=(160, 240)))
pygame.display.update()
I don't why the display is not working according to the code
To draw your text in different locations, you need to increment your y position every iteration, e.g.
y = starting_value
for foo in bar:
screen.blit(foo)
y += line_height
Here's a minimal example based on your code that shows how to render your text in different positions on the screen.
import pygame
# create placeholder class
class FakeSensor(object):
pass
# initialisations
pygame.init()
pygame.font.init()
size = width, height = 800, 500
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Temperature Test")
myfont = pygame.font.SysFont("freesansbold.ttf", 25)
white = (255, 255, 255)
green = (0, 255, 0)
blue1 = (255, 0, 0)
black = (0, 0, 0)
red = (255, 0, 0)
clock = pygame.time.Clock()
sensor = FakeSensor()
sensor.pixels = [[1, 2, 300.5], [1, 2, 311.2]]
running = True
while running:
pygame.display.update() # redundant
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(green)
y = 240 # initial Y position
for row in sensor.pixels:
row.sort()
print(["Temperature is: {0}".format(temp) for temp in row], 1, red)
celsius = myfont.render("Temperature is: {0}".format(row[-1]), 1, red)
# pygame.draw.rect(screen, (0, 0, 0), celsius.get_rect(center=(160, y)), 1)
screen.blit(celsius, celsius.get_rect(center=(160, y)))
y += 20 # increment Y position for line spacing
pygame.display.update()
clock.tick(60) # limit frame rate
pygame.quit()
Note: the extra initial pygame.display.update() function call is so you can confirm for yourself that multiple calls are unrelated to your original error.
i wanted to draw a circle in pygame that is empty, i mean you should see behind surface through it.
i wrote a code that find all the colored pixels in a surface and empty their coordinates in the other surface.
def draw_empty(surf1, surf2, pos):
"""makes surf2 pixels empty on surf1"""
pixobj2 = pygame.PixelArray(surf2)
empty = []
for x in range(len(pixobj2)):
for y in range(len(pixobj2[x])):
if pixobj2[x][y]!=0:
empty.append((x,y))
del pixobj2
pixobj1 = pygame.PixelArray(surf1)
for pix in empty:
pixobj1[pix[0]+pos[0]][pix[1]+pos[1]] = 0
del pixobj1
window = pygame.display.set_mode((600, 600))
white_surf = pygame.surface.Surface((600, 600))
white_surf.fill((255, 255, 255))
circle_surf = pygame.surface.Surface((50, 50))
pygame.draw.circle(circle_surf, (1, 1, 1), circle_surf.get_rect().center, 25)
pic = pygame.image.load('pic.png')
pic = pygame.transform.scale(pic, (600, 600))
done = False
while not done:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
draw_empty(white_surf, circle_surf, (200, 200))
window.blit(pic, (0, 0))
window.blit(white_surf, (0, 0))
pygame.display.update()
pygame.time.Clock().tick(60)
i expected to see background picture through that circle but the circle i just black, is this even possible to do that? can somebody help me with this?
P.S.
by empty, i mean like a hole in my front surface
Just use the draw() function:
pygame.draw.circle(Surface, color, pos, radius, width=0)
width represents the size of the circumference curve. When it is 0, the circle is solid. If you set it to 1, the edge will be only one pixel wide (an empty circle).
edit: to draw a circle with a sprite, you can use this code†:
import pygame
import pygame.gfxdraw
IMG = pygame.Surface((30, 30), pygame.SRCALPHA)
pygame.gfxdraw.aacircle(IMG, 15, 15, 14, (0, 255, 0))
pygame.gfxdraw.filled_circle(IMG, 15, 15, 14, (0, 255, 0))
class img(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = IMG
self.rect = self.image.get_rect(center=(150, 200))
†Credit for code goes to dunker_wanderer
You can draw the shape the same way you would if it had a color or surface image, and then just set the fill to a transparent color using:
empty = (255,255,255,0)
surface.fill(empty)
You will have to figure out how to add this to your code without breaking it, or rewrite it but, first you will need to make a set with three numbers
white = (255, 255, 255)
next use the function
pygame.draw.rect(canvas_name, set_name, (100, 100, 100, 100))
last use
pygame.display.flip()
#Initializes pygame import pygame, random, sys, time, math pygame.init()
#Creates screen screen = pygame.display.set_mode((400, 350)) pygame.display.set_caption("Game")
#Initializes screen running = True while running: pass
#Colors screen white = (255, 255, 255) screen.fill(white)
#Draws rectangle num1 = random.randrange(0, 255) num2 = random.randrange(0, 255) num3 = random.randrange(0, 255)
color = (num1, num2, num3) pygame.draw.rect(screen, color, pygame.Rect(100, 100, 60, 60), )
pygame.display.flip()
#Made by and only by the kid genius Nathan
I'm trying to make a simple Pygame application where some colors are blended with colors under them. Here is my code:
code-listing 1:
import pygame, sys, time
from pygame.locals import *
#define constants
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 600
FPS = 60
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), 0, 32)
alpha = 0
increasing = True
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#fill screen with red
screen.fill(pygame.Color(247, 25, 0,255))
alpha_surface = pygame.Surface((screen.get_rect().width, screen.get_rect().height))
alpha_surface = alpha_surface.convert_alpha()
surfaceRect = alpha_surface.get_rect()
#fill surface with orange
pygame.draw.rect(alpha_surface, (247, 137, 0, 255), (0, 0, 480, 480))
#fill surface with translucent yellow
pygame.draw.rect(alpha_surface, (220, 247, 0, alpha), (120, 120, 360, 360))
#fill surface with green
pygame.draw.rect(alpha_surface, (0, 247, 4), (240, 240, 240, 240))
#fill surface with translucent blue
pygame.draw.rect(alpha_surface, (0, 78, 247, alpha), (360, 360, 120, 120))
screen.blit(alpha_surface, (120,120))
pygame.display.update()
clock.tick(FPS)
if increasing:
alpha += 1
if alpha > 255:
alpha = 255
increasing = False
else:
alpha -= 1
if alpha < 0:
alpha = 0
increasing = True
the code is supposed to make it so the yellow rectangle blends with the orange rectangle and the blue rectangle with the green rectangle. Instead I am getting something that goes from this:
figure 1: original state with yellow and blue opacity set to 0%
to this:
figure 2: final state with yellow and blue opacity set to 100%
As you can see the yellow and blue rectangles not only blend with the red rectangle (screen surface) but they also make a hole to the orange and green rectangle so that we can see the red rectangle through them.
If you want to blend different layers, then you have to create different pygame.Surfaces or images. A Surface can be genrated by loading an image (pygame.image) or by constructing a pygame.Surface object.
Create a surface completely transparent Surface with per pixel alpha. Use pygame.Surface.convert_alpha to change the pixel format of an image including per pixel alphas. Fill the Surface with a transparent color (e.g pygame.Color(0, 0, 0, 0)):
Minimal example:
import pygame
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((600, 600), 0, 32)
def transparentSurface(size):
surface = pygame.Surface(size).convert_alpha()
surface.fill((0, 0, 0, 0))
return surface
alpha, increase = 0, 1
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
screen.fill(pygame.Color(247, 25, 0,255))
alpha_surface1 = transparentSurface(screen.get_size())
pygame.draw.rect(alpha_surface1, (247, 137, 0, 255), (120, 120, 480, 480))
alpha_surface2 = transparentSurface(screen.get_size())
pygame.draw.rect(alpha_surface2, (220, 247, 0, alpha), (240, 240, 360, 360))
alpha_surface3 = transparentSurface(screen.get_size())
pygame.draw.rect(alpha_surface3, (0, 247, 4), (360, 360, 240, 240) )
alpha_surface4 = transparentSurface(screen.get_size())
pygame.draw.rect(alpha_surface4, (0, 78, 247, alpha), (480, 480, 120, 120) )
screen.blit(alpha_surface1, (0,0))
screen.blit(alpha_surface2, (0,0))
screen.blit(alpha_surface3, (0,0))
screen.blit(alpha_surface4, (0,0))
pygame.display.update()
alpha += increase
if alpha < 0 or alpha > 255:
increase *= -1
alpha = max(0, min(255, alpha))
pygame.quit()
So I fell against this weird problem where I can't fill the screen. My code is
#Import all modules
import pygame
import random
import math
import time
#Colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
ORANGe = (255, 115, 0)
YELLOW = (242, 255, 0)
BROWN = (115, 87, 39)
PURPLE = ( 298, 0, 246)
GRAY = ( 168, 168, 168)
BLUE = ( 0, 0, 255)
pygame.init()
# Clock
clock = pygame.time.Clock()
#Screen
screenx = 1000
screeny = 700
screen = pygame.display.set_mode([screenx,screeny])
#Title
pygame.display.set_caption("OUCH Version 0.1")
#Classes (if any)
#Variables
sound = True
password = False
titlescreen = True
#Booleans
#Sounds
#The graphics
#Positions
#Time management
fps = 60
#Other Things
#Main Loop__________________________________________________
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if titlescreen:
screen.fill(WHITE)
#Flip the Display
pygame.display.flip
clock.tick(fps)
#If done
pygame.quit()
I just updated to python 3.4, may that be the problem. Or am I overlooking something. I'm trying to fill the screen white but every time I run it the screen always turns out to be black. Thank you for any help. :)
What I have tried__________
I tried looking if theirs something with the color WHITE, but when I try any other color it does not work. I also tried just screen filling with ought the Boolean yet that doesn't work either. I have also tried not doing if titlescreen:, but if titlescreen == True: then bla bla
You forgot to put brakets:
pygame.display.flip()