0
votes

i have been tinkering in pygame quite a bit, but whenever i run the program, all i get is a blank screen(no errors). I know it's not a problem with my code because i copied another persons program off the internet and that didn't even work. I am on Mac os. About 1 month back i wrote this code and it was working then, but now all i get is a blank screen. How can i fix this problem?(I have also looked at other similar questions and none of those solutions worked) Here is my code anyway (used a tech with tim tutorial):

import pygame
import neat
import time
import os
import random

WINDOW_WIDTH = 500
WINDOW_HEIGHT = 800

BIRD_IMGS = [pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','bird1.png'))),pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','bird2.png'))),pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','bird3.png')))]
PIPE_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','pipe.png')))
BASE_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','base.png')))
BG_IMG = pygame.transform.scale2x(pygame.image.load(os.path.join('imgs','bg.png')))

class Bird:
    IMGS = BIRD_IMGS
    MAX_ROTATION = 25
    ROT_VEL = 20
    ANIMATION_TIME = 5

    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.tilt = 0
        self.tick_count = 0
        self.vel = 0
        self.height = self.y
        self.img_count = 0
        self.img = self.IMGS[0]

    def jump(self):
        self.vel = -10.5
        self.tick_count = 0
        self.height = self.y
    def move(self):
        self.tick_count += 1

        d = self.vel * self.tick_count + 1.5 * self.tick_count**2
        if d >= 16:
            d = 16
        if d < 0:
            d -= 2

        self.y += d

        if d < 0 or self.y < self.y + 50:
            if self.tilt < self.MAX_ROTATION:
                self.tilt = self.MAX_ROTATION
        else:
            if self.tilt > -90:
                self.tilt -= self.ROT_VEL
    def draw(self,win):
        self.img_count += 1

        if self.img_count < self.ANIMATION_TIME:
            self.img = self.IMGS[0]

        elif self.img_count < self.ANIMATION_TIME*2:
            self.img = self.IMGS[1]
        elif self.img_count < self.ANIMATION_TIME*3:
            self.img = self.IMGS[2]
        elif self.img_count < self.ANIMATION_TIME*4:
            self.img = self.IMGS[1]
        elif self.img_count == self.ANIMATION_TIME*4+1:
            self.img = self.IMGS[0]
            self.img_count = 0
        if self.tilt <= 80:
            self.img = self.IMGS[1]
            self.img_count = self.ANIMATION_TIME*2

        rotated_img = pygame.transform.rotate(self.img, self.tilt)
        new_rect = rotated_img.get_rect(center=self.img.get_rect(topleft = (self.x, self.y)).center)
        win.blit(rotated_img, new_rect.topleft)

    def get_mask(self):
        return pygame.mask.from_surface(self.img)

def draw_window(win,bird):
    win.blit(BG_IMG, (0,0))
    bird.draw(win)
    pygame.display.update()

def main():
    bird = Bird(200,200)
    win = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
    clock = pygame.time.Clock()

    running = True
    while running:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
        bird.move()
        draw_window(win, bird)
    pygame.quit()
    quit()


if __name__ == '__main__':
    main()
2
I cannot reproduce the issue, at least the background is shown. The issue seems to be related to your system or IDE. - Rabbid76
@Rabbid76 yup that's what i thought. Any idea on how to fix it on mac - krmogi
Sorry, but no. In my experience, anything not made by Apple itself has ongoing and annoying version issues. You're not the only, who has issues with PyGame on Mac. - Rabbid76
Thats okay, thanks for your help. - krmogi

2 Answers

0
votes

Your code has the win = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT)) in the main function. I usually take the pygame.display.set_mode outside the main function. You also didn't add pygame.init.This is what I think is the problem.

0
votes

I took a break for a couple of months and figured I should re-install the latest version of pygame once again. Once I did that, I tried both on atom and Jupyter notebook, but none of them worked. I tried to use the regular python IDE, and that surprisingly worked.