Skip to content

Instantly share code, notes, and snippets.

@davcri
Last active January 27, 2022 19:01
Show Gist options
  • Save davcri/062690d3c88fc0d67e91d749bc6eb4c8 to your computer and use it in GitHub Desktop.
Save davcri/062690d3c88fc0d67e91d749bc6eb4c8 to your computer and use it in GitHub Desktop.
Provando... Pyxel (codice della live Twitch)
import pyxel
import math
w = 100
h = 100
pyxel.init(w, h)
pyxel.load("my_resource.pyxres")
road_width = w / 2
input_movimento = ""
car_x = w / 2 - 4
car_y = h / 2
collectible_x = w / 2
collectible_y = -30
collectible_taken = False
trattino_h = 10
class WhiteStripe():
def __init__(self) -> None:
self.x = 0
self.y = 0
def process(self, movement):
self.y += movement
pyxel.rectb(self.x - 1, self.y, 2, trattino_h, 7)
if self.y > h + trattino_h:
self.y = -trattino_h
white_stripes = []
# trattini bianchi
for i in range(math.floor(h / (trattino_h * 2)) + 1):
ws = WhiteStripe()
ws.x = w / 2
ws.y = i * 2 * trattino_h
white_stripes.append(ws)
def draw():
global car_x, collectible_y, collectible_taken
movement = pyxel.frame_count % h
pyxel.cls(3)
# asfalto
pyxel.rect(road_width / 2, 0, road_width, h, 13)
# trattini bianchi
collectible_y += 1
if input_movimento == "left":
car_x -= 3
elif input_movimento == "right":
car_x += 3
for stripe in white_stripes:
stripe.process(3)
if are_colliding(car_x, car_y, collectible_x, collectible_y):
collectible_taken = True
if not collectible_taken:
draw_collectible(collectible_x, collectible_y)
# macchina
pyxel.blt(car_x, car_y, 0, 4, 2, 8, 12, 0)
pyxel.flip()
def draw_collectible(x: int, y: int):
pyxel.blt(x, y, 0, 20, 2, 8, 12, 0)
def are_colliding(c1_x, c1_y, c2_x, c2_y, w = 8, h = 12):
return abs(c1_x - c2_x) < w and abs(c1_y - c2_y) < h
def update():
global input_movimento
if pyxel.btn(pyxel.KEY_LEFT):
input_movimento = "left"
elif pyxel.btn(pyxel.KEY_RIGHT):
input_movimento = "right"
else:
input_movimento = ""
if pyxel.btnp(pyxel.KEY_Q):
pyxel.quit()
# pyxel.play(0, 2)
pyxel.run(update, draw)
@marco-zanella
Copy link

Ho pensato a un'implementazione alternativa per are_colliding:
def are_colliding(c1_x, c1_y, c2_x, c2_y, w = 8, h = 12):
return abs(c1_x - c2_x) < w and abs(c1_y - c2_y) < h
in quella attuale mi sembra che w e h siano invertite

@davcri
Copy link
Author

davcri commented Jan 27, 2022

ciao @marco-zanella ! Hai completamente ragione, svista mia. Grazie per il suggerimento, appena posso correggo :)

@marco-zanella
Copy link

grazie a te @davcri , le tue live sono molto interessanti!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment