Pygame returning black screen even though other parts of the same script are working
Im following a tutorial on Youtube which results in displaying a red blob/dot on a white background. I am only getting a black screen instead of the expected result.
I've tried to find any errors in the code but cant find any. When i run the code i am presented with a window with a black background and nothing on it. The only part of the code that is working is the exit button. I would think that if that part of the script is working, all of it would be working but i guess i still messed up. Help would be much appreciated.
This is my code:
import pygame
import random
width = 800
height = 600
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
game_display = pygame.display.set_mode((width, height))
pygame.display.set_caption("2D Blob world")
clock = pygame.time.Clock()
class blob:
def __init__(self, color):
self.x = random.randrange(0,width)
self.y = random.randrange(0,height)
self.size = random.randrange(5,10)
self.color = color
def move(self):
self.x += random.randrange(-5,5)
self.y += random.randrange(-5,5)
if self.x >width:
self.x = width
elif self.x <0:
self.x = 0
if self.y >height:
self.y = height
elif self.y <0:
self.y = 0
def draw_environment(blob):
game_display.fill((white))
pygame.draw.circle(game_display, blob.color,(blob.x, blob.y), blob.size)
pygame.display.update()
def main():
red_blob = blob(red)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw_environment(red_blob)
clock.tick(60)
if __name__ == '__main__':
main()
draw_environment(red_blob)
What it should get as a result is a red dot on a white background.
python
add a comment |
Im following a tutorial on Youtube which results in displaying a red blob/dot on a white background. I am only getting a black screen instead of the expected result.
I've tried to find any errors in the code but cant find any. When i run the code i am presented with a window with a black background and nothing on it. The only part of the code that is working is the exit button. I would think that if that part of the script is working, all of it would be working but i guess i still messed up. Help would be much appreciated.
This is my code:
import pygame
import random
width = 800
height = 600
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
game_display = pygame.display.set_mode((width, height))
pygame.display.set_caption("2D Blob world")
clock = pygame.time.Clock()
class blob:
def __init__(self, color):
self.x = random.randrange(0,width)
self.y = random.randrange(0,height)
self.size = random.randrange(5,10)
self.color = color
def move(self):
self.x += random.randrange(-5,5)
self.y += random.randrange(-5,5)
if self.x >width:
self.x = width
elif self.x <0:
self.x = 0
if self.y >height:
self.y = height
elif self.y <0:
self.y = 0
def draw_environment(blob):
game_display.fill((white))
pygame.draw.circle(game_display, blob.color,(blob.x, blob.y), blob.size)
pygame.display.update()
def main():
red_blob = blob(red)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw_environment(red_blob)
clock.tick(60)
if __name__ == '__main__':
main()
draw_environment(red_blob)
What it should get as a result is a red dot on a white background.
python
add a comment |
Im following a tutorial on Youtube which results in displaying a red blob/dot on a white background. I am only getting a black screen instead of the expected result.
I've tried to find any errors in the code but cant find any. When i run the code i am presented with a window with a black background and nothing on it. The only part of the code that is working is the exit button. I would think that if that part of the script is working, all of it would be working but i guess i still messed up. Help would be much appreciated.
This is my code:
import pygame
import random
width = 800
height = 600
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
game_display = pygame.display.set_mode((width, height))
pygame.display.set_caption("2D Blob world")
clock = pygame.time.Clock()
class blob:
def __init__(self, color):
self.x = random.randrange(0,width)
self.y = random.randrange(0,height)
self.size = random.randrange(5,10)
self.color = color
def move(self):
self.x += random.randrange(-5,5)
self.y += random.randrange(-5,5)
if self.x >width:
self.x = width
elif self.x <0:
self.x = 0
if self.y >height:
self.y = height
elif self.y <0:
self.y = 0
def draw_environment(blob):
game_display.fill((white))
pygame.draw.circle(game_display, blob.color,(blob.x, blob.y), blob.size)
pygame.display.update()
def main():
red_blob = blob(red)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw_environment(red_blob)
clock.tick(60)
if __name__ == '__main__':
main()
draw_environment(red_blob)
What it should get as a result is a red dot on a white background.
python
Im following a tutorial on Youtube which results in displaying a red blob/dot on a white background. I am only getting a black screen instead of the expected result.
I've tried to find any errors in the code but cant find any. When i run the code i am presented with a window with a black background and nothing on it. The only part of the code that is working is the exit button. I would think that if that part of the script is working, all of it would be working but i guess i still messed up. Help would be much appreciated.
This is my code:
import pygame
import random
width = 800
height = 600
white = (255,255,255)
red = (255,0,0)
blue = (0,0,255)
game_display = pygame.display.set_mode((width, height))
pygame.display.set_caption("2D Blob world")
clock = pygame.time.Clock()
class blob:
def __init__(self, color):
self.x = random.randrange(0,width)
self.y = random.randrange(0,height)
self.size = random.randrange(5,10)
self.color = color
def move(self):
self.x += random.randrange(-5,5)
self.y += random.randrange(-5,5)
if self.x >width:
self.x = width
elif self.x <0:
self.x = 0
if self.y >height:
self.y = height
elif self.y <0:
self.y = 0
def draw_environment(blob):
game_display.fill((white))
pygame.draw.circle(game_display, blob.color,(blob.x, blob.y), blob.size)
pygame.display.update()
def main():
red_blob = blob(red)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
draw_environment(red_blob)
clock.tick(60)
if __name__ == '__main__':
main()
draw_environment(red_blob)
What it should get as a result is a red dot on a white background.
python
python
asked Jan 1 at 5:19


