Python Forum
[PyGame] adding mouse control to game
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] adding mouse control to game
#3
Hi,

thank you for answering...

I think, I got it now...

Because there were problems, if
self.rect.centerx = max(self.xmin, min(self.xmax, xpos))
is inside the function "spaceship.update()"
I put it in a own function "move()".

Now it works...

#create spaceship class
class Spaceship(pygame.sprite.Sprite):
	def __init__(self, x, y, health):
		pygame.sprite.Sprite.__init__(self)
		self.x = x
		self.y = y
		self.health_start = health
		self.health_remaining = health
		self.image = pygame.image.load("images/ship.png")
		self.rect = self.image.get_rect()
		self.rect.center = [x, y]
		self.xmin = self.rect.width // 2  # Compute Spaceship x range.
		self.xmax = screen_width - self.xmin
		self.last_shot = pygame.time.get_ticks()

	def move(self, xpos):
		self.rect.centerx = max(self.xmin, min(self.xmax, xpos))

	def update(self):
		#set a cooldown variable
		cooldown = 500 #milliseconds
		game_over = 0
		#record current time
		time_now = pygame.time.get_ticks()
		#shoot, get key press
		key = pygame.key.get_pressed()
		if key[pygame.K_SPACE] and time_now - self.last_shot > cooldown:
			#laser_fx.play()
			bullet_1 = Bullets(self.rect.centerx - 43, self.rect.top)
			bullet_2 = Bullets(self.rect.centerx + 43, self.rect.top)
			#bullet = Bullets(self.rect.centerx, self.rect.top)
			bullet_group.add(bullet_1)
			bullet_group.add(bullet_2)
			self.last_shot = time_now

		#update mask
		self.mask = pygame.mask.from_surface(self.image)

		#draw health bar
		pygame.draw.rect(screen, red, (self.rect.x, (self.rect.bottom + 10), self.rect.width, 15))
		if self.health_remaining > 0:
			pygame.draw.rect(screen, green, (self.rect.x, (self.rect.bottom + 10), int(self.rect.width * (self.health_remaining / self.health_start)), 15))
		elif self.health_remaining <= 0:
			explosion = Explosion(self.rect.centerx, self.rect.centery, 3)
			explosion_group.add(explosion)
			self.kill()
			game_over = -1
		return game_over
Reply


Messages In This Thread
adding mouse control to game - by flash77 - Apr-06-2024, 06:39 PM
RE: adding mouse control to game - by deanhystad - Apr-06-2024, 09:24 PM
RE: adding mouse control to game - by flash77 - Apr-08-2024, 06:52 PM
RE: adding mouse control to game - by flash77 - May-13-2024, 07:03 PM
RE: adding mouse control to game - by deanhystad - May-13-2024, 08:37 PM
RE: adding mouse control to game - by flash77 - May-14-2024, 04:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding an inventory and a combat system to a text based adventure game detkitten 2 6,974 Dec-17-2019, 03:40 AM
Last Post: detkitten
  Adding persistent multiple levels to game michael1789 2 2,467 Nov-16-2019, 01:15 AM
Last Post: michael1789
  Adding a single player mode to my wxOthello game keames 29 12,317 May-01-2019, 02:56 AM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020