Below is a program using pygame which updates the histogram as values change.
However after a few seconds of running, the program freezes. Can someone point out the error?
import random
import pygame
SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
FRAME_RATE = 50
BACKGROUND_COLOR = pygame.Color("white")
BAR_COLOR = pygame.Color("Black")
BUCKET_CNT = 20
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
screen.fill(BACKGROUND_COLOR)
buckets = BUCKET_CNT*[0]
bar_w = SCREEN_WIDTH / BUCKET_CNT
clock = pygame.time.Clock()
background = pygame.Surface(screen.get_size())
background.fill(BACKGROUND_COLOR)
while True:
clock.tick(FRAME_RATE)
random.seed()
idx = random.randrange(BUCKET_CNT)
buckets[idx] += 1
# Create rectangles representing bars in the histogram.
bars = [pygame.Rect(i*bar_w,
SCREEN_HEIGHT - buckets[i],
bar_w, buckets[i]) for i in range(BUCKET_CNT)]
# Draw bars on the background
[pygame.draw.rect(background, BAR_COLOR, b, 5) for b in bars]
# Blit the background
screen.blit(background, (0, 0))
# Show "stuff" on the screen
pygame.display.flip()
EDIT
These are very good suggestions. I've changed my code using to follow them, however the code still freezes. Here is how the code looks now:
import random
import pygame
SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
FRAME_RATE = 50
BACKGROUND_COLOR = pygame.Color("white")
BAR_COLOR = pygame.Color("Black")
BUCKET_CNT = 20
GROWTH_RATE = 10
pygame.init()
screen = pygame.display.set_mode(SCREEN_SIZE)
screen.fill(BACKGROUND_COLOR)
buckets = BUCKET_CNT*[0]
bar_w = SCREEN_WIDTH / BUCKET_CNT
clock = pygame.time.Clock()
background = pygame.Surface(screen.get_size())
background.fill(BACKGROUND_COLOR)
# Create rectangles representing bars in the histogram.
bars = [pygame.Rect(i*bar_w,
SCREEN_HEIGHT - buckets[i],
bar_w, buckets[i]) for i in range(BUCKET_CNT)]
random.seed()
while True:
clock.tick(FRAME_RATE)
idx = random.randrange(BUCKET_CNT)
buckets[idx] += 1
bars[idx].inflate_ip(0, GROWTH_RATE)
# Draw bars on the background
pygame.draw.rect(background, BAR_COLOR, bars[idx])
# Blit the background
screen.blit(background, (0, 0))
# Show "stuff" on the screen
pygame.display.flip()
I want to apologize for taking everybody's time. As it turns out there is no issue with the code. My work machine was simply overloaded with various processes. It is running a multi-threaded test and a virtual machine (which is currently compiling a very large code base). That all explains why my program was freezing. Thank you DJMcMayhem for trying out the code. A special shout out to Alex Van Leiw. Thanks to you I learned a few new things about pygame today.
Related
I am struggling to move the sprite correctly. Instead of smooth move I can see blur move and I do not know how to solve it.
Is there any chance you can point what I do incorrectly ?
My target with it to drop the pizza so it hits the bottom and bounce back and bounc back if it hits the top and again the bottom -> bounce -> top -> bounce etc. etc.
import pygame
gravity = 0.5
class PizzaSprite:
def __init__(self, image, spritepos):
self.image = image
self.spos = spritepos
(x, y) = spritepos
self.posx = x
self.posy = y
self.xvel = 1
self.yvel = 1
print "x ", x
print "y ", y
def draw(self, target_surface):
target_surface.blit(self.image, self.spos)
def update(self):
self.posy -= self.yvel
self.spos = (self.posx, self.posy)
return
def main():
pygame.init()
screen_width = 800
screen_height = 600
x = screen_width
y = screen_height
screen = pygame.display.set_mode((screen_width, screen_height))
wall_image = pygame.image.load("wall.png")
sky_image = pygame.image.load("sky.png")
pizza_image = pygame.image.load("pizza.png")
screen.blit(wall_image,(0,200))
screen.blit(sky_image,(0,0))
all_sprites = []
pizza1 = PizzaSprite(pizza_image, (x/2, y/2))
all_sprites.append(pizza1)
while True:
ev = pygame.event.poll()
if ev.type == pygame.QUIT:
break
for sprite in all_sprites:
sprite.update()
for sprite in all_sprites:
sprite.draw(screen)
pygame.display.flip()
pygame.quit()
main()
in the beginning of your main game while loop add
white = (255,255,255)
screen.fill(white)
let me give you a small analogy of what is happening now,
you have paper and a lot of pizza stickers with the intent to make a flip book. To make the illusion of movement on each piece of paper you place a sticker a little bit lower. The screen.fill command essentially clears the screen with the rgb color value you give it. When you dont fill the screen essentially what you are doing is trying to make that flipbook on one piece of paper. You just keep placing more and more stickers a little bit lower making a blur when what you want is one on each page.
and place
pygame.init()
screen_width = 800
screen_height = 600
x = screen_width
y = screen_height
screen = pygame.display.set_mode((screen_width, screen_height))
wall_image = pygame.image.load("wall.png")
sky_image = pygame.image.load("sky.png")
all outside of your main game loop assuming you wont be making changes to these variables ever in your program it is tedious and inefficient to redefine screen,,x,y,and your two images over and over again if they dont change.
so to sum it all up:
use the screen.fill(white) command to reset the color of your screen
You only need to import pngs and define variables once if they are never going to change and don't need them in your main loop
hope this helps clear things up.
I want to make the background perfect for the running screen, but the vertical axis of the picture doesn't stretch even if I keep changing it in the code.
How can I get it right?
import pygame
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 600
pygame.init()
SCREEN = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("pygame test")
screen= pygame.image.load("그림.png")
SCREEN.blit(screen,(0,100))
SCREEN.blit(player, player_Rect)
pygame.display.flip()
if i add screen=pygame.transform.scale(500,600) this code
print black screen
pygame.transform.scale() takes two arguments, the source surface and a tuple with the size of the new and scaled surface:
screen=pygame.transform.scale(500,600)
screen = pygame.transform.scale(screen, (500, 600))
Scale the surface immediately after loading it:
screen = pygame.transform.scale(pygame.image.load("그림.png").convert_alpha(), (500, 600))
This question already has answers here:
Lag when win.blit() background pygame
(2 answers)
Closed 2 years ago.
I want to make a (a bit bigger) game in PyGame, but even with this simple code I just get arround 10 fps instead of 60? Here's the code:
import pygame
res = 1280,720
display = pygame.display.set_mode(res, pygame.RESIZABLE)
pygame.display.set_caption("Test")
background = pygame.transform.smoothscale(pygame.image.load("Background.png"), res)
a = 0
while True:
pygame.time.Clock().tick(60)
display.fill((0,0,0))
a += 10
display.blit(background, (0,0)) #Without this line: arround 20 fps
pygame.draw.rect(display,(255,0,0), (a,8,339,205))
pygame.display.update()
pygame.quit()
What am I doing wrong?
Thank you!
Try the following optimizations:
Use the convert() method on your image pygame.image.load("Background.png").convert()). This makes the animation about 5 times faster.
Instead of re-blitting entire background every frame, only update the changed parts of the screen.
You don't need to clear the screen before drawing the background.
Use the same Clock instance every frame.
Here's the code:
import pygame
pygame.init()
res = (1280, 720)
display = pygame.display.set_mode(res, pygame.RESIZABLE)
pygame.display.set_caption("Test")
background = pygame.transform.smoothscale(pygame.image.load(r"E:\Slike\Bing\63.jpg").convert(), res)
a = 0
clock = pygame.time.Clock()
display.blit(background, (0, 0))
pygame.display.update()
while True:
clock.tick(60)
rect = (a,8,339,205)
display.blit(background, rect, rect) # draw the needed part of the background
pygame.display.update(rect) # update the changed area
a += 10
rect = (a,8,339,205)
pygame.draw.rect(display, (255,0,0), rect)
pygame.display.update(rect) # update the changed area
I am trying to make a game in python and pygame. I want to make a game with decent resolution, but I can't get higher resolution, any ways?
import pygame
import time
import random
import os
pygame.init()
height = 600
width = 800
window = pygame.display.set_mode((width,height))
white = 255,255,255
black = 0,0,0
red = 255,0,0
blue = 0,0,255
def update():
pygame.display.update()
def game():
stop_game = False
while not stop_game:
window.fill(white)
loaded_image = pygame.image.load("Player.png")
loaded_image = pygame.transform.scale(loaded_image,(150,150))
window.blit(loaded_image,(0,0))
update()
game()
print pygame.display.list_modes()
This will list the available display modes for you. Mine goes up to (2646, 1024).
I am making an RPG in Pygame and I am having an error that is ruining the point of a fade. What is happening is that when I try to put the fading code in a function, so I don't have to have it dozens of times, it completely skips over the fading part and just flashes.
This is the working code
import pygame
import time
delay = time.sleep
white = 255,255,255
black = 0,0,0
pygame.display.set_caption("Liam's RPG")
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
menuIntro = False
while not menuIntro:
menuIntro = True
logo = pygame.image.load("logo.png").convert()
logo.set_alpha(0)
for alpha in range(255):
screen.fill(black)
screen.blit(logo, (100,35))
logo.set_alpha(alpha)
clock.tick(200)
pygame.display.update()
delay(1.5)
This is the code that causes the fade to skip and mess up
import pygame
import time
delay = time.sleep
white = 255,255,255
black = 0,0,0
pygame.display.set_caption("Liam's RPG")
pygame.init()
pygame.font.init()
screen = pygame.display.set_mode((800,600))
clock = pygame.time.Clock()
menuIntro = False
sprite = ""
fade_x = 0
fade_y = 0
clocking = clock.tick(10)
def fading(sprite,clocking,fade_x,fade_y):
logo = pygame.image.load(sprite + ".png").convert()
logo.set_alpha(0)
for alpha in range(255):
screen.fill(black)
screen.blit(logo, (fade_x,fade_y))
logo.set_alpha(alpha)
clocking
while not menuIntro:
menuIntro = True
sprite = 'logo'
fade_x = 100
fade_y = 35
clocking = clock.tick(200)
fading(sprite,clocking,fade_x,fade_y)
pygame.display.update()
delay(1.5)
Here is the logo thing that I am trying to fade in, it needs to be in the same directory as the file
In correct code you have tick and update inside for alpha loop. In incorrect code you have tick and update outside this loop - and it doesn't work as you expect.