Need assistance with blit function - python

I am making a new game in pygame and the blit function just is not working for me, it should be simple but I just don't know what's wrong.
import pygame
import sys
pygame.init()
clock = pygame.time.Clock()
game = True
WIDTH = 160
HEIGHT = 144
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
level = pygame.image.load('RBYG/Level.png')
def redrawGameWindow():
screen.blit(level, (0, 0))
while game:
screen.blit(level,(0, 0))
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
redrawGameWindow()
clock.tick(60)

You're missing pygame.display.flip(), you have to do that after you blit or nothing will happen. Your redrawGameWindow() function should flip the display. Also, you're calling screen.blit(level, (0, 0)) twice in your main loop, which may be unnecessary; try just keeping one of them (just make sure to flip() after).

Related

Pygame Window not Responding but no Error

I recently started learning pygame but I've had an error which shows pygame not responding after some time. It would really help if someone could find my bug! Here's my code:
import pygame
pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Space Invaders")
BLUE = (0, 0, 255)
def player():
pygame.draw.rect(screen,BLUE,(200,150,100,50))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0,0,0))
pygame.display.update()
From my limited knowledge of pygame, it is likely that you set running = True and did a while True: statement, causing too much memory to be used and the script to crash after some time.
You need a proper termination that uses pygame.quit(), followed by sys.exit(). Setting running = False does not terminate the script.
Try this code:
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
sys.exit()
Reference
Try adding a pygame.quit() at the end of your code:
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invaders")
BLUE = (0, 0, 255)
def player():
pygame.draw.rect(screen, BLUE, (200, 150, 100, 50))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((0,0,0))
pygame.display.update()
pygame.quit()

video system not initialized

When I run the following code I get an error. Most of the other similar posts talks about pygame.quit()
sys.exit() which I have, but still see the error
import pygame, sys, math
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode((width, height))
ball = pygame.image.load("GolfBall.png").convert_alpha()
ball_rect= ball.get_rect()
is_playing= True
while is_playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_playing= False
screen.fill((0,0,0))
screen.blit(ball, ball_rect)
pygame.display.flip()
pygame.time.wait(20)
pygame.quit()
sys.exit()
The error is
Traceback (most recent call last):
File "C:/Users/.............................", line 10, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
pygame.quit() should be out of the loop:
import pygame, sys, math
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode((width, height))
ball = pygame.image.load("GolfBall.png").convert_alpha()
ball_rect= ball.get_rect()
is_playing= True
while is_playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_playing= False
screen.fill((0,0,0))
screen.blit(ball, ball_rect)
pygame.display.flip()
pygame.time.wait(20)
pygame.quit()
sys.exit()
Otherwise after the first iteration of the loop, pygame.quit() would have been called and on the second iteration the error would have occured.
You have to care about the Indentation
While pygame.quit() has to be done after the main application loop (as mentioned in another answer and the comments), drawing the scene and the update of the display has to be done in the main application loop rather than the event loop:
import pygame, sys, math
pygame.init()
size = width, height = 600, 400
screen = pygame.display.set_mode((width, height))
ball = pygame.image.load("GolfBall.png").convert_alpha()
ball_rect= ball.get_rect()
# application loop
is_playing= True
while is_playing:
# event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_playing= False
#<--| INDENTATION
# draw the scene
screen.fill((0,0,0))
screen.blit(ball, ball_rect)
pygame.display.flip()
pygame.time.wait(20)
#<--| INDENTATION
# quit pygame
pygame.quit()
sys.exit()
Note, the event loop is executed once for each event, but the application loop is executed once for each frame.

Pygame used in VS Code with Pygame Snippets [duplicate]