mubashir m. khalidmubashir m. khalid
132
132
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The problem with the above code is inside the main method's definition. You are creating a blob object and then going inside an infinite loop, instead of drawing the environment first.
To fix it you need to move draw_environment(red_blob) from line 48 to after line 41, so that your main method looks like this:
def main():
red_blob = blob(red)
draw_environment(red_blob)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
clock.tick(60)
You may also want to move the clock.tick(60) method to somewhere above also as you need it to be.
1
This solution completely works for me. I never considered it. Thank you very much.
– mubashir m. khalid
Jan 1 at 15:05
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993163%2fpygame-returning-black-screen-even-though-other-parts-of-the-same-script-are-wor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem with the above code is inside the main method's definition. You are creating a blob object and then going inside an infinite loop, instead of drawing the environment first.
To fix it you need to move draw_environment(red_blob) from line 48 to after line 41, so that your main method looks like this:
def main():
red_blob = blob(red)
draw_environment(red_blob)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
clock.tick(60)
You may also want to move the clock.tick(60) method to somewhere above also as you need it to be.
1
This solution completely works for me. I never considered it. Thank you very much.
– mubashir m. khalid
Jan 1 at 15:05
add a comment |
The problem with the above code is inside the main method's definition. You are creating a blob object and then going inside an infinite loop, instead of drawing the environment first.
To fix it you need to move draw_environment(red_blob) from line 48 to after line 41, so that your main method looks like this:
def main():
red_blob = blob(red)
draw_environment(red_blob)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
clock.tick(60)
You may also want to move the clock.tick(60) method to somewhere above also as you need it to be.
1
This solution completely works for me. I never considered it. Thank you very much.
– mubashir m. khalid
Jan 1 at 15:05
add a comment |
The problem with the above code is inside the main method's definition. You are creating a blob object and then going inside an infinite loop, instead of drawing the environment first.
To fix it you need to move draw_environment(red_blob) from line 48 to after line 41, so that your main method looks like this:
def main():
red_blob = blob(red)
draw_environment(red_blob)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
clock.tick(60)
You may also want to move the clock.tick(60) method to somewhere above also as you need it to be.
The problem with the above code is inside the main method's definition. You are creating a blob object and then going inside an infinite loop, instead of drawing the environment first.
To fix it you need to move draw_environment(red_blob) from line 48 to after line 41, so that your main method looks like this:
def main():
red_blob = blob(red)
draw_environment(red_blob)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
clock.tick(60)
You may also want to move the clock.tick(60) method to somewhere above also as you need it to be.
answered Jan 1 at 5:27


Muhammad AsifMuhammad Asif
332210
332210
1
This solution completely works for me. I never considered it. Thank you very much.
– mubashir m. khalid
Jan 1 at 15:05
add a comment |
1
This solution completely works for me. I never considered it. Thank you very much.
– mubashir m. khalid
Jan 1 at 15:05
1
1
This solution completely works for me. I never considered it. Thank you very much.
– mubashir m. khalid
Jan 1 at 15:05
This solution completely works for me. I never considered it. Thank you very much.
– mubashir m. khalid
Jan 1 at 15:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53993163%2fpygame-returning-black-screen-even-though-other-parts-of-the-same-script-are-wor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown