Screenfilling not working - python

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()

Related

PyGame won't load the images on window and crashes immediately

So I was making a game in python with pygame and I had some assets as characters. I coded everything correctly. But when I run the program none of the images show up and the window crashes immediately.
import pygame
import os
import random
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Fly game")
FPS = 60
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
FPS = 60
VEL = 5
BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)
PLAYER_WIDTH, PLAYER_HEIGHT = 55, 40
SKY = pygame.image.load(
os.path.join('C:\\Users\\Kostadin Klemov\\Desktop\\Programms\\Python\\projects\\Fly game\\Assets\\SKY.jpg')), (WIDTH, HEIGHT)
JETPACK_MAN_IMAGE = pygame.image.load(
os.path.join('C:\\Users\\Kostadin Klemov\\Desktop\\Programms\\Python\\projects\\Fly game\\Assets\\JETPACK_MAN.jpg'))
JETPACK_MAN = pygame.transform.scale(
JETPACK_MAN_IMAGE, (PLAYER_WIDTH, PLAYER_HEIGHT))
FLY_IMAGE = pygame.image.load(
os.path.join('C:\\Users\\Kostadin Klemov\\Desktop\\Programms\\Python\\projects\\Fly game\\Assets\\FLY.png'))
FLY = pygame.transform.scale(
FLY_IMAGE, (PLAYER_WIDTH, PLAYER_HEIGHT))
def draw_window(jetpack, fly):
WIN.blit(SKY, (0, 0))
pygame.draw.rect(WIN, BLACK, BORDER)
WIN.blit(JETPACK_MAN, (jetpack.x, jetpack.y))
WIN.blit(FLY, (fly.x, fly.y))
pygame.display.update()
def main():
jetpack = pygame.Rect(225, 250, PLAYER_WIDTH, PLAYER_HEIGHT)
fly = pygame.Rect(675, 250, PLAYER_WIDTH, PLAYER_HEIGHT)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
draw_window(jetpack, fly)
main()
if __name__ == "__main__":
main
No error showed up so I didn't know what was wrong.
If you can, please check out the code and try to fix it!
This is an indentation error.
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
# <---- INDENTATION
# this should be in the while loop, the "game loop", not in the quit condition
draw_window(jetpack, fly)
Also, you probably don't want to call main() after you program terminates. (Assuming you want people to be able to exit your game easily.
if __name__ == "__main__":
main
This main does nothing, it needs to be called, like main(). With the parentheses. It's not "crashing instantly," it's just not running anything.
SKY = pygame.image.load(
os.path.join('C:\\Users\\Kostadin Klemov\\Desktop\\Programms\\Python\\projects\\Fly game\\Assets\\SKY.jpg')), (WIDTH, HEIGHT)
That (WIDTH, HEIGHT) at the end is very suspicious. Presumably you just want the image put in the SKY variable, not another random tuple.
On another note, os.path.join() does nothing if you give it the full path as an argument.

How to debug "pygame.Surface' object is not callable"?

Question
I'm pretty new at Python and Pygame so I figured I'd jump right it head first and work on making a little rpg style game. I keep getting the error message below. I keep checking the code and re-watching the tutorial I'm following and I can't seem to find the problem.
I'm sure it's pretty obvious but I don't see the problem. I'm pretty sure I formatted the code in this post right but I don't know.
Error Message
Traceback (most recent call last):
File "C:\Users\Clayton\PycharmProjects\newgame\main.py", line 42, in <module>
gameDisplay.blit(Tiles.grass(x, y))
TypeError: 'pygame.Surface' object is not callable
Main.py
import pygame
from gamescripts.textures import *
# initialize PyGame
pygame.init()
# display information
# int defines number a an integer (ex1)
display_width = int(800)
display_height = int(600)
tile_size = 32
# color definitions
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
skyblue = (135, 206, 235)
yellow = (255, 255, 0)
# window
gameDisplay = pygame.display.set_mode((display_width, display_height),
pygame.HWSURFACE | pygame.DOUBLEBUF)
pygame.display.set_caption('test game')
clock = pygame.time.Clock()
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
# render graphics
gameDisplay.fill(skyblue)
for x in range(0, 620, tile_size):
for y in range(0, 480, tile_size):
gameDisplay.blit(Tiles.grass(x, y))
# draws everything to window
pygame.display.update()
# num entered is game fps
clock.tick(60)
# quit PyGame
pygame.quit()
# quit python
quit()
Textures.py
import pygame
pygame.init()
class Tiles:
Size = 32
def load_texture(file, Size):
bitmap1 = pygame.image.load(file)
bitmap2 = pygame.transform.scale(bitmap1, (Size, Size))
surface = pygame.Surface((Size, Size), pygame.HWSURFACE | pygame.SRCALPHA)
surface.blit(bitmap2, (0, 0))
return surface
grass = load_texture('graphics\\grass.png', Size)
You have to create an instance of your Tiles class.
Your Tiles class cannot be directly blitted without creating an instance first.
#create a instance of `Tiles`
tile=Tiles()
tile.load_texture('graphics\\grass.png',32)
Implemented into your code:
import pygame
from gamescripts.textures import *
# initialize PyGame
pygame.init()
# display information
# int defines number a an integer (ex1)
display_width = int(800)
display_height = int(600)
tile_size = 32
# color definitions
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
skyblue = (135, 206, 235)
yellow = (255, 255, 0)
# window
gameDisplay = pygame.display.set_mode((display_width, display_height),
pygame.HWSURFACE | pygame.DOUBLEBUF)
pygame.display.set_caption('test game')
clock = pygame.time.Clock()
#create an instance of your `Tile` class
tile=Tile()
tile.load_texture('graphics\\grass.png',32)
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
print(event)
# render graphics
gameDisplay.fill(skyblue)
for x in range(0, 620, tile_size):
for y in range(0, 480, tile_size):
#choose example coordinates for x and y
gameDisplay.blit(tile,(x,y))
# draws everything to window
pygame.display.update()
# num entered is game fps
clock.tick(60)
# quit PyGame
pygame.quit()
# quit python
quit()
import pygame
pygame.init()
You should change your Tile class as well.
class Tiles:
#initialize your class
def__init__(self):
pass
#ALWAYS use self as the first parameter
def load_texture(self,file, Size):
bitmap1 = pygame.image.load(file)
bitmap2 = pygame.transform.scale(bitmap1, (Size, Size))
surface = pygame.Surface((Size, Size), pygame.HWSURFACE | pygame.SRCALPHA)
surface.blit(bitmap2, (0, 0))
return surface
I suggest you really take a look at the the pygame documentation and the python documentation.
You should also look at SO if your question was already posted. For example, I found this example.

How to let pygame text input detect the key in Pygame? [duplicate]

This question already has answers here:
How can I create a text input box with Pygame?
(5 answers)
Closed 5 years ago.
I'm doing a project that using Python and Pygame. I want to ask some question about the key in Pygame.
Thank you Shubhitgarg. I use his/her suggestions(use pygame_textinput), it succeed. Now I want to change the text input position with pressed an enter, but it can't detect the enter. How can I fix it? My real code:
# installing package
import pygame
from pygame import *
import pygame_textinput
pygame.init()
# colour settings
red = (255, 0, 0)
green = (0, 255, 0)
grass_green = (112, 173, 71)
blue = (0, 0, 255)
yellow = (255, 255, 0)
white = (255, 255, 255)
black = (0, 0, 0)
# screen settings
window = pygame.display.set_mode((680, 600))
window.fill(grass_green)
pygame.display.flip()
# font settings
default_font = pygame.font.get_default_font()
big_font = pygame.font.Font(default_font, 96)
font_a = pygame.font.Font(default_font, 50)
font_b = pygame.font.Font(default_font, 30)
font_c = pygame.font.Font(default_font, 18)
# text input settings
textinput = pygame_textinput.TextInput("freeansbold.ttf", 96, True, black, white, 400, 35)
# timer
start = pygame.time.get_ticks()
# text
please_guess_a_number = font_a.render("Please guess a number. ", 1, white)
text_range = font_c.render("from to", 1, white)
wrong_bigger = font_b.render("Sorry, You haven’t guess it rightly. Please try again.(The answer must be bigger.)", 1, white)
wrong_smaller = font_b.render("Sorry, You haven’t guess it rightly. Please try again.(The answer must be smaller.)", 1, white)
correct = font_b.render("Congratulations! You guess it correctly!!", 1, white)
# game
ask_you_to_set_range_1 = True
ask_you_to_set_range_2 = False
while True:
if ask_you_to_set_range_1:
window.blit(please_guess_a_number, (60, 80))
pygame.draw.rect(window, yellow, [60, 200, 230, 300])
pygame.draw.rect(window, yellow, [390, 200, 230, 300])
window.blit(text_range, (10, 550))
pygame.display.flip()
while True:
events = pygame.event.get()
textinput.update(events)
window.blit(textinput.get_surface(), (110, 300))
pygame.display.flip()
if ask_you_to_set_range_2:
while True:
events = pygame.event.get()
textinput.update(events)
window.blit(textinput.get_surface(), (440, 300))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if ask_you_to_set_range_1 and event.key == pygame.K_KP_ENTER:
ask_you_to_set_range_1 = False
ask_you_to_set_range_2 = True
if textinput.update(event):
num1 = textinput.get_text()
text1 = big_font.render(num1, 1, black)
window.blit(text1, (110, 300))
Can anyone teach me to solve it?
You see this https://github.com/ShubhitGarg/pygame-text-input .
You can import this and use the text_input class and blit it on the same or next page
Use this
textinput = pygame_textinput.TextInput()
while True:
screen.fill((225, 225, 225))
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
# Feed it with events every frame
textinput.update(events)
# Blit its surface onto the screen
screen.blit(textinput.get_surface(), (10, 10))
pygame.display.update()

