Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Format Code According to PEP8#10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
sayampradhan wants to merge1 commit intocodebasics:main
base:main
Choose a base branch
Loading
fromsayampradhan:patch-5
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions1_snake_game/5_snake_eats_apple_and_score.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -7,6 +7,7 @@

SIZE = 40


class Apple:
def __init__(self, parent_screen):
self.parent_screen = parent_screen
Expand All@@ -19,45 +20,46 @@ def draw(self):
pygame.display.flip()

def move(self):
self.x = random.randint(1,25)*SIZE
self.y = random.randint(1,20)*SIZE
self.x = random.randint(1, 25) * SIZE
self.y = random.randint(1, 20) * SIZE


class Snake:
def __init__(self, parent_screen, length):
self.parent_screen = parent_screen
self.image = pygame.image.load("resources/block.jpg").convert()
self.direction ='down'
self.direction ="down"

self.length = length
self.x = [40]*length
self.y = [40]*length
self.x = [40] *length
self.y = [40] *length

def move_left(self):
self.direction ='left'
self.direction ="left"

def move_right(self):
self.direction ='right'
self.direction ="right"

def move_up(self):
self.direction ='up'
self.direction ="up"

def move_down(self):
self.direction ='down'
self.direction ="down"

def walk(self):
# update body
for i in range(self.length-1,0,-1):
self.x[i] = self.x[i-1]
self.y[i] = self.y[i-1]
for i in range(self.length - 1, 0,-1):
self.x[i] = self.x[i -1]
self.y[i] = self.y[i -1]

# update head
if self.direction =='left':
if self.direction =="left":
self.x[0] -= SIZE
if self.direction =='right':
if self.direction =="right":
self.x[0] += SIZE
if self.direction =='up':
if self.direction =="up":
self.y[0] -= SIZE
if self.direction =='down':
if self.direction =="down":
self.y[0] += SIZE

self.draw()
Expand All@@ -74,6 +76,7 @@ def increase_length(self):
self.x.append(-1)
self.y.append(-1)


class Game:
def __init__(self):
pygame.init()
Expand All@@ -90,21 +93,22 @@ def is_collision(self, x1, y1, x2, y2):
return False

def display_score(self):
font = pygame.font.SysFont('arial',30)
score = font.render(f"Score: {self.snake.length}",True,(200,200,200))
self.surface.blit(score,(850,10))
font = pygame.font.SysFont("arial",30)
score = font.render(f"Score: {self.snake.length}",True,(200,200,200))
self.surface.blit(score,(850,10))

def play(self):
self.snake.walk()
self.apple.draw()
self.display_score()
pygame.display.flip()

if self.is_collision(self.snake.x[0], self.snake.y[0], self.apple.x, self.apple.y):
if self.is_collision(
self.snake.x[0], self.snake.y[0], self.apple.x, self.apple.y
):
self.snake.increase_length()
self.apple.move()


def run(self):
running = True

Expand All@@ -131,10 +135,9 @@ def run(self):

self.play()

time.sleep(.2)
time.sleep(0.2)

if __name__ == '__main__':

if __name__ == "__main__":
game = Game()
game.run()



[8]ページ先頭

©2009-2025 Movatter.jp