Pycharm displays an anomally when running a pygame code - python

I am having a weird display when I run a simple Pygame. The game runs perfectly but the screen is weird.
Here's the code:
import pygame
pygame.init()
window = pygame.display.set_mode((500, 500))
pygame.display.set_caption('First Game')
pygame.display.update()
x = 50
y = 50
width = 40
height = 60
vel = 5
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(window, (255, 0, 0), (x, y, width, height))
pygame.quit()

You need to update the screen every frame. Put pygame.display.update() inside your main_loop, and remove it from original positon:
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(window, (255, 0, 0), (x, y, width, height))
pygame.display.update()
This should work

Related

Why does my display draw behind my screen in pygame

So I'm learning pygame and I want to use pixel art for the sprites and I'm trying to scale up my display but it seems it's drawing behind the main display instead of Infront of it
import sys, math
import pygame
from pygame.locals import *
pygame.init()
fps = 60
is_running = True
clock = pygame.time.Clock()
window_size = [600, 400]
scale_size = [300, 200]
screen = pygame.display.set_mode(window_size)
display = pygame.Surface(scale_size)
player_img = pygame.image.load("images/player.png")
player_pos = [150, 100]
player_move_speed = 3
player_y_momentum = 1
while is_running:
for event in pygame.event.get():
if event.type == QUIT:
is_running = False
pygame.quit()
sys.exit()
surf = pygame.transform.scale(display, window_size)
screen.blit(surf, (0, 0))
surf.blit(player_img, player_pos)
surf.fill((255, 255, 255))
pygame.display.update()
clock.tick(fps)
I've tried to troubleshoot the problem but I can't seem to find the solution
The order of drawing/filling is important!
Change the order to the following:
while is_running:
for event in pygame.event.get():
if event.type == QUIT:
is_running = False
pygame.quit()
sys.exit()
surf = pygame.transform.scale(display, window_size)
surf.fill((255, 255, 255))
surf.blit(player_img, player_pos)
screen.blit(surf, (0, 0))
pygame.display.update()
clock.tick(fps)

When a number is blit on the pygame window when the user presses a numeric key more than once it overwrites. I want a way for it to not overwrite

I am making a Calculator with PyGame and I am making good progress. But When I blit a number which is pressed by the user, it overwrites it. Can anyone tell me how to set a rule like if a character is in the window then when you blit it again you blit it by it's side or away from it.
Here's my code
import pygame, math
pygame.init()
screen_width = 1200
screen_height = 500
gameWindow = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Calculator')
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
black = (0, 0, 0)
grey = (128, 128, 128)
font = pygame.font.SysFont('comicsans', 40)
fps = 60
clock = pygame.time.Clock()
def text_screen(text, color, x, y):
screen_text = font.render(text, True, color)
gameWindow.blit(screen_text, [x ,y])
def welcome_screen(run):
clock.tick(fps)
gameWindow.fill(grey)
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
pygame.display.update()
def gameLoop(run):
clock.tick(fps)
gameWindow.fill(grey)
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
if event.type == pygame.KEYDOWN:
key = pygame.key.name(event.key)
iter(key)
if key.isnumeric():
text_screen(key, red, 45, 45)
pygame.display.update()
if __name__ == "__main__":
gameLoop(False)
You can fill the screen first.
def text_screen(text, color, x, y):
screen_text = font.render(text, True, color)
gameWindow.fill(grey)
gameWindow.blit(screen_text, [x, y])
here is a possibility.
def gameLoop(run):
clock.tick(fps)
gameWindow.fill(grey)
pos_x = 45
pos_y = 45
while not run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = True
if event.type == pygame.KEYDOWN:
key = pygame.key.name(event.key)
iter(key)
if key.isnumeric():
pos_x += 45
pos_y += 45
text_screen(key, red, pos_x, pos_y)
pygame.display.update()

Why is my character not appearing in my pygame window?

so i'm coding this pygame for fun just to test my knowledge in this area and i'm facing a minor problem. My character is not appearing on the pygame window except for like a second when i press the X button to close the tab, here is what i've coded so far.
import pygame
pygame.init
window = pygame.display.set_mode((700 , 700))
pygame.display.set_caption("practice")
#Character attributes
x = 50
y = 50
width = 30
height =30
velocity = 5
#quit
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(window, (0, 0, 255), (x , y, width, height))
pygame.display.update()
pygame.quit()
It's a common problem when people forget to indent code inside nested loops right. Here drawing occurs only when event happens, but it should be done on every frame. To fix this unindent render code
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.draw.rect(window, (0, 0, 255), (x , y, width, height))
pygame.display.update()

Changing default PyGame background from black to white

I am trying to change the default color from black to white.
not to fill it.
My current code where I fill it:
import pygame
pygame.init()
size = 600, 600
surface = pygame.display.set_mode(size, pygame.RESIZABLE)
surface.fill((255, 255, 255))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.display.update()
It works however it is being shown as black for a second and then it changes itself, and i am looking for a solution where in advance it starts with white background. Thanks in advance
Maybe this will help you
import pygame
pygame.init()
size = 600, 600
surface = pygame.display.set_mode(size, pygame.RESIZABLE)
surface.fill((255, 255, 255))
while True:
surface.fill((255, 255, 255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
pygame.display.update()
By #starbuck45 I used the flag pygame.HIDDEN.
import pygame
pygame.init()
size = 600, 600
surface = pygame.display.set_mode(size, pygame.RESIZABLE | pygame.HIDDEN)
surface.fill("white")
pygame.display.set_mode(size)
line = pygame.draw.line(surface, "black", (0, 0), (600, 600))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
and it worked fine!

Make pygame draw cubes wherever the user clicks

I want to make a kind of "level editor" as an exercise.
I've got this code:
import pygame
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
def draw(self):
pygame.draw.rect(screen, (255, 0, 0), self.square)
cube = Cube()
drawing_cube = False
drawing_cube2 = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
cube.update()
drawing_cube = True
screen.fill((0, 255, 0))
if drawing_cube:
cube.draw()
pygame.display.flip()
pygame.quit()
However, it doesn't create multiple squares; it just re-locates the already created square.
It's doing exactly what you told it to do: update the one and only Cube object in the game, and then redraw the screen and that one object. If you want multiple cubes, you have to create each one. Perhaps something like this:
cube_list = [] # List of all cubes
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN: # Make a new cube
cube = Cube()
cube.update()
cube_list.append(cube)
drawing_cube = True
screen.fill((0, 255, 0))
if drawing_cube: #New cube made; draw them all.
for cube in cube_list:
cube.draw()
pygame.display.flip()
Now, as an exercise for the student, can you simplify this so that it merely adds the most recent cube to the existing screen, instead of redrawing the entire game area for each new cube?
Thanks, Prune! but for anyone that is reading this after me, here is the full code:
import pygame
cube_list = []
running = True
pygame.init()
screen = pygame.display.set_mode((800, 500))
class Cube:
def update(self):
self.cx, self.cy = pygame.mouse.get_pos()
self.square = pygame.Rect(self.cx, self.cy, 50, 50)
def draw(self):
pygame.draw.rect(screen, (255, 0, 0), self.square)
cube = Cube()
drawing_cube = False
drawing_cube2 = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN: # Make a new cube
cube = Cube()
cube.update()
cube_list.append(cube)
drawing_cube = True
screen.fill((0, 255, 0))
if drawing_cube: #New cube made; draw them all.
for cube in cube_list:
cube.draw()
pygame.display.flip()
pygame.quit()
Cheers!
P.S I've marked your answer as accepted

Categories

Resources