This question already has answers here:
How to get keyboard input in pygame?
(11 answers)
What all things happens inside pygame when I press a key? When to use pygame.event==KEYDOWN
(1 answer)
Closed 2 years ago.
I have installed vs code and added pygame snippets to use pygame library. My big problem is, every time I try to use any key option of pygame, like pygame.KEYDOWN or pygame.QUIT it tells me that QUIT is not a function of pygame. Can someone help me?
Everything else seems to work, like display or surface
even pygame.key.get_pressed() don’t make problems.
import pygame, random, sys
from pygame.locals import *
from pygame.key import *
def set_Background():
screen = pygame.display.set_mode((500,500))
surface = pygame.image.load('Background.png')
surface = pygame.transform.scale(surface, (500, 500))
screen.blit(surface, (0,0))
pygame.display.update()
return screen
def set_Enemy():
enemy = pygame.image.load('Enemy.png')
enemy = pygame.transform.scale(enemy, (50, 50))
return enemy
def set_Player():
player = pygame.image.load('Player.png')
player = pygame.transform.scale(player, (70, 70))
return player
RUNNING = True
while RUNNING:
background = set_Background()
enemy = set_Enemy()
player = set_Player()
enemy_rect = enemy.get_rect()
player_rect = player.get_rect()
e_x = random.randint(10,450)
e_y = random.randint(10,450)
background.blit(enemy, (e_x, e_y))
pygame.display.update()
for event in pygame.event.get():
key = pygame.key.get_pressed()
if event.type == key[pygame.K_ESCAPE]:
#module pygame has no K_ESCAPE member
sys.exit()
if event.type == pygame.QUIT:
#says module pygame has no QUIT member
sys.exit()
pygame.key.get_pressed() shouldn't be in the event loop, but in the main while loop. In the event loop you need to check if the event type is pygame.QUIT and then set the running flag to False.
Here's a fixed version:
import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
running = True # Uppercase names are for constants not variables.
while running:
# The event loop.
for event in pygame.event.get():
# If a pygame.QUIT event is in the queue.
if event.type == pygame.QUIT:
running = False
# To check if it was a `KEYDOWN` event.
elif event.type == pygame.KEYDOWN:
# If the escape key was pressed.
if event.key == pygame.K_ESCAPE:
running = False
# Use pygame.key.get_pressed to see if a key is held down.
# This should not be in the event loop.
key = pygame.key.get_pressed()
if key[pygame.K_UP]:
print('up arrow pressed')
screen.fill((30, 30, 30))
pygame.display.flip()
clock.tick(60)
Add from pygame.locals import * at the top of your code.
You are mixing two types of key presses in one go. You should instead either
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SOMEKEY:
do_something()
or
keys = pygame.key.get_pressed()
if keys[pygame.K_somekey]:
do_something()
so the code above with the pygame.key.get_pressed() should not be in the event loop

Cannot find reference 'load' in 'image.py'

I want to blit out my image on the screen with Pygame, but it doesn't work; It gives me a warning :
Cannot find reference 'load' in 'image.py'
And during execution time, it gives no such fatal error. All I get is a blank white screen with no image. I have referred to this and this, but just does not seem to work. This is my code:
import pygame
class Bouncer(object):
display_width = 600
display_height = 400
color_white = (255, 255, 255)
clock = pygame.time.Clock()
game_exit = False
bar_image = pygame.image.load('bar.png') # <------ here is the issue
def __init__(self):
pygame.init()
self.game_display = pygame.display.set_mode((self.display_width,
self.display_height))
pygame.display.set_caption('Bouncy Bouncer')
def showBar(self):
self.game_display.blit(self.bar_image, (10, 10))
def gameLoop(self):
while not self.game_exit:
for event in pygame.event.get():
#on close quit
if event.type == pygame.QUIT:
self.game_exit = True
pygame.quit()
quit()
#on key press quit
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
self.game_exit = True
pygame.quit()
quit()
self.showBar()
self.game_display.fill(self.color_white)
pygame.display.update()
self.clock.tick(60)
game_instance = Bouncer()
game_instance.gameLoop()
I called fiest called my image then the fill function, so it overlapped the white background, thus, covering the image. So, what I did was first call the background, then the image.

Pygame draw interactively

Hi I'm having some trouble realizing my ideas. I first wanted to draw rectangles with two mouse clicks, but it didn't work properly so i reduced it to this: to draw fixed-size rectangles with one mouse click.
However it still doesn't work/...
import pygame
windowSize = (500,500)
white = (255,255,255)
black = (0,0,0)
pygame.init()
screen = pygame.display.set_mode(windowSize)
running = 1
while running:
screen.fill(white)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = 0
THE PROBLEM IS HERE SOMEWHERRE
elif event.type == pygame.MOUSEBUTTONDOWN:
rect = pygame.Rect(event.dict["pos"],(30,50))
pygame.draw.rect(screen,black,rect,1)
pygame.display.flip()
I know there might be a lot of conceptual errors with my code... please help!
You are filling white the entire screen every tick. So after you actually draw the screen become blank again on the next tick. Just move screen.fill(white) out of main cycle:
import pygame
windowSize = (500,500)
white = (255,255,255)
black = (0,0,0)
pygame.init()
screen = pygame.display.set_mode(windowSize)
running = 1
screen.fill(white)
while running:
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
elif event.type == pygame.MOUSEBUTTONDOWN:
rect = pygame.Rect(event.dict["pos"],(30,50))
pygame.draw.rect(screen,black,rect,1)
pygame.display.flip()

Categories

Resources