collision with rock

This commit is contained in:
s464923 2024-03-24 19:28:14 +01:00
parent 375eec002f
commit 7351c59ab7
11 changed files with 35 additions and 14 deletions

View File

@ -1,25 +1,45 @@
import pygame import pygame
from constant import size, rows, cols from constant import size, rows, cols
import random
class Board: class Board:
def __init__(self): def __init__(self):
self.board = [] self.board = []
self.load_images()
self.generate_board()
def load_images(self): def load_images(self):
self.grass = pygame.image.load("grass.png") self.grass = pygame.image.load("board/grass.png")
self.dirt = pygame.image.load("dirt.png") self.dirt = pygame.image.load("board/dirt.png")
self.rock= pygame.image.load("board/rock.png")
self.weeds = pygame.image.load("board/weeds.png")
def generate_board(self):
self.board = [[random.choice([0,1,2,3,4,5,6,7,8,9]) for _ in range(rows)] for _ in range(cols)]
def draw_cubes(self, win): def draw_cubes(self, win):
for row in range(rows): for row in range(rows):
for col in range(cols): for col in range(cols):
cube_rect = pygame.Rect(row * size, col * size, size, size) cube_rect = pygame.Rect(row * size, col * size, size, size)
cube=self.board[row][col]
if (row) % 2 == 0: if row==4 and col==4:
win.blit(self.grass, cube_rect)
elif cube == 0:
rock_scale = pygame.transform.scale(self.rock, (size, size))
win.blit(self.dirt, cube_rect)
win.blit(rock_scale, cube_rect)
elif cube == 1:
weed_scale = pygame.transform.scale(self.weeds, (size,size))
win.blit(self.grass, cube_rect)
win.blit(weed_scale, cube_rect)
elif cube in(2,3,4,5):
win.blit(self.grass, cube_rect) win.blit(self.grass, cube_rect)
else: else:
win.blit(self.dirt, cube_rect) win.blit(self.dirt, cube_rect)
def is_rock(self, col, row):
return self.board[row][col] == 0

View File

Before

Width:  |  Height:  |  Size: 699 KiB

After

Width:  |  Height:  |  Size: 699 KiB

View File

Before

Width:  |  Height:  |  Size: 62 KiB

After

Width:  |  Height:  |  Size: 62 KiB

BIN
board/rock.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

BIN
board/weeds.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 93 KiB

View File

@ -25,16 +25,16 @@ def main():
run = False run = False
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
if keys[pygame.K_UP] and tractor.row > 0: if keys[pygame.K_UP] and tractor.row > 0 and not board.is_rock(tractor.row-1, tractor.col):
tractor.row -= 1 tractor.row -= 1
tractor.direction = "up" tractor.direction = "up"
if keys[pygame.K_DOWN] and tractor.row < rows-1: if keys[pygame.K_DOWN] and tractor.row < rows-1 and not board.is_rock(tractor.row+1,tractor.col):
tractor.row += 1 tractor.row += 1
tractor.direction = "down" tractor.direction = "down"
if keys[pygame.K_LEFT] and tractor.col > 0: if keys[pygame.K_LEFT] and tractor.col > 0 and not board.is_rock(tractor.row,tractor.col-1):
tractor.col -= 1 tractor.col -= 1
tractor.direction = "left" tractor.direction = "left"
if keys[pygame.K_RIGHT] and tractor.col < cols -1: if keys[pygame.K_RIGHT] and tractor.col < cols -1 and not board.is_rock(tractor.row,tractor.col+1):
tractor.col += 1 tractor.col += 1
tractor.direction = "right" tractor.direction = "right"

View File

@ -5,12 +5,13 @@ class Tractor:
self.row = row self.row = row
self.col = col self.col = col
self.images = { self.images = {
"up": pygame.image.load("up.png"), "up": pygame.image.load("tractor/up.png"),
"down": pygame.image.load("down.png"), "down": pygame.image.load("tractor/down.png"),
"left":pygame.image.load("left.png"), "left": pygame.image.load("tractor/left.png"),
"right":pygame.image.load("right.png") "right":pygame.image.load("tractor/right.png")
} }
self.direction = "down" self.direction = "down"
def draw(self, win): def draw(self, win):
tractor_image = self.images[self.direction] tractor_image = self.images[self.direction]
win.blit(tractor_image, (self.col*size, self.row*size)) win.blit(tractor_image, (self.col*size, self.row*size))

View File

Before

Width:  |  Height:  |  Size: 3.8 KiB

After

Width:  |  Height:  |  Size: 3.8 KiB

View File

Before

Width:  |  Height:  |  Size: 5.0 KiB

After

Width:  |  Height:  |  Size: 5.0 KiB

View File

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB