I'm on my Mac M1 playing around with Pygame. What happening is that Pygame isn't allowing me to use the font module. Looking at some GitHub discussions I've tried installing sdl2 libraries using brew but nothing works. How would I resolve this problem?
P.S Although there is no error with the code I will provide it anyways.
import pygame
from pygame.draw import circle,rect
from pygame.locals import *
from pygame.font import SysFont
import random
pygame.init()
width = 500
height = 900
font = pygame.font.SysFont('arial',50)
screen = pygame.display.set_mode((width,height))
enemies = pygame.sprite.Group()
players = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
plasers = pygame.sprite.Group()
alasers = pygame.sprite.Group()
ENEMYLASER = pygame.USEREVENT+1
pygame.time.set_timer(ENEMYLASER,1000)
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player,self).__init__()
self.surf = pygame.Surface((30,40))
self.surf.fill((255,0,0))
self.rect = self.surf.get_rect()
self.speed = 5
self.rect.center = (width//2,800)
def update(self,pressed_keys):
if pressed_keys[K_a]:
self.rect.move_ip(-self.speed,0)
if pressed_keys[K_d]:
self.rect.move_ip(self.speed,0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(self.speed,0)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-self.speed,0)
if self.rect.right >width:
self.rect.right = width
if self.rect.left<0:
self.rect.left = 0
if pygame.sprite.spritecollideany(self,alasers):
players.remove(self)
all_sprites.remove(self)
self.kill()
class Alien(pygame.sprite.Sprite):
def __init__(self,x,y):
super(Alien,self).__init__()
self.surf=pygame.Surface((50,50))
self.rect = self.surf.get_rect()
self.rect.center=(x,y)
self.surf.fill((0,255,0))
def update(self):
if pygame.sprite.spritecollideany(self,plasers):
enemies.remove(self)
all_sprites.remove(self)
self.kill()
class PLaser(pygame.sprite.Sprite):
def __init__(self,x,y):
super(PLaser,self).__init__()
self.surf = pygame.Surface((10,20))
self.surf.fill((0,0,255))
self.rect = self.surf.get_rect()
self.rect.center=(x,y)
self.speed = -5
self.visibility = True
def update(self):
self.rect.move_ip(0,self.speed)
if not self.visibility:
plasers.remove(self)
all_sprites.remove(self)
self.kill()
if pygame.sprite.spritecollideany(self,enemies):
self.visibility= False
class ALaser(pygame.sprite.Sprite):
def __init__(self,x,y):
super(ALaser,self).__init__()
self.surf = pygame.Surface((10,20))
self.surf.fill((255,255,0))
self.rect = self.surf.get_rect()
self.rect.center=(x,y)
self.speed = 5
self.visibility = True
def update(self):
self.rect.move_ip(0,self.speed)
if not self.visibility:
alasers.remove(self)
all_sprites.remove(self)
self.kill()
if pygame.sprite.spritecollideany(self,players):
self.visibility= False
player = Player()
players.add(player)
all_sprites.add(player)
for x in range(3):
for y in range(3):
alien = Alien(x*100+100,y*100+100)
enemies.add(alien)
all_sprites.add(alien)
def won():
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
text = font.render("You won",True,(255,0,0))
text_rect = text.get_rect()
text_rect.center = (width//2,height//2)
screen.blit(text,text_rect)
pygame.display.flip()
def lost():
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
text = font.render("You lost",True,(255,0,0))
text_rect = text.get_rect()
text_rect.center = (width//2,height//2)
screen.blit(text,text_rect)
pygame.display.flip()
running=True
while running:
for event in pygame.event.get():
if event.type == QUIT:
running=False
if event.type == KEYDOWN:
if event.key == K_SPACE:
laser = PLaser(player.rect.centerx,player.rect.centery)
plasers.add(laser)
all_sprites.add(laser)
if event.type == ENEMYLASER:
enemy = random.choice(enemies.sprites())
laser= ALaser(enemy.rect.centerx,enemy.rect.centery)
alasers.add(laser)
all_sprites.add(laser)
screen.fill((0,0,0))
pressed_keys= pygame.key.get_pressed()
player.update(pressed_keys)
for entity in plasers:
entity.update()
for entity in alasers:
entity.update()
for entity in enemies:
entity.update()
for entity in all_sprites:
screen.blit(entity.surf,entity.rect)
if len(players.sprites()) == 0:
running=False
lost()
if len(enemies.sprites()) == 0:
running=False
won()
pygame.display.flip()
pygame.quit()
See Question&Answers more detail:
os