I think the bug’s pretty self-explanatory, so I’ll just post the code and a screenie of what happens
'''
Created on Oct 29, 2012
@author: pipsqueaker
'''
import pygame, sys, random
from pygame.locals import *
frapsClock = pygame.time.Clock()
class Alien:
def __init__(self, arg1, arg2):
self.x = arg1
self.y = arg2
def move(self, craftx, crafty):
if self.x < craftx:
self.x += 1
elif self.x > craftx:
self.x -= 1
if self.y < crafty:
self.y += 1
elif self.y > crafty:
self.y -= 1
class PlayShip():
def __init__(self, ag1, ag2):
self.x = ag1
self.y = ag2
class mainLopp():
Board = pygame.display.set_mode((650, 500))
pygame.display.set_caption("This")
pygame.init()
player = PlayShip(random.randrange(1, 650), random.randrange(1, 500))
alienList = [Alien(random.randrange(1, 650), random.randrange(1, 500))]
while True:
pygame.draw.rect(Board, (0, 0, 255), (player.x, player.y, 12, 12))
for currAlien in alienList:
currAlien.move(player.x, player.y)
pygame.draw.rect(Board, (255, 0, 0), (currAlien.x, currAlien.y, 16, 16))
for event in pygame.event.get():
if event.type == QUIT:
print(alienList)
pygame.quit()
sys.exit()
pygame.display.update()
frapsClock.tick(100)
the move() method is supposed to (over time) move the alien (red) to the player (blue). But when that code runs, this happens
It isnt supposed to be continuous like that. Which begs the question, how do I fix it?
A quick fix:
Precreate a color before your main loop:
and in your main loop:
This will paint black over the entire screen, effectively erasing everything.
This is OKAY because you re-draw everything again immediately after erasing it.
In more advanced drawing code, instead of filling the screen (which can be slow), you would: