Return to “Scripting & Modding”

Post

Re: How to program in Python + Paste Code for review/help

#62
Cornflakes_91 wrote:
Sun Jan 28, 2018 3:55 pm
I would return the [name, value] tuple from the retrieve_name() function instead of straight up printing it. More reusability.
As you can do anything with the data not just barf it on screen
Once you have a function like that, Formal, you can then keep it aside in a personal library for reuse later.
I don't tend to do this myself, but I do have a few places where I reuse the same older functions.
Particularly in string padding functions, or some basic math/iterator replacements for JS.

Anything you find a use for between more than 3 projects should be stored long term in my own opinion.
°˖◝(ಠ‸ಠ)◜˖°
WebGL Spaceships and Trails
<Cuisinart8> apparently without the demon driving him around Silver has the intelligence of a botched lobotomy patient ~ Mar 04 2020
console.log(`What's all ${this} ${Date.now()}`);
Post

Re: How to program in Python + Paste Code for review/help

#63
So I tried to create my own library, but anything beyond a simple print is beyond me at this time.. it will come.. but first!
My first program in python using pygame.
:excite:

Wow, this is so cool, especially seeing as I only saw how the vid on how to move a sprite left and right.. I figured out how to move a custom-drawn rectangle, not only left and right, but up and down too!! :lol:

This is fun :D

Without further ado;

Code: Select all

#!/usr/bin/python3

import inspect
import pygame
# pygame.locals required to draw rectangles
from pygame.locals import *


def retrieve_name(var):
    global tup1
    callers_local_vars = inspect.currentframe().f_back.f_locals.items()
    tup1 = ([var_name for var_name, var_val in callers_local_vars if var_val is var], var)

# 1) Uncomment this if you have a sprite to import
#def car(start_car_x,start_car_y):
#    gameDisplay.blit(carImg,(start_car_x,start_car_y))


# 2) then uncommment this to load the sprite into pygame
# carImg = pygame.image.load('racecar.png')


display_width  = 1440
display_height = 800

rectangle_width = 200
rectangle_height = 300

# x is along width axis
rectangle_x_pos = 1000
# y is along height axis
rectangle_y_pos = 500

# 3) Uncomment these to set the starting point for the sprite
# start_car_x = (display_width * 0.45)
# start_car_y = (display_height * 0.8)


black = (0,0,0)
white = (255,255,255)
red   = (255,0,0)
red2  = (230,50,50)
green = (0,255,0)
blue  = (0,0,255)
blue2 = (40,50,180)

change_rect_x_pos = 0
change_rect_y_pos = 0

# Main Game Loop
pygame.init()
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('My First Game Window')
clock = pygame.time.Clock()

crashed = False

while not crashed:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            change_rect_x_pos = -5
        elif event.key == pygame.K_RIGHT:
            change_rect_x_pos = 5
        elif event.key == pygame.K_UP:
            change_rect_y_pos = -5
        elif event.key == pygame.K_DOWN:
            change_rect_y_pos = 5

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
            change_rect_x_pos = 0
        elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            change_rect_y_pos = 0

    rectangle_x_pos += change_rect_x_pos
    rectangle_y_pos += change_rect_y_pos


    gameDisplay.fill(blue)
# 4) Uncomment this to draw the car
#    car(start_car_x,start_car_y)
# or
    pygame.draw.rect(gameDisplay, (red2), Rect((rectangle_x_pos,rectangle_y_pos),(rectangle_width,rectangle_height)))

#    retrieve_name(rectangle_x_pos)
#    print (tup1[0], tup1[1])
#    retrieve_name(rectangle_y_pos)
#    print (tup1[0], tup1[1])

#        for debugging only
#        print(event)

    pygame.display.update()
# or
#    pygame.display.flip()
    clock.tick(60)

pygame.quit()
quit()

P.S. The Retrieve_name function was priceless when I did a bad computation on one iteration.. basically the x position of the rectangle added itself up to 600,000 or some crazy number.. :lol:
YAY PYTHON \o/

In Josh We Trust
-=326.3827=-

Online Now

Users browsing this forum: No registered users and 2 guests

cron