Why do I get a black screen?

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()

animation in pygame

The goal of this program is to make a green circle bounce up and down on the screen in a loop. I have made it go down and back up again, but not to make it go down again.
import pygame, sys, time, random
from pygame.locals import *
from time import *
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("Bounce")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
info = pygame.display.Info()
sw = info.current_w
sh = info.current_h
y= 0
yy= sh
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
sleep(.006)
y+= 1
if y>sh:
pygame.draw.circle(windowSurface, GREEN , (250,yy), 13, 0)
sleep(.0001)
yy +=-1
if yy<-.00001:
y=0
y+= 1
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
In your if yy < -.00001: block, I think you're trying to reset the state back to how it was at the beginning of the program. If that's the case, you forgot to set yy = sh.
In other words:
if yy<-.00001:
y=0
yy=sh # <- add this line
y+= 1
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
In general though, I agree with halflings. This code is way too complicated. I'd suggest using a state variable to record whether the ball is moving up or down, then in the loop check that variable and either increment or decrement the position. When you get to the other end of the screen, swap the state variable to change the direction of the ball.
EDIT: Try this:
import pygame, sys, time, random
from pygame.locals import *
from time import *
pygame.init()
windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("Bounce")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
info = pygame.display.Info()
sw = info.current_w
sh = info.current_h
y = 0
# Initial direction is down
direction = 1
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
#print "Drawing at 250,", y
sleep(.006)
y += direction
if y >= sh:
# Changes the direction from down to up
direction = -1
elif y <= 0:
# Changes the direction from up to down
direction = 1
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Note: Hopefully this runs for you. I don't have pygame installed so I've only run it with the pygame stuff commented out and print calls substituted instead of draw commands. It works for me, anyway.
Your code seem way too complicated for the animation you're trying to do.
What you're looking for to simulate a "bouncing effect" may be the sin function (as it ossilates between -1 and 1) :
t = 0
speed = 0.01
maxHeight = 200
while True:
windowSurface.fill(BLACK)
pygame.draw.circle(windowSurface, GREEN , (250,y), 13, 0)
sleep(.006)
t += 1
y = int( ( sin(t*speed) + 1) * maxHeight / 2 )
pygame.draw.circle(windowSurface, GREEN , (250, y , 13, 0)
pygame.display.update()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Do not forget to put from math import sin at the top of your code.
If you're trying to achieve something realistic, you should probably use the speed and acceleration of the ball instead of using the 'sin' function to simulate a bouncing effect.
Use a while loop for your ball to bounce.
It should loop the code over and over again. What happens when it doesn't go down a second time? Does it throw an error? Does it just freeze?
A easy fix you can add is a counter variable so every time you ball changes directions the variable +=1 so after a set number of changes the program stops. Assuming thats what you want it to do.

Categories

Resources