Related
I'm working on a subprogram code that will make this happy face bounce around the screen and turn different colours. For some reason, the screen turns into that black glitchy screen and when I press exit at the top the face shows for a quick second before the program shuts down. I can't figure out why this is, here is my code and I've included a picture of what happens at first when I run it:
""" Program to show a very basic function
Most of the program is exactly the same as other programs we have done
The main difference is the grouping of code into a function called
drawHappy() to draw a few shapes together
In the main loop we "call" this function whenever we want to draw this
group of shapes
"""
# import the necessary modules
import pygame
import sys
import math
import random
from random import randint
# initialize pygame
pygame.init()
# set the size for the surface (screen)
# note this screen is resizable by the user
screen = pygame.display.set_mode((800, 600), pygame.RESIZABLE)
# set the caption for the screen
pygame.display.set_caption("Happy Face")
#screen width and height
screenW = screen.get_width()
screenH = screen.get_height()
# define colours you will be using
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
# funtion to draw a the "happy face"
# it has 4 parameters passed to it xPos, yPos, radius, and colour
# notice all the shapes are drawn "relative" to the xPos and yPos and the radius
def drawHappy(xPos,yPos,r,colour):
pygame.draw.circle(screen,colour,(xPos,yPos),r,1)
eyeRadius = int(1/6*r)
eyeX = int(xPos-1/3*r)
eyeY = int(yPos- 1/3*r)
pygame.draw.circle(screen,colour,(eyeX,eyeY),eyeRadius,1)
eyeX = int(xPos + 1/3*r)
pygame.draw.circle(screen,colour,(eyeX,eyeY),eyeRadius,1)
wMouth = 1.5*r
xMouth = xPos - 3/4*r
yMouth = yPos - 3/4*r
pygame.draw.arc(screen,colour,(xMouth,yMouth,wMouth,wMouth),math.pi,2*math.pi,1)
randomR = randint(1,300)
r = randomR
randomX = randint(r, 800-r)
randomY = randint(r, 600-r)
dx = 0
dy = 0
x = 100
y = 100
speed = 3
x2 = randomX
y2 = randomY
dx2 = speed
dy2 = -speed
colour_list = [YELLOW, BLACK, BLUE, RED, GREEN]
randomcolour = random.choice(colour_list)
colour = RED
# set up clock to control frames per second
clock = pygame.time.Clock()
FPS = 120
# set main loop to True so it will run
main = True
# main loop
while main:
for event in pygame.event.get(): # check for any events (i.e key press, mouse click etc.)
if event.type == pygame.QUIT: # check to see if it was "x" at top right of screen
main = False # set the "main" variable to False to exit while loop
clock.tick(FPS)
screen.fill(WHITE)
oldx = x
oldy = y
x += dx
y += dy
if x >= 800-r or x <= 0+r:
x = oldx
if y >= 600-r or y <= 0+r:
y = oldy
x2 += dx2
y2 += dy2
if x >= 800-r or x <= 0+r:
dx2 = -dx2
randomcolour = random.choice(colour_list)
colour = randomcolour
if y2 >= 600-r or y2 <= 0+r:
dy2 = -dy2
randomcolour = random.choice(colour_list)
colour = randomcolour
# "call" the function "drawHappy()" to draw the happy face
# this is where we would normally do a pygame.draw or a screen.blit()
# we are "passing" the function 4 values to use(x,y,radius, colour)
# it will use these to know where to draw the happy face
drawHappy(x2,y2,r,colour)
pygame.display.flip()
# quit pygame and exit the program (i.e. close everything down)
pygame.quit()
sys.exit()
First of all, you need to call your draw function inside the loop. Your current code shows only a glimpse of "drawing" because it gets executed once you exit the main loop.
So, put your drawHappy() inside of main loop:
while main:
for event in pygame.event.get(): # check for any events (i.e key press, mouse click etc.)
if event.type == pygame.QUIT: # check to see if it was "x" at top right of screen
main = False # set the "main" variable to False to exit while loop
drawHappy(x2,y2,r,colour)
pygame.display.update()
clock.tick(FPS)
screen.fill(WHITE)
Now you will get a random size "smiley" on the screen, But now it will move on exit only, for the same reason it wouldn't display earlier. Next thing is to make it bounce (move). For this you'll need some kind of update of the coordinates, just like you did in the last part of your code, except they also need to be updated during the loop, not after it.
I suggest making a Class because then it will be easier to manipulate the object.
Also, I found it easier to separate draw and update_coordinates code into separate functions and them call them from main loop for example.
Hope this helps, and if you need more help, ask.
Here, I made a quick solution using parts of your code, there is plenty room for improvement especially for update_smiley_position() method where you can control how "smiley" moves.
Also, if you need multiple objects, a list should be passed instead of single object.
import pygame as pg
import math
import random
pg.init()
clock = pg.time.Clock()
window = pg.display.set_mode((800, 600), pg.RESIZABLE)
pg.display.set_caption("Happy Face")
SCREEN_W = window.get_width()
SCREEN_H = window.get_height()
class Smiley:
def __init__(self, x, y, r, color):
self.x = x
self.y = y
self.r = r
self.color = color
self.create_smiley()
def create_smiley(self):
self.eye_radius = int(1/6 * self.r)
self.eye_x1 = int(self.x - 1/3 * self.r)
self.eye_x2 = int(self.x + 1/3 *self.r)
self.eye_y = int(self.y - 1/3 *self.r)
self.mouth_width = 1.5 * self.r
self.mouth_x = self.x - self.r * 0.75
self.mouth_y = self.y - self.r * 0.75
def draw_smiley(self, win):
pg.draw.circle(win, self.color, (self.x, self.y), self.r, 1)
pg.draw.circle(win, self.color, (self.eye_x1, self.eye_y), self.eye_radius, 1)
pg.draw.circle(win, self.color, (self.eye_x2, self.eye_y), self.eye_radius, 1)
pg.draw.arc(win, self.color, (self.mouth_x, self.mouth_y, self.mouth_width, self.mouth_width), math.pi, 2*math.pi, 1)
def update_smiley_position(self):
if self.x >= SCREEN_H - self.r or self.x <= 0 + self.r:
self.x = random.randint(100, 400)
else:
self.x += 5
if self.y >= SCREEN_W - self.r or self.y <= 0 + self.r:
self.y = random.randint(100, 400)
else:
self.y -= 5
self.create_smiley()
def draw(win, smiley):
win.fill(pg.Color("white"))
smiley.draw_smiley(win)
smiley.update_smiley_position()
pg.display.update()
def main_loop(win, smiley):
clock.tick(30)
for event in pg.event.get():
if event.type == pg.QUIT:
return False
draw(win, smiley)
return True
r = random.randint(1, 300)
x = random.randint(r, SCREEN_W - r)
y = random.randint(r, SCREEN_H - r)
smiley = Smiley(x, y, r, pg.Color("red"))
while main_loop(window, smiley):
pass
pg.quit()
Below is the basic code for creating and managing the pipes of the game:
import pygame as pg
import sys,os,math,time,random
# colours
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
# general stuff
WIDTH = 1024
HEIGHT = 576
FPS = 60
# other
all_events = [pg.QUIT, pg.ACTIVEEVENT, pg.KEYDOWN, pg.KEYUP, pg.MOUSEMOTION,
pg.MOUSEBUTTONUP, pg.MOUSEBUTTONDOWN, pg.VIDEORESIZE,
pg.VIDEOEXPOSE, pg.USEREVENT]
pg.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
clock = pg.time.Clock()
# Class to manage Pipes
class Pipe_Manager:
def __init__(self):
self.pipe_width = 50
self.pipes = []
self.pipe_speed = 5
self.max_tick = 75
self.spawn_tick = self.max_tick
def manage_pipes(self):
self.spawner()
self.manage()
self.display()
def make_pipe(self):
height = random.randint(100,326)
gap = random.randint(100,250)
surf1 = pg.Surface((self.pipe_width, height))
surf1.fill(green)
surf2 = pg.Surface((self.pipe_width, HEIGHT - (height + gap)))
surf2.fill(green)
# surface, (x,y) and vertical height
pipe = [surf1, [WIDTH, 0], height]
pipe2 = [surf2, [WIDTH, height + gap], HEIGHT - (height + gap)]
self.pipes.append(pipe)
self.pipes.append(pipe2)
def spawner(self):
if self.spawn_tick == self.max_tick:
self.make_pipe()
self.spawn_tick = 0
self.spawn_tick += 1
def manage(self):
for pipe in self.pipes:
# move the pipe
pipe[1][0] -= self.pipe_speed
# check if it's off screen
if pipe[1][0] + self.pipe_width < 0:
self.pipes.remove(pipe)
def display(self):
for pipe in self.pipes:
screen.blit(pipe[0], (pipe[1][0], pipe[1][1]))
################################################################################
pg.event.set_blocked(all_events)
pg.event.set_allowed([pg.QUIT, pg.KEYDOWN])
pipe_manager = Pipe_Manager()
loop = True
while loop:
screen.fill(white)
pipe_manager.manage_pipes()
pg.display.update()
clock.tick(FPS)
The pipes seem to shake as they move horizontally and sometimes the top pipe becomes misaligned from the bottom one.
I hope this isn't a problem specific to my computer because I have abstracted away a significant amount of my flappy-bird-clone code and the source of this pipe lag problem must lie somewhere in here.
The problem is this piece of code:
for pipe in self.pipes:
# move the pipe
pipe[1][0] -= self.pipe_speed
# check if it's off screen
if pipe[1][0] + self.pipe_width < 0:
self.pipes.remove(pipe)
Here you change the list you're currently iterating over. Once a pipe gets removed from the list, the next one misses one movement step.
Take a look at the following example in which you can spot the problem yourself (see how 5 is missing in the output because we removed 4):
>>> l = [1,2,3,4,5,6,7,8,9,10]
>>> for x in l:
... if x == 4:
... l.remove(x)
... print(x)
...
1
2
3
4
6
7
8
9
10
>>>
(There's a reason other languages forbid changing the sequence you're currently iterating).
A simple fix is to make a copy of the list first:
for pipe in self.pipes[:]:
For smoother movement, try increasing your framerate and use timestepping.
Here's a possible way:
import pygame as pg
import sys,os,math,time,random
# colours
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
# general stuff
WIDTH = 1024
HEIGHT = 576
FPS = 120
# other
all_events = [pg.QUIT, pg.ACTIVEEVENT, pg.KEYDOWN, pg.KEYUP, pg.MOUSEMOTION,
pg.MOUSEBUTTONUP, pg.MOUSEBUTTONDOWN, pg.VIDEORESIZE,
pg.VIDEOEXPOSE, pg.USEREVENT]
pg.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
clock = pg.time.Clock()
class Pipe:
def __init__(self, img, pos):
self.img = img
self.pos = pos
# Class to manage Pipes
class Pipe_Manager:
def __init__(self):
self.pipe_width = 50
self.pipes = []
self.pipe_speed = 0.3
self.max_tick = 1500
self.spawn_tick = self.max_tick
def manage_pipes(self, dt):
self.spawner(dt)
self.manage(dt)
self.display()
def make_pipe(self):
height = random.randint(100,326)
gap = random.randint(100,250)
surf1 = pg.Surface((self.pipe_width, height))
surf1.fill(green)
surf2 = pg.Surface((self.pipe_width, HEIGHT - (height + gap)))
surf2.fill(green)
pipe = Pipe(surf1, pg.Vector2(WIDTH, 0))
pipe2 = Pipe(surf2, pg.Vector2(WIDTH, height + gap))
self.pipes.append(pipe)
self.pipes.append(pipe2)
def spawner(self, dt):
if self.spawn_tick >= self.max_tick:
self.make_pipe()
self.spawn_tick = 0
self.spawn_tick += dt
def manage(self, dt):
for pipe in self.pipes[:]:
# move the pipe
pipe.pos.x -= self.pipe_speed * dt
# check if it's off screen
if pipe.pos.x + self.pipe_width < 0:
self.pipes.remove(pipe)
def display(self):
for pipe in self.pipes:
screen.blit(pipe.img, pipe.pos)
################################################################################
pg.event.set_blocked(all_events)
pg.event.set_allowed([pg.QUIT, pg.KEYDOWN])
pipe_manager = Pipe_Manager()
loop = True
dt=0
while loop:
for e in pg.event.get():
if e.type == pg.QUIT:
loop = False
screen.fill(white)
pipe_manager.manage_pipes(dt)
pg.display.update()
dt=clock.tick(FPS)
This question is really difficult to ask, but I know you guys here at Stack Overflow are the brightest minds.
I'm totally blinded by why this issue happens (I'm fairly at Python and Pygame, so any suggestions on how to improve the code will be received with the love of improving my skills).
What I'm creating:
It's really a gimmick project, I have a little 2.5" screen (PiTFT) attached to a Raspberry Pi and the code is creating a typewriter effect with a moving cursor in front of the text as it's being written.
Challenge 1 was that every time you move a sprite in pygame, you must redraw everything, otherwise you will see a trail, and since the cursor is moving in front of the text, the result would look like this:
I managed to solve this issue by blackening / clearing the screen. But then I lost all the previously written letters.
So I created a list (entireword), which I'm populing with all the previously written characters. I use this list every time I cycle through the loop to redraw all the previous written text.
So now:
As you can see, the text looks funny.
It's supposed to read:
[i] Initializing ...
[i] Entering ghost mode ... []
I've been spending hours and hours getting to this point - and the code ALMOST works perfectly! The magic happens in the function print_screen(), but WHAT in my code is causing the text to include a letter from the other line in the end? :>
Help is GREATLY appreciated <3
Here's the entire code:
import pygame
import time
import os
import sys
from time import sleep
from pygame.locals import *
positionx = 10
positiony = 10
entireword = []
entireword_pos = 10
counter = 0
entire_newline = False
#Sets the width and height of the screen
WIDTH = 320
HEIGHT = 240
speed = 0.05
#Importing the external screen
os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')
#Initializes the screen - Careful: all pygame commands must come after the init
pygame.init()
#Sets mouse cursor visibility
pygame.mouse.set_visible(False)
#Sets the screen note: must be after pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# initialize font; must be called after 'pygame.init()' to avoid 'Font not Initialized' error
myfont = pygame.font.SysFont("monospace", 18)
#Class
class cursors(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 20))
self.image.fill((0,255,0))
self.rect = self.image.get_rect()
self.rect.center = (positionx + 10, positiony + 10)
def update(self):
self.rect.x = positionx + 10
self.rect.y = positiony
#Functions
#Prints to the screen
def print_screen(words, speed):
rel_speed = speed
for char in words:
#speed of writing
if char == ".":
sleep(0.3)
else:
sleep(rel_speed)
#re-renders previous written letters
global entireword
# Old Typewriter functionality - Changes position of cursor and text a newline
#Makes sure the previous letters are rendered and not lost
#xx is a delimter so the program can see when to make a newline and ofcourse ignore writing the delimiter
entireword.append(char)
if counter > 0:
loopcount = 1
linecount = 0 # This is to which line we are on
for prev in entireword:
if prev == 'xx':
global linecount
global positiony
global loopcount
linecount = linecount + 1
positiony = 17 * linecount
loopcount = 1
if prev != 'xx': #ignore writing the delimiter
pchar = myfont.render(prev, 1, (255,255,0))
screen.blit(pchar, (loopcount * 10, positiony))
loopcount = loopcount + 1
if char != 'xx':
# render text
letter = myfont.render(char, 1, (255,255,0))
#blits the latest letter to the screen
screen.blit(letter, (positionx, positiony))
# Appends xx as a delimiter to indicate a new line
if entire_newline == True:
entireword.append('xx')
global entire_newline
entire_newline = False
global positionx
positionx = positionx + 10
all_sprites.update()
all_sprites.draw(screen)
pygame.display.flip()
screen.fill((0,0,0)) # blackens / clears the screen
global counter
counter = counter + 1
#Positions cursor at new line
def newline():
global positionx
global positiony
positionx = 10
positiony = positiony + 17
all_sprites = pygame.sprite.Group()
cursor = cursors()
all_sprites.add(cursor)
#Main loop
running = True
while running:
global speed
global entire_newline
words = "[i] Initializing ..."
entire_newline = True
newline()
print_screen(words,speed)
words = "[i] Entering ghost mode ..."
entire_newline = True
newline()
print_screen(words,speed)
#Stops the endless loop if False
running = False
sleep(10)
Sorry if I don't answer your question directly, because your code is too confusing for me now, so I took the liberty to rewrite your code to get done what you want.
The idea is to have two sprites:
the cursor, which is a) displayed on the screen and b) keeps track of what text to write and where
the board, which is basically just a surface that the text is rendered on
Note how all the writing logic is on the Cursor class, and we have a nice, simple and dumb main loop.
import pygame
import os
#Sets the width and height of the screen
WIDTH = 320
HEIGHT = 240
#Importing the external screen
os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')
#Initializes the screen - Careful: all pygame commands must come after the init
pygame.init()
clock = pygame.time.Clock()
#Sets mouse cursor visibility
pygame.mouse.set_visible(False)
#Sets the screen note: must be after pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
class Board(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((WIDTH, HEIGHT))
self.image.fill((13,13,13))
self.image.set_colorkey((13,13,13))
self.rect = self.image.get_rect()
self.font = pygame.font.SysFont("monospace", 18)
def add(self, letter, pos):
s = self.font.render(letter, 1, (255, 255, 0))
self.image.blit(s, pos)
class Cursor(pygame.sprite.Sprite):
def __init__(self, board):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((10, 20))
self.image.fill((0,255,0))
self.text_height = 17
self.text_width = 10
self.rect = self.image.get_rect(topleft=(self.text_width, self.text_height))
self.board = board
self.text = ''
self.cooldown = 0
self.cooldowns = {'.': 12,
'[': 18,
']': 18,
' ': 5,
'\n': 30}
def write(self, text):
self.text = list(text)
def update(self):
if not self.cooldown and self.text:
letter = self.text.pop(0)
if letter == '\n':
self.rect.move_ip((0, self.text_height))
self.rect.x = self.text_width
else:
self.board.add(letter, self.rect.topleft)
self.rect.move_ip((self.text_width, 0))
self.cooldown = self.cooldowns.get(letter, 8)
if self.cooldown:
self.cooldown -= 1
all_sprites = pygame.sprite.Group()
board = Board()
cursor = Cursor(board)
all_sprites.add(cursor, board)
text = """[i] Initializing ...
[i] Entering ghost mode ...
done ...
"""
cursor.write(text)
#Main loop
running = True
while running:
for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
all_sprites.update()
screen.fill((0, 0, 0))
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(60)
Use the pygame.event module. Use pygame.time.set_timer() to repeatedly create a USEREVENT in the event queue. The time has to be set in milliseconds. e.g.:
typewriter_event = pygame.USEREVENT+1
pygame.time.set_timer(typewriter_event, 100)
Add a new letter to the text, when the timer event occurs:
while run:
for event in pygame.event.get():
# [...]
if event.type == typewriter_event:
text_len += 1
See also Typewriter
Minimal example:
repl.it/#Rabbid76/PyGame-Typewriter
import pygame
pygame.init()
window = pygame.display.set_mode((500, 150))
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 100)
background = pygame.Surface(window.get_size())
ts, w, h, c1, c2 = 50, *window.get_size(), (32, 32, 32), (64, 64, 64)
tiles = [((x*ts, y*ts, ts, ts), c1 if (x+y) % 2 == 0 else c2) for x in range((w+ts-1)//ts) for y in range((h+ts-1)//ts)]
for rect, color in tiles:
pygame.draw.rect(background, color, rect)
text = 'Hello World'
text_len = 0
typewriter_event = pygame.USEREVENT+1
pygame.time.set_timer(typewriter_event, 100)
text_surf = None
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == typewriter_event:
text_len += 1
if text_len > len(text):
text_len = 0
text_surf = None if text_len == 0 else font.render(text[:text_len], True, (255, 255, 128))
window.blit(background, (0, 0))
if text_surf:
window.blit(text_surf, text_surf.get_rect(midleft = window.get_rect().midleft).move(40, 0))
pygame.display.flip()
pygame.quit()
exit()
This question already has answers here:
How do I create animated sprites using Sprite Sheets in Pygame?
(1 answer)
How do I use Sprite Sheets in Pygame?
(1 answer)
Closed 1 year ago.
I am making a simple program that displays a background image and animates a simple sprite with only 3 frames. Every time I run it, it gives me the error
screen.blit(firstexp[currentimage], (xpos,ypos)) IndexError: list
index out of range
I've been working with this for a while now and I'm not sure what to do anymore. Any help would be greatly appreciated, here's my code:
import pygame, sys, random
from pygame.locals import *
pygame.init()
class Cat(pygame.sprite.Sprite):
animationArray = []
numframes = 0
currentframe = 0
anispeed = 0
lastdraw = 0
xspeed = 5
xpos = 10
ypos = 10
def __init__(self, imgname, startx, starty, w, h, numframes, anispeed):
pygame.sprite.Sprite.__init__(self)
spsheet = pygame.image.load(imgname)
f1 = spsheet.subsurface(0,0,120,60)
self.animationArray.append(f1)
f2 = spsheet.subsurface(0,60,120,60)
self.animationArray.append(f2)
f3 = spsheet.subsurface(0,120,120,60)
self.animationArray.append(f3)
self.numframes = 3
self.currentframe = 0
self.anispeed = anispeed
def update(self, secs):
self.xpos = self.xpos + self.xspeed
self.lastdraw = self.lastdraw+secs
if self.lastdraw self.anispeed:
self.currentframe = self.currentframe+1
if self.currentframe==2:
self.currentframe=0
self.lastdraw = 0
self.image = self.animationArray[self.currentframe]
self.rect = (self.xpos,0,0,120,180)
def setEnv(background):
clock = pygame.time.Clock()
backimage = pygame.image.load(background)
w = backimage.get_rect().w
h = backimage.get_rect().h
screen = pygame.display.set_mode((w,h))
return clock, backimage, screen
###main game code
clock, backimage, screen = setEnv("landscape-illustration.jpg")
allSprites = pygame.sprite.Group() collSprites = pygame.sprite.Group()
catx = Cat("runningcat.png", 0,0,120,180,3,1)
allSprites.add(catx)
collSprites.add(catx)
screen.blit(backimage,(0,0))
while True:
secs = clock.tick(30)/1000
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
allSprites.clear(screen, backimage)
allSprites.update(secs)
allSprites.draw(screen)
collSprites.draw(screen)
pygame.display.flip()
You need to change the update() method of Cat class. Try something like this :
def update(self, secs):
self.xpos += self.xspeed
self.lastdraw += secs
if self.lastdraw > self.anispeed:
self.currentframe = (self.currentframe+1) % 3
self.lastdraw = 0
self.image = self.animationArray[self.currentframe]
self.rect = (self.xpos,0,0,120,180)
if you want to know what is problem with your code, you can print value of cat.currentframe.
When I update my array of which image the program should use for each location, I can place alive cells over dead, but the original doesn't go away and I can't add dead cells over live ones. Does anyone have a fix?
Original File
import pygame, pygamehandle, standard, sys
from pygame.locals import *
loader = pygamehandle.load()
pygame.mixer.music.load('music1.ogg')
pygame.mixer.music.play(-1, 0.0)
SCREEN_SIZE = (600, 400)
fps = 24
fpsClock = pygame.time.Clock()
imgs = ["live.png", "dead.png", "background.png"]
icon = "icon.png"
screen = loader.loadScreen(SCREEN_SIZE, "Game of Life", icon)
lImgs = loader.listImgLoad(imgs)
objects, grid = loader.grid(SCREEN_SIZE, lImgs[1])
loader.blit(objects, grid)
pygame.display.update()
while True:
mouseClicked = False
fpsClock.tick(fps)
for i in pygame.event.get():
if i.type == MOUSEBUTTONDOWN:
if i.button == 1:
mouseposx, mouseposy = pygame.mouse.get_pos()
mouseposx = (mouseposx // 20) * 20
mouseposy = (mouseposy // 20) * 20
mousepos = (mouseposx, mouseposy)
index = grid.index(mousepos)
objects[index] = lImgs[0]
if i.button == 2:
mouseposx, mouseposy = pygame.mouse.get_pos()
mouseposx = (mouseposx // 20) * 20
mouseposy = (mouseposy // 20) * 20
mousepos = (mouseposx, mouseposy)
index = grid.index(mousepos)
objects[index] = lImgs[1]
if i.type == QUIT:
pygame.quit()
sys.exit()
pygame.Surface.fill(screen, [0, 0, 0])
loader.blit(objects, grid)
pygame.display.flip()
I also used these functions from the pygamehandle file.
import pygame, standard
class load(object):
pygame.init()
def loadScreen(self, size, text, icon):
pygame.display.set_caption(text, icon)
pygame.display.set_icon(pygame.image.load(icon))
screen = pygame.display.set_mode(size)
self.screen = screen
return screen
def listImgLoad(self, list):
img = []
for i in range (0, len(list)):
img.append(pygame.image.load(list[i]).convert())
return img
def blit(self, items, locations):
for i in range (0, len(items)):
self.screen.blit(items[i], locations[i])
def grid(self, size, object):
objects =[]
locations = []
x, y = size
for xT in range (0, int(x / 20)):
for yT in range(0, int(y / 20)):
objects.append(object)
locations.append((xT * 20, yT * 20))
return objects, locations
A better way to do this is make a Sprite class for each cell, add a bool to deteermine if the cell is dead or alive and blit accordingly.
If you are familiar with Sprites here is the docs, It may be confusing at first but they will help in making more complex games, also here is a link to my version of The Game of Life
Goodluck