# Pygame scrolling

1 Tallied Votes 1K Views

This code should be able to get you started to design 2-D scrolling games, like Mario. I spent a few hours trying to figure this out, and firgured I'd put up my efforts. I think there might be a better way to do it, but this should help you get started.

``````# Attempt at scrolling
import pygame
from pygame.locals import *

pygame.init()

# All colors used
red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
orange = (255, 100, 0)
purple = (255, 0, 255)
black = (0, 0, 0)
white = (255, 255, 255)
colors = [green, blue, orange, purple, white]

# Initialize the screen you see
window_width = 600
window_height = 480
screen = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption("Scrolling Attempt!")

# Draw the main map
# (Draw what you want here)
map_width = 2000
map_height = 700
main_map = pygame.Surface((map_width, map_height))
main_map = main_map.convert()
def draw():
main_map.fill(black)
rect1 = pygame.draw.rect(main_map, red, (250, 40, 1700, 400))
circle1 = pygame.draw.circle(main_map, white, (1000, 300), 20)
circle1 = pygame.draw.circle(main_map, green, (1000, 200), 20)
circle1 = pygame.draw.circle(main_map, blue, (1000, 100), 20)
draw()
# The coords you see
map_x = 0 # Only this should change
map_y = 0
# The change for x
map_x_c = -3

screen.blit(main_map, (map_x, map_y, window_width, window_height))
pygame.display.flip()

print map_x, map_y, map_x+window_width, map_y+window_height

ucircle = pygame.draw.circle(screen, orange, (((-map_x+window_width)/2)+-map_x, ((map_y+window_height)/2)), 20)
# The program
while True:
for event in pygame.event.get():
pass

key_pressed = pygame.key.get_pressed()

if key_pressed[K_LEFT]: #and map_x != 0:
map_x -= map_x_c
print map_x, map_y, map_x+window_width, map_y+window_height
#draw()
#ucircle = pygame.draw.circle(main_map, orange, ((((-map_x+window_width)/2)+(-(map_x)/2)), (((-map_y+window_width)/2)+(-(map_y)/2))), 20)

if key_pressed[K_RIGHT]: #and map_x != 2000:
map_x += map_x_c
print map_x, map_y, map_x+window_width, map_y+window_height
#draw()
#ucircle = pygame.draw.circle(main_map, orange, ((((-map_x+window_width)/2)+(-(map_x)/2)), (((-map_y+window_width)/2)+(-(map_y)/2))), 20)

if key_pressed[K_UP]: #and map_x != 0:
map_y -= map_x_c
print map_x, map_y, map_x+window_width, map_y+window_height
#draw()
#ucircle = pygame.draw.circle(main_map, orange, ((((-map_x+window_width)/2)+(-(map_x)/2)), (((-map_y+window_width)/2)+(-(map_y)/2))), 20)

if key_pressed[K_DOWN]: #and map_x != 2000:
map_y += map_x_c
print map_x, map_y, map_x+window_width, map_y+window_height
#draw()
#ucircle = pygame.draw.circle(main_map, orange, ((((-map_x+window_width)/2)+(-(map_x)/2)), (((-map_y+window_width)/2)+(-(map_y)/2))), 20)

if key_pressed[K_ESCAPE]:
quit()

if map_x > 0:
map_x = 0

if map_x < -(map_width-window_width):
map_x = -(map_width-window_width)

if map_y > 0:
map_y = 0

if map_y < -(map_height-window_height):
map_y = -(map_height-window_height)

screen.blit(main_map, (map_x, map_y, window_width, window_height))
ucircle = pygame.draw.circle(screen, orange, (window_width/2, window_height/2), 20)
pygame.display.flip()``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.580459,"math_prob":0.9908639,"size":3313,"snap":"2022-40-2023-06","text_gpt3_token_len":1016,"char_repetition_ratio":0.21909943,"word_repetition_ratio":0.12944162,"special_character_ratio":0.3637187,"punctuation_ratio":0.25528455,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9973854,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-01T14:00:12Z\",\"WARC-Record-ID\":\"<urn:uuid:ec7057b6-45fc-40e4-83de-3c25db3a8033>\",\"Content-Length\":\"63791\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:b221ec70-3826-4853-a870-aaef6046d6c5>\",\"WARC-Concurrent-To\":\"<urn:uuid:98eeffeb-9071-4a96-8fa2-dd9c09371538>\",\"WARC-IP-Address\":\"172.66.42.251\",\"WARC-Target-URI\":\"https://www.daniweb.com/programming/software-development/code/254039/pygame-scrolling\",\"WARC-Payload-Digest\":\"sha1:MBUXNFPYQMZCZFXP5VQBAD6FQ65D7CCZ\",\"WARC-Block-Digest\":\"sha1:V6AXSMUA65XIPBT5C767SBUX2FFNCXUG\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030336674.94_warc_CC-MAIN-20221001132802-20221001162802-00712.warc.gz\"}"}