Python language: Example 1: Enter the integer grid size => 10 10 Enter a probabi
ID: 3722652 • Letter: P
Question
Python language:
Example 1: Enter the integer grid size => 10 10 Enter a probability (0.0 - 1.0) => 0.2 0.2 Saw a pokemon at turn 7, location (4, 5) Missed .. Saw a pokemon at turn 13, location (4, 5) Caught it! Saw a pokemon at turn 18, location (5, 3) Missed ••• Saw a pokemon at turn 19, location (5, 4) Missed ... Saw a pokemon at turn 20, location (5, 5) Caught it! Saw a pokemon at turn 23, location (6, 5) Caught it! Saw a pokemon at turn 26, location (4, 4) Missed ... Saw a pokemon at turn 29, location (3, 4) Missed ... Saw a pokemon at turn 35, location (1, 6) Caught it! Trainer left the field at turn 37, location (0, 7). 9 pokemon were seen, 4 of which were captured.Explanation / Answer
import pygame
pygame.init()
HEIGHT = 480
WIDTH = 640
screen=pygame.display.set_mode((WIDTH,HEIGHT))
BLACK = ( 0, 0, 0)
STEP = 16
BACKWARD = -STEP
FORWARD = STEP
up = False
down = False
left = False
right = False
#---------------------------------------#
# classes #
#---------------------------------------#
class AnimatedSprite(pygame.sprite.Sprite):
""" (fileName, int, int)
Sequence of 2D images, assembled in rows.
Inherits Sprite to use its Rect property.
See Sprite documentation here:
http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite
"""
def __init__(self, spritesheet, columns, rows):
pygame.sprite.Sprite.__init__(self)
self.x = 0
self.y = 0
self.visible = False
self.columns = columns
self.rows = rows
self.numImages = columns * rows # number of images in the file
self.spritesheet = pygame.image.load(spritesheet)
self.spritesheet = self.blend_image(spritesheet)
sheetrect = self.spritesheet.get_rect()
self.width,self.height = sheetrect.width, sheetrect.height
self.width = self.width/columns
self.height = self.height/rows
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
down = True
self.orientation_down()
def spawn(self, x, y):
""" Assign coordinates to the object and make it visible.
"""
self.x, self.y = x,y
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
self.visible = True
def draw(self, surface):
surface.blit(self.image, self.rect)
def blend_image(self, spritesheet):
""" Remove background colour of an image.
Need to use it when a picture is stored in BMP or JPG format, with opaque background.
"""
image_surface = pygame.image.load(spritesheet)
image_surface = image_surface.convert()
colorkey = image_surface.get_at((0,0))
image_surface.set_colorkey(colorkey)
return image_surface
def activate_up(self): # set of bool values designed as a
up = True # orientation detection
down = False
left = False # purpose is for user interface, using
right = False # arrow keys; when another key is pressed
def activate_down(self): # while one is already being held down,
down = True # it will cancel out the held and assign new
up = False
left = False
right = False
def activate_left(self):
left = True
up = False
down = False
right = False
def activate_right(self):
right = True
up = False
down = False
left = False
def orientation_up(self):
""" Clip-out one image from the spritesheet (as per the index),
and assign it as the current image.
"""
up = True
self.spritesheet.set_clip(0, 64, self.width, self.height)
self.image = self.spritesheet.subsurface(self.spritesheet.get_clip())
def orientation_down(self):
down = True
self.spritesheet.set_clip(0, 0, self.width, self.height)
self.image = self.spritesheet.subsurface(self.spritesheet.get_clip())
def orientation_left(self):
left = True
self.spritesheet.set_clip(0, 128, self.width, self.height)
self.image = self.spritesheet.subsurface(self.spritesheet.get_clip())
def orientation_right(self):
right = True
self.spritesheet.set_clip(0, 192, self.width, self.height)
self.image = self.spritesheet.subsurface(self.spritesheet.get_clip())
def walking_up(self,step):
if step<0:
self.orientation_up()
self.y = self.y + step
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
## self.image = pygame.transform.flip(self.image, True, False)
def walking_down(self,step):
if step>0:
self.orientation_down()
self.y = self.y + step
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
def walking_left(self,step):
if step<0:
self.orientation_left()
self.x = self.x + step
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
def walking_right(self,step):
if step>0:
self.orientation_right()
self.x = self.x + step
self.rect = pygame.Rect(self.x, self.y, self.width, self.height)
#---------------------------------------#
# functions #
#---------------------------------------#
def redraw_screen():
screen.fill(BLACK)
if player.visible:
player.draw(screen)
pygame.display.update()
#---------------------------------------#
# main program starts here #
#---------------------------------------#
player = AnimatedSprite("sprites/player.png",2,4)
player.spawn(100,400)
clock = pygame.time.Clock()
FPS = 10
inPlay = True
while inPlay:
clock.tick(FPS)
pygame.event.get()
keys = pygame.key.get_pressed()
if keys[pygame.K_ESCAPE]:
inPlay = False
if keys[pygame.K_UP]:
player.walking_up(BACKWARD)
if keys[pygame.K_DOWN]:
player.walking_down(FORWARD)
if keys[pygame.K_LEFT]:
player.walking_left(BACKWARD)
if keys[pygame.K_RIGHT]:
player.walking_right(FORWARD)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if not player.walking_up(BACKWARD):
if up == False:
player.activate_up()
player.orientation_up()
if event.key == pygame.K_DOWN:
if down == False:
player.activate_down()
player.orientation_down()
if event.key == pygame.K_LEFT:
if not player.walking_left(BACKWARD):
if left == False:
player.activate_left()
player.orientation_left()
if event.key == pygame.K_RIGHT:
if not player.walking_right(FORWARD):
if right == False:
player.activate_right()
player.orientation_right()
redraw_screen()
#---------------------------------------#
pygame.quit()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.