Recently, I started to take a look at pygame, but now I´m facing a serious problem. I followed the tutorial by "KidsCanCode" on Youtube and now have this code:
# Pygame template - skeleton for a new pygame project
import pygame
import random
WIDTH = 600
HEIGHT = 480
FPS = 30
# define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
class Player(pygame.sprite.Sprite):
# sprite for the Player
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 10
self.speedx = 0
def update(self):
self.speedx = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -5
if keystate[pygame.K_RIGHT]:
self.speedx = 5
self.rect.x += self.speedx
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
# initialize pygame and create window
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
all_sprites = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
# Game loop
running = True
while running:
# keep loop runnig at the right speed
clock.tick(FPS)
# Process input (events)
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
running = False
# Update
all_sprites.update()
# Draw / render
screen.fill(BLACK)
all_sprites.draw(screen)
# *after* drawing everything, flip the display
pygame.display.flip()
pygame.quit()
The code itself is working fine, the pygame window appears and I can see the green sprite. But when I try to control the sprite via arrow keys, I only control the cursor in the text editor, I wrote the script in.
If I didn't open the text editor in full screen mode, the pygame window even disappears to behind the editor´s window. When I move the editor aside / minimize it, and click on the pygame window it still doesn't´t get focused.
I´m using a Mac, macOS 10.12.4, Python 3.6.0, Anaconda 4.3.1.
When I try to go to the pygame window via "cmd + tab", it doesn't´t even appear in the list.
I installed pygame via pip.
I hope someone has either seen this problem before / has an idea to fix this.