'How can I stop text from diapering on Ursina?
This game in not complete yet.
To play the top side of the board, click the right mouse button. To play the bottom side of the board click the left mouse button.
information
The cards are labeled with A, B, HP. A is to calculate the amount of damage the card can produce, B represents the amount of damage the card can handle and finally HP represents the cars health. here's my code:
# this is a ursina card game, the objective is to lower the other players' health as soon as possible
# imports
import random
from ursina import *
# time limit
min = 0
# play game
game = True
# update
def update():
global time, min,game,play_1,play_2,hp,hp_2,deck,deck_2,finder,finder_2
if game == True:
# player 1
if play_1 == True:
# hit animation
hit_box_2.animate_color((255, 0, 0, 255), duration=2)
hit_box_2.animate_color((0, 0, 0, 0), duration=1)
# deck script
if deck > 0:
deck -= 1
card_health = random.randint(1, 30)
card = Card(finder, hp=card_health, blocking=round(random.uniform(0, 9), 2),strength=round(random.uniform(0, 9), 2))
if card_health >= 20:
card.color = color.rgb(255, 0, 127)
elif card_health >= 10:
card.color = color.rgb(0, 0, 255)
else:
card.color = color.rgb(0, 204, 0)
# update hand list
hand.append(card)
# text display update
deck_size.text = 'Deck: {}'.format(deck)
# update finder
finder = 0
# player 2 health update
hp_2 -= 1
health_2.text = 'Player Health {}'.format(hp_2)
play_1 = False
# player 2
if play_2 == True:
# hit animation
hit_box.animate_color((255, 0, 0, 255), duration=2)
hit_box.animate_color((0, 0, 0, 0), duration=1)
# deck script
if deck_2 > 0:
deck_2 -= 1
card_health = random.randint(1, 30)
card = Card(finder_2, hp=card_health, blocking=round(random.uniform(0, 9), 2),strength=round(random.uniform(0, 9), 2))
card.y = 0.38
if card_health >= 20:
card.color = color.rgb(255, 0, 127)
elif card_health >= 10:
card.color = color.rgb(0, 0, 255)
else:
card.color = color.rgb(0, 204, 0)
# update hand list
hand_2.append(card)
# text display update
deck_size_2.text = 'Deck: {}'.format(deck)
# update finder
finder_2 = 0
# player 2 health update
hp -= 1
health.text = 'Player Health {}'.format(hp)
play_2 = False
# time
min += 1
if min >= 60:
min = 0
time -= 1
if time < 1:
game = False
timer.text = 'Timer: {}'.format(time)
app = Ursina()
# make the cards
class Card(Button):
def __init__(self, x,strength,blocking,hp):
super().__init__()
self.parent = camera.ui
self.model = 'quad'
self.scale = 0.2
self.color = color.white
self.highlight_color = color.red
self.x = x
self.y = -0.38
self.text = 'A: {} \nB: {} \nHP: {}'.format(strength,blocking,hp)
self.text_color = color.black
self.text_origin = (-0.4,-0.4)
# what card is in play and how it should react
def input(self, key):
global game, play_1,play_2,finder,finder_2
# which side is playing
if self.hovered:
if key == 'left mouse down':
if game == True:
play_1 = True
finder = self.x
# animation
self.animate_position((self.x,1,0),duration = 1)
self.animate_rotation((0,0,-90),duration = 0.9)
self.fade_out(0,duration = 0.89)
# remove card
destroy(self,delay = 2)
if key == 'right mouse down':
if game == True:
play_2 = True
finder_2 = self.x
# animation
self.animate_position((self.x,-1,0),duration = 1)
self.animate_rotation((0,0,90),duration = 0.9)
self.fade_out(0,duration = 0.89)
# remove card
destroy(self,delay = 2)
# timer
time = 60
timer = Text(text = 'Timer: {}'.format(time),color = color.black,size = 0.04,origin = (0,0))
# player
play_1 = False # if they are playing a card
hp = 100
deck = 50
finder = 0 # this will be used to replace the old card
# display on screen
bar = Entity(parent = camera.ui,model = 'quad',scale = 0.26,y = -0.37,x = -0.76)
health = Text(text = 'Player Health {}'.format(hp),y = -0.3,color = color.dark_gray,x = -0.86)
deck_size = Text(text = 'Deck: {}'.format(deck),y = -0.35,color = color.dark_gray,x = -0.86)
hit_box = Entity(parent = camera.ui,model = 'quad',scale = (16,0.26),y = -0.37,color = color.clear)
# player 1 hand
hand = []
x = -0.5
for y in range(6):
# deck size
deck -= 1
# making the hand
card_health = random.randint(1,30)
card = Card(x,hp = card_health,blocking = round(random.uniform(0,9),2),strength = round(random.uniform(0,9),2))
# card rarity
if card_health >= 20:
card.color = color.rgb(255,0,127)
elif card_health >= 10:
card.color = color.rgb(0,0,255)
else:
card.color = color.rgb(0,204,0)
x += 0.25
# hand
hand.append(card)
# computer ----------------------------------------------------------------------------------------------------------
# player 2
play_2 = False
hp_2 = 100
deck_2 = 50
finder_2 = 0 # this will be used to replace the old card
# display on screen
bar_2 = Entity(parent = camera.ui,model = 'quad',scale = 0.26,y = 0.37,x = -0.76)
health_2 = Text(text = 'Player Health {}'.format(hp_2),y = 0.3,color = color.black,x = -0.86)
deck_size_2 = Text(text = 'Deck: {}'.format(deck_2),y = 0.35,color = color.black,x = -0.86)
hit_box_2 = Entity(parent = camera.ui,model = 'quad',scale = (16,0.26),y = 0.37,color = color.clear)
# player 2 hand
hand_2 = []
x2 = -0.5
for y2 in range(6):
# deck size
deck_2 -= 1
# making the hand
card_health = random.randint(1,30)
card = Card(x2,hp = card_health,blocking = round(random.uniform(0,9),2),strength = round(random.uniform(0,9),2))
card.y = 0.38
# card rarity
if card_health >= 20:
card.color = color.rgb(255,0,127)
elif card_health >= 10:
card.color = color.rgb(0,0,255)
else:
card.color = color.rgb(0,204,0)
x2 += 0.25
# hand
hand_2.append(card)
app.run()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
