I am practicing my pygame knowledge but I have got a problem here. I have two rectangles but when I run the program one of them is blinking. I tried to swap zombi.appear()
and player.appear()
and noticed that the blinking rect is always the second one (therefore in the case below it's player.appear()
). So I assume they somehow interrupt each other. Please help me solve this.
Thanks in advance!
import pygame
import random
# Variables
inProcess = True
width, height = 500, 500
red = (255, 0, 0)
blue = (0, 0, 255)
# Pygame Settings
pygame.init()
screen = pygame.display.set_mode([width, height])
clock = pygame.time.Clock()
class Mob:
def __init__(self, x, y, speed, type, color):
self.x = x
self.y = y
self.speed = speed
self.type = type
self.color = color
def render(self):
pygame.display.flip()
def appear(self):
pygame.draw.rect(screen, self.color, (self.x, self.y, 64, 64))
if self.type == 'player':
self.move_player()
if self.type == 'zombi':
self.move_zombi()
self.render()
def move_player(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_w] and self.y > 0:
self.y -= self.speed
elif keys[pygame.K_s] and self.y < height - 64:
self.y += self.speed
elif keys[pygame.K_a] and self.x > 0:
self.x -= self.speed
elif keys[pygame.K_d] and self.x < width - 64:
self.x += self.speed
def move_zombi(self):
if self.y < 0 or self.y + 64 > height:
self.speed *= -1
self.y += self.speed
def main():
player = Mob(64, 64, 1, 'player', red)
zombi = Mob(256, 256, 2, 'zombi', blue)
while inProcess:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
screen.fill((255, 255, 255))
zombi.appear()
player.appear()
clock.tick(60)
main()