This question already has answers here:
Why is nothing drawn in PyGame at all?
(2 answers)
Closed 24 days ago.
I'm currently trying to make a chess game with python 3.10.9 using pygame, but i've already encountered a problem, my chess board is blank.
I checked the code over and over but i can't find any error.
Here is my code:
main.py
import pygame
import sys
from const import *
from game import Game
class Main:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Chess')
self.game = Game()
def mainloop(self):
while True:
self.game.show_bg(self.screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update
main = Main()
main.mainloop()
game.py
class Game:
def __init__(self):
pass
# Show Methods
def show_bg(self, surface):
for row in range(ROWS):
for col in range(COLS):
if (row+col)%2==0:
color = (234, 235, 200)
else:
color = (119, 154, 88)
rect = (col*SQSIZE, row*SQSIZE, SQSIZE, SQSIZE)
pygame.draw.rect(surface, color, rect)
const.py
COLS=8
ROWS=8
SQSIZE=WIDTH//COLS
pygame.display.update() not pygame.display.update
Related
This question already has answers here:
Invalid destination position for blit error, not seeing how
(1 answer)
How to draw images and sprites in pygame?
(4 answers)
Closed last month.
import pygame
class popUpWindow:
#Initialization of Window
def __init__(self,capNa,width,height):
self.screen = pygame.init()
self.capNa = capNa
self.width = width
self.height = height
self.screen = pygame.display.set_mode((self.width,self.height))
pygame.display.set_caption(self.capNa)
#Make Player
self.px = float
self.py = float
self.psrc = 'Images/player.png'
self.player_load = pygame.image.load(self.psrc)
def LoadPlayer(self):
self.screen.blit(self.player_load, (self.px,self.py))
pygame.display.update()
def runWindow(self):
self.__init__(self.capNa,self.width,self.height)
self.Is_running = True
self.player_load()
while self.Is_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
pygame.display.update()
def closeWindow(self):
pygame.quit()
I tried to make the code change the player position but I failed!
How can I change the player positions at any time, without returning back to the code.
This question already has answers here:
Why is my PyGame application not running at all?
(2 answers)
Closed 1 year ago.
My friends and I are making a quiz game in PyGame and would like to know how, when the user presses a button, he can go to next question (without leaving the previous text behind).
First of all I would suggest that you go to the PyGame documentation and read up a little about PyGame. (Link)
However to save you time what you have to do is before you draw your new set of shapes/writings on the screen you have to use the function screen.fill(#Your chosen colour). That is the function in PyGame that gets rid of the old screen and allows you to draw new items on to a clear screen without the pervious drawings left on there.
Example:
import pygame
import sys
from pygame.locals import *
white = (255,255,255)
black = (0,0,0)
red = (255, 0, 0)
class Pane(object):
def __init__(self):
pygame.init()
self.font = pygame.font.SysFont('Arial', 25)
pygame.display.set_caption('Box Test')
self.screen = pygame.display.set_mode((600,400), 0, 32)
self.screen.fill((white))
pygame.display.update()
def addRect(self):
self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
pygame.display.update()
def addText(self):
self.screen.blit(self.font.render('Hello!', True, black), (200, 100))
pygame.display.update()
def addText2(self):
self.screen.blit(self.font.render('Hello!', True, red), (200, 100))
pygame.display.update()
def functionApp(self):
if __name__ == '__main__':
self.addRect()
self.addText()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit();
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.screen.fill(white)
self.addRect()
self.addText2() #i made it so it only changes colour once.
display = Pane()
display.functionApp()
In your game loop, before drawing the new frame, fill the frame with the background color.
Example:
ball = pygame.Rect(0,0,10,10)
while True:
mainSurface.fill((0,0,0))
pygame.draw.circle(display,(255,255,255),ball.center,5)
ball.move_ip(1,1)
pygame.display.update()
The key point is the mainSurface.fill which will clear the previous frame.
from __future__ import division
import math
import sys
import pygame
class MyGame(object):
def __init__(self):
pygame.mixer.init()
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init()
self.width = 800
self.height = 600
self.screen = pygame.display.set_mode((self.width, self.height))
self.bg_img = pygame.image.load('pokebg.jpeg')
self.FPS = 30
self.REFRESH = pygame.USEREVENT+1
pygame.time.set_timer(self.REFRESH, 1000//self.FPS)
def run(self):
running = True
while running:
event = pygame.event.wait()
if event.type == pygame.QUIT:
running = False
elif event.type == self.REFRESH:
self.draw()
else:
pass
def draw(self):
self.screen.fill(self.bg_img)
pygame.display.flip()
MyGame().run()
pygame.quit()
sys.exit()
i want to have an image instead of color as a background, but it the error says "invalid color argument" is there a way to fix this or the only way to fill the screen is with color? or use a different filling method?
Just replace the line self.screen.fill(self.bg_img) with self.screen.blit(self.bg_img, self.bg_img.get_rect()). This will work only if the image is the exact same size as the screen. If it isn't you should use pygame.transform.scale.
You can't fill() with image you can only blit() image on screen. If you use loop you can fill all screen.
You can see code and screenshot with screen filled using "ball" image in my answer to question Pygame- window and sprite class - python
import os, sys
import pygame
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.mouse.set_visible(False)
def loadimage(name, colorkey=None):
try:
pygame.image.load(os.path.join(name))
except pygame.error, message:
print "Failed to load image ",name
class Userplane(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = loadimage("userplane.bmp")
def update(self):
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
def main():
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.mouse.set_visible(False)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((255,255,255))
screen.blit(background, (0,0))
pygame.display.flip()
user = Userplane()
allsprites = pygame.sprite.RenderPlain((user))
clock = pygame.time.Clock()
while 1:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
return
elif event.type == KEYDOWN and event.key == K_ESCAPE:
return
allsprites.update()
screen.blit(background(0,0))
pygame.display.flip()
if __name__ == '__main__': main()
hi im very new, ive just finished the codeacademy course and the chimppunch game in the pygame tutorial. Im using almost an exact copy of that game but instead im trying to display a plane at the bottom of the screen instead of a fist. my Userplane class keeps throwing the following error and i dont know why, 'typeerror: 'nonetype' object is not iterable
The reason is in your loadimage function.
It does not return the loaded image, so python is complaining that you are trying to assign self.image, self.rect from a NoneType.
In Python if a function does not return anything, it is assumed to return None.
I have been trying to learn pygame the last day or so, and tried to write a basic program that just has a small image of a leaf falling from the top of the screen.
Nothing appears when I run it, and I imagine I'm missing something obvious with how I'm doing this. (I can tell this is a very inefficient way of doing this as well, so tips would be appreciated!)
Here's the code:
import pygame
from pygame.locals import *
import random
pygame.init()
class Leaf:
def __init__(self):
self.leafimage = pygame.image.load('fallingleaf.jpg').convert()
self.leafrect = self.leafimage.get_rect()
xpos = random.randint(0, 640)
self.leafrect.midtop = (xpos, 0)
def move(self):
self.leafrect = self.leafrect.move([0, 1])
def main():
width= 640
heigth = 480
dimensions = (width, heigth)
screen = pygame.display.set_mode(dimensions)
pygame.display.set_caption('Some Epic Pygame Stuff')
clock = pygame.time.Clock()
leaves = []
for i in range(5):
leaves.append(Leaf())
running = 1
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = 0
for i in leaves:
i.move()
screen.blit(i.leafimage, i.leafrect)
screen.fill((255, 255, 255))
pygame.display.flip()
if __name__ == '__main__': main()
You probably don't want this sequence:
for i in leaves:
i.move()
screen.blit(i.leafimage, i.leafrect)
screen.fill((255, 255, 255))
pygame.display.flip()
You draw the leaves, and then fill the entire screen with white, and then show the screen.
fill the screen, then draw the leaves, then flip()