Here is the code.
import random
import arcade
SCREEN_WIDTH = 960
SCREEN_HEIGHT = 720
SCREEN_TITLE = "Гонки"
CAR_ANGLE = 20
CAR_SPEED = 5
WALL_SPEED = 5
class Car(arcade.Sprite):
def update(self):
self.center_x += self.change_x
class Wall(arcade.Sprite):
def __init__(self, filename, scale):
super().__init__(filename, scale)
self.score = 0 # Initialize the score attribute
def update(self):
self.center_y -= self.change_y
if self.top < 0:
self.bottom = SCREEN_HEIGHT + random.randint(0, SCREEN_HEIGHT)
self.center_x = random.randint(168, SCREEN_WIDTH - 168)
self.score += 1 # Increment the score
return self.score # Return the updated score
class MyGame(arcade.Window):
def __init__(self, width, height, title):
super().__init__(width, height, title)
self.bg = arcade.load_texture('bg.png')
self.car = Car("yellow_car.png", 0.8)
self.car.center_x = SCREEN_WIDTH / 2
self.car.center_y = 100
self.score = 0 # Initialize score attribute for the game
self.wall = Wall('wall.png', 0.8)
self.wall.center_y = SCREEN_HEIGHT
self.wall.center_x = random.randint(168, SCREEN_WIDTH - 168)
self.wall.change_y = WALL_SPEED
self.game = True
def update(self, delta_time: float):
if self.game:
self.car.update()
self.score = self.wall.update() # Update score from Wall class
def on_draw(self):
self.clear()
arcade.draw_texture_rectangle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT, self.bg)
self.car.draw()
self.wall.draw()
if self.game:
arcade.draw_text(f"Your score is {self.score}", 10, 10, arcade.color.AFRICAN_VIOLET, font_size=26)
else:
arcade.draw_text("Авария!", SCREEN_WIDTH / 2 - 150, SCREEN_HEIGHT / 2, arcade.color.CYAN, font_size=60)
if self.score == 10:
arcade.draw_text("Выигрыш!", SCREEN_WIDTH / 2 - 150, SCREEN_HEIGHT / 2, arcade.color.GOLD, font_size=60)
def update(self, delta_time: float):
if self.game:
self.car.update()
self.wall.update()
if arcade.check_for_collision(self.car, self.wall):
self.game = False
def on_key_press(self, symbol: int, modifiers: int):
if self.game:
if symbol == arcade.key.LEFT:
self.car.change_x = -CAR_SPEED
self.car.angle = CAR_ANGLE
if symbol == arcade.key.RIGHT:
self.car.change_x = CAR_SPEED
self.car.angle = -CAR_ANGLE
def on_key_release(self, symbol: int, modifiers: int):
if symbol == arcade.key.LEFT:
self.car.change_x = 0
self.car.angle = 0
if symbol == arcade.key.RIGHT:
self.car.change_x = 0
self.car.angle = 0
window = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
arcade.run()
There is a problem especially in this chunk:
def on_draw(self):
self.clear()
arcade.draw_texture_rectangle(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_WIDTH, SCREEN_HEIGHT, self.bg)
self.car.draw()
self.wall.draw()
if self.game:
arcade.draw_text(f"Your score is {self.score}", 10, 10, arcade.color.AFRICAN_VIOLET, font_size=26)
When I start the game, on the screen it should say how much the score is when the plank goes below the 0y coordinate. But when it goes by, nothing happens and the score stays at zero.