Skip to content

Instantly share code, notes, and snippets.

@greut
Last active March 5, 2017 08:53
Show Gist options
  • Save greut/1b1c2ab3159d299edb9f69647aa83c30 to your computer and use it in GitHub Desktop.
Save greut/1b1c2ab3159d299edb9f69647aa83c30 to your computer and use it in GitHub Desktop.
Does pygame have a bug or am I dummy?
import sys
import pygame
FPS = 30
WIDTH, HEIGHT = GEOMETRY = 420, 360
screen = pygame.display.set_mode(GEOMETRY)
pygame.display.set_caption('Bug')
blocks = (
pygame.Rect(WIDTH // 2, 0, 60, HEIGHT),
pygame.Rect(WIDTH, 0, 60, HEIGHT),
pygame.Rect(0, 0, 60, HEIGHT), )
speed = pygame.math.Vector2(-10, 0)
time = 0
clock = pygame.time.Clock()
while True:
clock.tick(FPS)
dt = clock.get_time()
time += dt
for block in blocks:
block.x += speed.x * (dt / 100)
if block.topright[0] < 0:
block.x = WIDTH
background = pygame.Surface(GEOMETRY, pygame.SRCALPHA)
background.fill((0xff, 0xff, 0xff, 0x99))
screen.blit(background, (0, 0))
for block in blocks:
pygame.draw.rect(screen, (0x99, 0x00, 0x00), block)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.flip()
import sys
import math
import pygame
FPS = 30
WIDTH, HEIGHT = GEOMETRY = 420, 360
screen = pygame.display.set_mode(GEOMETRY)
pygame.display.set_caption('No bug')
size = 4
w = 60
blocks = [
pygame.Rect((WIDTH + w) / size * i, 0, w, HEIGHT) for i in range(size)
]
speed = pygame.math.Vector2(-10, 0)
time = 0
clock = pygame.time.Clock()
while True:
clock.tick(FPS)
dt = clock.get_time()
time += dt
for block in blocks:
block.x = math.ceil(block.x + speed.x * (dt / 100))
if block.topright[0] < 0:
block.x = WIDTH
background = pygame.Surface(GEOMETRY, pygame.SRCALPHA)
background.fill((0xff, 0xff, 0xff, 0x99))
screen.blit(background, (0, 0))
for block in blocks:
pygame.draw.rect(screen, (0x99, 0x00, 0x00), block)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.flip()
import sys
import pygame
import math
from collections import namedtuple
FPS = 30
WIDTH, HEIGHT = GEOMETRY = 420, 360
screen = pygame.display.set_mode(GEOMETRY)
pygame.display.set_caption('To ceil or not to ceil?')
Block = namedtuple("Block", "position rect")
size = 4
w = 60
blocks = [
Block(
pygame.math.Vector2((WIDTH + w) / size * i, 0),
pygame.Rect(0, 0, w, HEIGHT)) for i in range(size)
]
speed = pygame.math.Vector2(-10, 0)
time = 0
clock = pygame.time.Clock()
while True:
clock.tick(FPS)
dt = clock.get_time()
time += dt
for i, block in enumerate(blocks):
if block.rect.topright[0] < 0:
block.position.x = WIDTH
block.position.x = block.position.x + speed.x * (dt / 100)
if i % 2:
block.rect.x = math.ceil(block.position.x)
else:
block.rect.x = block.position.x
background = pygame.Surface(GEOMETRY, pygame.SRCALPHA)
background.fill((0xff, 0xff, 0xff, 0x99))
screen.blit(background, (0, 0))
for i, block in enumerate(blocks):
color = (0, 0x99, 0) if i % 2 else (0x99, 0, 0)
pygame.draw.rect(screen, color, block.rect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment