I am trying to generate terrain randomly, and I have made it scroll it just as a test, but when the pixels hit the end of the screen, they play back on the other side as such:brokenscroll before hitting the other side it looks like:scrolling Here's my code:
import pygame as py
import random as r
import time as t
import math as m
py.init()
left = 0
Ylev = 8
display = py.display.set_mode((512, 512))
display.fill((175, 215, 225))
def scrollX(screenSurf, offsetX):
width, height = screenSurf.get_size()
copySurf = screenSurf.copy()
screenSurf.blit(copySurf, (offsetX, 0))
if offsetX < 0:
screenSurf.blit(copySurf, (width + offsetX, 0), (0, 0, -offsetX, height))
else:
screenSurf.blit(copySurf, (0, 0), (width - offsetX, 0, offsetX, height))
while True:
num = round(r.randint(0, 5) ** 1/4)
neg = r.randint(0, 1)
if neg == 0:
num = -num
Ylev += num
if Ylev < 0:
Ylev = 0
if Ylev > 16:
Ylev = 15
print(Ylev)
scrollX(display, -16)
py.draw.rect(display, (0, 100, 20), py.Rect(31 * 16, Ylev * 16, 16, 16))
py.display.flip()
t.sleep(0.25)````
I am trying to achieve image 2, but with the ability to hit the other side of the screen and still generate new terrain. How can this be achieved, and if possible, how can I allow a player (represented by 2 pixels) to interact with this terrain, in the form of movement, but disallowing movement through the generated terrain. Can this be achieved by detecting the colour of a pixel (e.g. the terrain) and letting the player interact with it that way, or do I need a list of all the terrain pixels?
I tried to update everything but the first row, but
1. This leads to the first pixel never being displayed, and
2. This doesn't fix the problem.
Aucun commentaire:
Enregistrer un commentaire